0

My site contains posts separated into 6 categories. Each category has it's own page with a custom loop on each page displaying posts from the respective category.

I would like to consolidate all posts to one page with 6 category buttons, so that when a button is selected the posts from that category are displayed in the container.

There are 2 ways I imagine this can be done.

1) One custom loop with the category_name as a variable, where each button contains a value for the variable, added upon button press.

2) Multiple custom loops on one page, each button selects which loop will be run.

My inexperience coding is more apparent then ever to me now as I'm not sure what approach / languages will help me accomplish this best.

I've come this far (code below) and I'm willing to learn whatever is necessary as I work through this. Any guidance and support on what direction I should take this in to achieve my goal is greatly appreciated.

<?php
// Disable warnings and notices
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);

// Define custom query parameters
$custom_query_args = array(     
    'post_type'         => 'post',
    'category_name'     => 'cat-1',
    'posts_per_page'    => 15,
    'meta_key'          => 'post_status',
    'orderby'           => 'meta_value',
    'order'             => 'DESC' );

// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );

// Pagination fix
$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $custom_query;
?>
<?php if( $custom_query->have_posts() ): ?>
    <div>
    <?php while( $custom_query->have_posts() ) : $custom_query->the_post(); 

        $class = get_field('post_status') ? 'class="post_status"' : '';

        ?>

        <div class="post-link" <?php echo $class; ?>>
            <a href="<?php the_permalink(); ?>">
      <?php $tpost_status = get_field('post_status'); ?>
              <?php if(is_array($post_status)): ?>

                <?php if( in_array('30', $post_status) ): ?>
                  <span class="new-badge">New</span>
               <?php endif; ?>
               <?php if( in_array('20', $post_status) ): ?>
                  <span class="active-badge">Active</span>
               <?php endif; ?>
               <?php if( in_array('10', $post_status) ): ?>
                  <span class="closed-badge">Closed</span>  
               <?php endif; ?>
              <?php endif; ?>
            <?php the_title(); ?></a>
        </div>
<?php
    endwhile;
endif;
// Reset postdata
wp_reset_postdata();

// Custom query loop pagination

previous_posts_link( '<div class="prev-btn">Prev</div>' );
next_posts_link( '<div class="next-btn">Next</div>', $custom_query->max_num_pages );


// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.