Wordpress教程 2023年04月19日
0 收藏 0 点赞 570 浏览 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 在购物车中的特定产品的WooCommerce结帐上显示某些国家/地区
要在 WooCommerce 结账页面上只显示特定的国家/地区,您可以使用以下代码在函数文件(functions.php)中添加相应的过滤器…
日期:2023-05-29 点赞:0 阅读:921
最新一键关闭WordPress后台主题、WordPress插件的自动更新提醒,禁止WordPress更新提醒
PHP版本低于7.2用这个代码: 将以下代码放入您的主题functions.php //修改代码在后台显示更新 add_filter('pr…
日期:2023-05-24 点赞:0 阅读:173
wordpress相关文章功能代码示例
本文来介绍下wordpress的相关文章功能实现代码。 首先还是来说明下这个相关文章是个什么逻辑,文章的相关性都是通过tag标签来关联的,如…
日期:2023-05-23 点赞:0 阅读:937
woocommerce添加结账页面缩略图和货号+购物车页面货号
购物车上添加图片和货号SKU functions.php文件里面添加: /** * Display SKU in cart table wi…
日期:2023-05-18 点赞:0 阅读:1,054
codestar-framework获取后台设置控制指定分类文章和显示数量
可以在主题模板中使用 _izhanke() 函数获取后台设置的 xinwen_num 字段的值,然后将其传递到 query_posts() …
日期:2023-05-10 点赞:0 阅读:1,000
怎么指定邮箱可注册?
一些做会员网站的站长,肯定很讨厌那种用临时邮箱注册的用户吧,这次分享一段WordPress代码,让用户只能用指定的邮箱注册。 在主题根目录下…
日期:2023-05-09 点赞:0 阅读:696
发表评论
暂无评论

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