最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Display all posts from specific category

matteradmin8PV0评论

Problem:

I'm trying to display all posts from a specific category named 'Culture' (slug: culture) in my website. However, I'm always getting an empty field.

My Attempt:

I've tried to use the same structure used in the custom template for other options. In this custom template, there's a tab container with 4 options: Latest, Trending, Videos and Galleries.

For example, to get all posts with a video post format there is this code:

<?php query_posts(array( 'tax_query' => array( array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video' )) )); if (have_posts()) : ?>
    <li>
        <a href="#videos"><span class="home-head-toggle-item"><?php esc_html_e( 'Videos', 'template' ); ?></span></a>
    </li>
<?php endif; wp_reset_query(); ?>

So I adapted it to:

<?php query_posts(array( 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => array( 'culture' ) )) )); if (have_posts()) : ?>
    <li>
        <a href="#culture"><span class="home-head-toggle-item"><?php esc_html_e( 'Culture', 'template' ); ?></span></a>
    </li>
<?php endif; wp_reset_query(); ?>

Unfortunately, it didn't work as I expected and nothing appears.

Am I doing something wrong?

Let me know if you need further details.

Thanks in advance!

Problem:

I'm trying to display all posts from a specific category named 'Culture' (slug: culture) in my website. However, I'm always getting an empty field.

My Attempt:

I've tried to use the same structure used in the custom template for other options. In this custom template, there's a tab container with 4 options: Latest, Trending, Videos and Galleries.

For example, to get all posts with a video post format there is this code:

<?php query_posts(array( 'tax_query' => array( array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video' )) )); if (have_posts()) : ?>
    <li>
        <a href="#videos"><span class="home-head-toggle-item"><?php esc_html_e( 'Videos', 'template' ); ?></span></a>
    </li>
<?php endif; wp_reset_query(); ?>

So I adapted it to:

<?php query_posts(array( 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => array( 'culture' ) )) )); if (have_posts()) : ?>
    <li>
        <a href="#culture"><span class="home-head-toggle-item"><?php esc_html_e( 'Culture', 'template' ); ?></span></a>
    </li>
<?php endif; wp_reset_query(); ?>

Unfortunately, it didn't work as I expected and nothing appears.

Am I doing something wrong?

Let me know if you need further details.

Thanks in advance!

Share Improve this question asked Nov 7, 2018 at 0:34 Maria HerreraMaria Herrera 1
Add a comment  | 

1 Answer 1

Reset to default 0

In your query post, I don't see your post type parameter. You must need this parameter to get all posts. Please try this follow the code and put it anywhere you want to show it.

<?php 
$post_args = array(
    'post_type' => 'post', //get post by 'post' (default post type).
    'posts_per_page' => -1, //show all posts.
    'tax_query' => array( 
        array( 
            'taxonomy' => 'category', 
            'field' => 'slug', 
            'terms' => array('culture') 
        )
    ) 
);
$the_qry = new WP_Query($post_args);
if($the_qry->have_posts()){
    while($the_qry->have_posts()){
        $the_qry->the_post();

        //show all list the post.
        echo '<li>
                <a href="'.get_permalink().'">'.get_the_title().'</a>
            </li>';
    }
    wp_reset_postdata();
}
?>
Post a comment

comment list (0)

  1. No comments so far