Support

Account

Home Forums General Issues Display posts from specified category

Helping

Display posts from specified category

  • I’m trying to use categories to allow for pages to display only posts from a specific category. There are two different templates I need to use this in. The first is simply pages to display posts from each category (preferably chosen by the page title). The second allows for the selection of featured posts that would be the only posts to display on the home page.

    I’m having trouble figuring out how to include the taxonomy for categories. Shouldn’t I be able to pass it into the array in $args in my WP_Query? Here’s my WP_Query, hoping I can just add a line to target the page title, or to target a category name.

    <?php
    	$args = array(
    		'post_type' => 'project'
    	);
    	$query = new WP_Query( $args );
    ?>
    
    <ul>
    	<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
    
    	<li class="projects gutters">
    		<a href="<?php the_permalink(); ?>">
    			<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>" class="project_pics">
    			<h5 class="project_name uppercase"><?php the_title(); ?></h5>
    		</a>
    	</li>
    
    	<?php endwhile; endif; wp_reset_postdata(); ?>
    </ul>
  • Yes, you can include a tax_query, or you can include category parameters in your query if you are using the standard categories taxonomy.

    However, if you’re using a custom post type I would suggest that you also use a custom taxonomy. The reason is that WP does not know how to tell the difference between standard posts and a custom post type when showing post categories and your custom posts will probably appear on your category archive pages.

    For category parameters: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    For Tax Query: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Display posts from specified category’ is closed to new replies.