wordpress:为第一篇文章设置自定义类,然后是下 2 篇文章,其余的则不同

天意之网

我正在寻找一种方法来修改此代码以使第一个样式不同,然后下 2 个帖子的样式也不同,而帖子的其余部分的样式也将不同。

<?php
$queryObject = new  Wp_Query( array(
    'showposts' => 5,
    'post_type' => array('post'),
    'category_name' => videos,
    'orderby' => 1,
    ));

// The Loop
if ( $queryObject->have_posts() ) :
    $i = 0;
    while ( $queryObject->have_posts() ) :
        $queryObject->the_post();
        if ( $i == 0 ) : ?>
            <div class="first-post">
            <a href="<?php the_permalink(); ?>" title="<?php printf(__( 'Read %s', 'wpbx' ), wp_specialchars(get_the_title(), 1)) ?>">
                <?php the_post_thumbnail('sidethumbs'); ?>
            </a>
        <?php endif;
        if ( $i != 0 ) : ?>
            <div class="secondary-post">
        <?php endif; ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
            <?php the_title(); ?>
        </a>
        </div>
        <?php $i++;
    endwhile;
endif;
?>

我已经尝试过,但这是我迄今为止所拥有的。请帮忙。

斯坦德

所以一个 switch case 可能看起来像这样 - 这样你就可以指定第一个、第二个和其余的应该是什么样子 - 但你也可以只使用 if(){} elseif(){} else{}

<?php
$queryObject = new  Wp_Query( array(
'showposts' => 5,
'post_type' => array('post'),
'category_name' => videos,
'orderby' => 1,
));

// The Loop
if ( $queryObject->have_posts() ) :
$i = 0;
while ( $queryObject->have_posts() ) :
    $queryObject->the_post();
    $postClass = "";
    switch ($i) {
        case 0:
            $postClass = "firstpost";?>
            <div class="<?php echo $postClass;?>">
                <a href="<?php the_permalink(); ?>" title="<?php printf(__( 'Read %s', 'wpbx' ), wp_specialchars(get_the_title(), 1)) ?>">
                    <?php the_post_thumbnail('sidethumbs'); ?>
                </a>
            </div>
        <?php break;
        case 1:
            $postClass = "secondpost";
            //More of the same stuff.
        break;
        default:
           $postClass = "restOfPosts";
           //How the rest of the posts should look like
        break;
    }
$i++;
endwhile;
endif;
?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

只选择作者的最后一篇文章

在一篇文章中使用2个中间件功能

显示wordpress的第一篇文章

如何显示每个标签的第一篇文章

WordPress的:第一篇,专栏;其他文章,2栏

如何用octopress可视化我的第一篇文章?

获取Instagram用户的第一篇文章

如何自定义博客作者的第一篇文章

jQuery UI手风琴仅适用于第一篇文章:WordPress自定义文章类型

如何自定义仅存档/主页上的第一篇文章的CSS?

下一篇和上一篇文章以相同的术语链接自定义分类法

WordPress的第一篇文章只在首页上有所不同

WordPress循环会忽略三个单独的meta_values的第一篇文章

一篇文章中多个外部链接的Wordpress高级自定义字段外部链接文本

用laravel记录一篇文章的浏览次数?

Rails App中的上一篇文章

删除第一篇文章并在PHP中显示第二篇文章

使用wp_query检查第一篇文章

PHP - Wordpress 上一篇文章链接显示在最后一篇文章中

如何显示与其他人不同的wordpress第一篇文章?

WP posts_per_page 和偏移量跳过第一篇文章

在 Wordpress 循环中的第一篇文章后添加一个 div

Wordpress 只显示一篇文章

渲染另一篇文章的引用

单击自定义分类法时,如何使用第一篇文章数据在同一页面上重定向?

在顶部显示模型中的第一篇文章

仅在 Vanilla JavaScript 中附加在第一篇文章上的评论

为什么我循环中的第一篇文章意外缩进

仅用于一篇文章的不同 url 路径(Django)