【问题标题】:Change WordPress loop to show category更改 WordPress 循环以显示类别
【发布时间】:2020-06-08 15:28:30
【问题描述】:

我有一个简单的 WordPress 循环来获取 100 个最新帖子。如何更改此设置以根据我所在的类别动态显示帖子?

  <?php
    // the query
    $wpb_all_query = new WP_Query(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'post-no' => 100));
    ?>
    <?php if ($wpb_all_query->have_posts()) :
        while ($wpb_all_query->have_posts()) : $wpb_all_query->the_post(); ?>
            <img class="card-img-top bottom-line mb-2 lozad "
                             data-src="<?php the_post_thumbnail_url(); ?>"
                             alt="<?php the_title(); ?>"/>
        <?php endwhile; ?>
    <?php else : ?>

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    如果您的意思是您在类别存档页面上,那么此代码将起作用:

    // gets the category information from the archive page
    $cat = get_the_category();
    
    // the query
    $wpb_all_query = new WP_Query(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        // queries for posts that have that category
        'cat' => $cat[0]->cat_ID,
        'post-no' => 100));
    

    【讨论】:

    • 你好@katie,抱歉耽搁了,我的意思是当我在一个类别上只显示类别帖子时。我试试你的答案看看我能不能让它工作
    猜你喜欢
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多