Wordpress教程 2023年04月19日
0 收藏 0 点赞 1,047 浏览 4295 个字

通过循环调用分类 ID 为 6、7、8 的文章,并按照一定的规则显示文章列表和相关信息。

首先,通过 array(6, 7, 8) 定义了需要调用的分类 ID 数组,然后通过 foreach 循环每个分类,使用 get_cat_name() 函数获取分类名称,并将其作为标题输出。

接下来,通过 WP_Query 建立了文章查询,并设置了每个分类只显示 5 篇文章,而 if ($query->have_posts()) 判断当前分类是否有文章,有则输出文章列表。

在文章列表的循环中,首先定义了一个计数器 $count,用于限制只输出 5 篇文章。在循环中,通过 get_post_meta() 函数获取文章的 is_top 自定义字段,如果当前文章为置顶文章,则将其加上 top 的 CSS 类,以便样式上进行区分。

如果文章不是置顶文章,而且 $count 小于 5,就将文章信息以列表项的形式输出。否则,跳出循环。

最后,通过 wp_reset_postdata() 函数重置文章查询,保证每个分类之间不会相互影响。最外层的 div 标签则是整个分类列表的外包装。

此代码主要使用了 WordPress 的分类查询、文章查询、自定义字段等功能,通过简单的逻辑控制,实现了对文章列表的定制化输出。

<?php
$categories = get_categories(array('include' => array(6,7,8)));
foreach ($categories as $category) :
    $args = array(
        'cat' => $category->term_id,
        'posts_per_page' => 5,
        'post__not_in' => get_option('sticky_posts') // 排除置顶文章
    );
    $query = new WP_Query($args);
    $sticky_args = array(
        'cat' => $category->term_id,
        'posts_per_page' => -1,
        'post__in' => get_option('sticky_posts') // 查询所有置顶文章
    );
    $sticky_query = new WP_Query($sticky_args);
    if ($sticky_query->have_posts()) :
?>
        <div class="bankuai">
            <div class="xl12 xs6 xm4 margin-large-bottom">
                <div class="item item1">
                    <h3><?php echo $category->name; ?></h3>
                    <ul>
                        <?php while ($sticky_query->have_posts()) : $sticky_query->the_post(); ?>
                            <li>
                                <span class="float-right"><?php the_time('Y-m-d'); ?></span>
                                <a href="<?php the_permalink(); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ><?php the_title(); ?></a>
                            </li>
                        <?php endwhile; ?>
                        <?php
                            $posts_count = $sticky_query->post_count;
                            if ($posts_count < 5 && $query->have_posts()) :
                                $posts_to_show = 5 - $posts_count;
                                while ($query->have_posts() && $posts_to_show > 0) :
                                    $query->the_post(); ?>
                                    <li>
                                        <span class="float-right"><?php the_time('Y-m-d'); ?></span>
                                        <a href="<?php the_permalink(); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ><?php the_title(); ?></a>
                                    </li>
                                    <?php
                                        $posts_to_show--;
                                    endwhile;
                                endif;
                            ?>
                    </ul>
                    <p class="more"><a href="<?php echo get_category_link($category->term_id); ?>" rel="external nofollow"  rel="external nofollow"  target="_blank"><i class="icon-plus"></i>READ MORE</a></p>
                </div>
            </div>
        </div>
<?php
    elseif ($query->have_posts()) :
?>
        <div class="bankuai">
            <div class="xl12 xs6 xm4 margin-large-bottom">
                <div class="item item1">
                    <h3><?php echo $category->name; ?></h3>
                    <ul>
                        <?php while ($query->have_posts()) : $query->the_post(); ?>
                            <li>
                                <span class="float-right"><?php the_time('Y-m-d'); ?></span>
                                <a href="<?php the_permalink(); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ><?php the_title(); ?></a>
                            </li>
                        <?php endwhile; ?>
                    </ul>
                    <p class="more"><a href="<?php echo get_category_link($category->term_id); ?>" rel="external nofollow"  rel="external nofollow"  target="_blank"><i class="icon-plus"></i>READ MORE</a></p>
                </div>
            </div>
        </div>
<?php
    endif;
    wp_reset_postdata();
endforeach;
?>

 

今天有客户说要修改代码以按特定顺序显示类别。下面安排

注意:本段内容隐藏解压密码/提取密码必须成功评论后刷新获取!

回复本文刷新本页

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.mizhanw.com/blog/2134.html

相关推荐
WordPress去除/禁止后台侧栏插件-主题-版本更新提示
今天使用了张戈修改的nginx-help插件修改版,官方版并无自动重启功能,因此不能更新。但是这WordPress后台一直有更新提示,这可真…
日期:2024-12-30 点赞:0 阅读:26
WordPress网站js防扒代码-禁止右键/F12/调试自动关闭窗口
网站防扒老生常谈了,今天老白博客@老白也给大家分享一下从网上看到的“WordPress网站防扒代码/禁止F12调试教程-以7b2主题为例” …
日期:2024-12-30 点赞:0 阅读:36
WordPress优化加速之导航菜单栏缓存Menu Cache
今天老白博客@老白给大家分享又一个WordPress优化加速教程:“导航菜单栏缓存WordPress插件Menu Cache”。我们都知道数…
日期:2024-12-30 点赞:0 阅读:27
2024基于网站性能测试的WordPress优化指南
老白博客WordPress优化高级教程:在如今5G遍地走,千兆多如狗的高速网络时代,如果你访问网站,还出现转圈圈的情况,那么你十有八九是要关…
日期:2024-12-30 点赞:0 阅读:31
WP网站用户行为监测回放插件nicen-replay
在数字营销的世界里,了解用户行为是提升用户体验和转化率的关键。老白推荐一款炫酷的WordPress插件:nicen-replay 。它能够让…
日期:2024-12-30 点赞:0 阅读:31
2024新WordPress缓存插件atec Cache APCu
WordPress插件atec Cache APCu 是一款2024年才投入使用的的高效缓存插件,与其他内存缓存选项(如 Redis 和 M…
日期:2024-12-30 点赞:0 阅读:38
发表评论
暂无评论

还没有评论呢,快来抢沙发~