Home Forums ACF PRO Show post from specific category on the archive page


Show post from specific category on the archive page

  • 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>
            <?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">&larr;</span> Previous', 'domain' ) ); ?></div>
                <div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">&rarr;</span>', 'domain' ) ); ?></div>
            <?php endif;

    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 category from specific url ?

  • This is not really an ACF question, it is a question about creating archive pages by in WP.

    When you load the URL for a CPT or a Term in a Taxonomy associated with that CPT WP has already done a query based on the CPT and the Term if applicable. Your query is overriding the query that WP already did.

    I would suggest the you look at pre_get_posts for and the WP template hierarchy rather than creating custom queries.

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

You must be logged in to reply to this topic.