【问题标题】:Display all posts in category wordpress显示 wordpress 类别中的所有帖子
【发布时间】:2016-05-18 19:50:46
【问题描述】:

我正在尝试在类别存档页面上显示该类别中的所有帖子。
我使用了以下代码,但它显示了所有类别的所有帖子。

有人可以帮帮我吗?

<?php 
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1)); ?>

<?php if ( $wpb_all_query->have_posts() ) : ?>

<ul>

    <!-- the loop -->
    <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <!-- end of the loop -->

</ul>

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

谢谢

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您需要在 WP_Query 数组中添加一个类别参数。 在“类别参数”部分查看此参考:https://codex.wordpress.org/Class_Reference/WP_Query

    这样的事情应该可以工作:

    $wpb_all_query = new WP_Query(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'category_name' => 'your_category_slug'
    ));
    

    【讨论】:

    • 我有 100 个类别。有没有办法拉入当前类别或者我需要创建 100 个模板?
    • 您的类别标签或 ID 在 URL 中吗?如果是这样,您可以使用它。
    • 类别名称将在 URL 中。我希望在每个类别存档页面上显示每个类别中的所有帖子。例如。 cat a 将显示 cat a 中的所有帖子
    • 如果在查询字符串中,可以使用函数:get_query_var('cat', 'default');其中“cat”是查询字符串参数名称,“default”是在未找到“cat”时要使用的默认值。如果它是路径的一部分,您可能需要在 / 上展开并找出该类别在结果数组中的哪个位置。
    • 这听起来有点出乎我的意料!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多