categories - Display all posts from specific category

admin2025-06-06  1

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();
}
?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749201379a317213.html

最新回复(0)