I am using ACF and CPT UI to add my projects. Each projects post have a custom category assigned to it.
I am using this code below to display all projects in the archive page…
<?php $loop = new WP_Query( array( 'post_type' => 'projets', 'paged' => $paged ) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-md-4 col-sm-6 p-4">
<div class="box">
<a href="<?php the_permalink(); ?>"><h3> <?php the_title(); ?></h3></a>
</div>
</div>
<?php endwhile;
if ( $loop->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Previous', 'domain' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">→</span>', 'domain' ) ); ?></div>
</div>
<?php endif;
endif;
wp_reset_postdata();?>
The code works well to get all the posts from all category but when I go to projects/category1, it still show all projects.
How can I modify this to show only the projects from specific category ?
2
Answers
this is a good reference for WP_Query
https://developer.wordpress.org/reference/classes/wp_query/
This will show only post from a specific category
replace ‘my_category_slug’ with the category you are interested in.
This though is usually not required have you refreshed your permalinks after creating the new post type. ie. go to settings permalinks and click save.
The standard loop should work for custom post types.
”’
”’
This would be your standard page layout to show the content then you would navigate to the the custom post type using http://yoursite.com/projets/
Though the loop above should already be included in your theme so you should rarely need to rewrite it unless building a theme from scratch.
have you tried WP default loop without any custom WP_Query i.e.