Wordpress教程 2023年04月19日
0 收藏 0 点赞 794 浏览 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

相关推荐
woocommerce 订单相关钩子
woocommerce_checkout_update_order_meta: 在订单元数据更新时触发。您可以在此钩子中更新订单的元数据,例…
日期:2024-03-15 点赞:0 阅读:38
最新Wp Rocket设置教程, 含插件下载+常见问题解决方法
最新Wp Rocket设置教程(超详细), 附GPL版本插件下载链接。图文并茂详细解释每一个设置的含义以及优缺点+兼容性排除,找到最合适的设…
日期:2024-02-24 点赞:0 阅读:120
WordPress破解版插件/主题(开心版, GPL, Nulled)安全吗?有啥缺点?如何获取?
解版插件产业链,安全与否,以及如何下载安全高质量破解版插件,避免下载有病毒主题/插件。 WordPress破解版插件/主题安全吗?这是绝大多…
日期:2024-02-24 点赞:0 阅读:118
终极WordPress速度优化教程, 秒开页面! 优化思路+方法+工具
终极WordPress速度优化教程,全面讲解影响速度的因素和原理。FCP, LCP, TTFB, TBT, CLS,Wp corn, Mar…
日期:2024-02-24 点赞:0 阅读:141
WordPress 的最佳 Cloudflare 设置(边缘涡轮增压)
无论您的 WordPress 主机有多快都无关紧要;您永远无法克服网络延迟。像 Cloudflare 这样的内容分发网络 (CDN) 通过将…
日期:2024-02-24 点赞:0 阅读:84
使用国外主题/插件后网站变很卡响应速度很慢的问题
变卡的原因 由于国内GFW的限制,有些资源被挡在海外无法加载,或者有些资源服务器延迟太高,导致网站加载速度缓慢,解决办法如下,二选一即可。 …
日期:2024-02-21 点赞:0 阅读:104
发表评论
暂无评论

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