I have 2 categories set up: DAY TOURS and a region: SANTA FE. I have created a category-santa-fe.php, but it has the following code. How do I adjust the markup to only show posts that are also in the DAY TOURS category?
<?php if(have_posts()): while(have_posts()): the_post(); ?>
<article role="article" id="post_<?php the_ID()?>" <?php post_class()?> style="margin-bottom:0px;">
<div >
<div style="padding:0; background-image:url('<?php the_post_thumbnail_url(); ?>'); background-size: cover; background-position:top center;" ><img src="<?php echo get_stylesheet_directory_uri() ?>/pillars/spacer.gif" /></div>
<div >
<div >
<h3><a href="<?php the_permalink(); ?>"><?php the_title()?></a></h3>
</div>
<p style="text-transform:uppercase; font-size:13px;"><?php if( get_field('difficulty_rating') ): ?>
DIFFICULTY RATING: <?php the_field('difficulty_rating'); ?> <em>(1 = Easy | 3 = Moderate | 5 = Difficult)</em>
<?php endif; ?></p>
<?php the_field('listing_page_short_description'); ?>
<p style="text-transform:uppercase; font-size:13px;">
<?php if( get_field('dates/seasons_offered') ): ?>
<?php the_field('dates/seasons_offered'); ?>
<?php endif; ?>
<?php if( get_field('price') ): ?>
<br/><?php the_field('price'); ?>
<?php endif; ?>
<?php if( get_field('price_additional_notes') ): ?>
<br />
<?php the_field('price_additional_notes'); ?>
<?php endif; ?>
</p>
<p align="right"><a href="<?php the_permalink(); ?>" >TOUR DETAILS</a></p>
</div>
</div><!--row-->
</article>
<?php endwhile; else: ?>
<div >
<i ></i> <?php _e('Sorry, your search yielded no results.', 'bst'); ?>
</div>
<?php endif; ?>
I am not sure how to parse by a second category...
CodePudding user response:
The default loop of your template looks at specific slug, this case being santa-fe
, and gets the posts with that category. To customize what posts to get you'll need to make your own query and get the posts with additional categories.
Do this with a WP_Query
instance where you search for posts with additional category terms. In the query below we're looking for all posts that are published and have both the category slugs santa-fe
and day-tours
.
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'santa-fe'
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'day-tours'
)
),
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) :
the_post(); ?>
<article role="article" id="post_<?php the_ID()?>" <?php post_class()?> style="margin-bottom:0px;">
<!-- PASTE THE REST OF YOUR HTML HERE -->
</article>
<?php
endwhile;
wp_reset_postdata();
elseif : ?>
<div >
<i ></i> <?php _e('Sorry, your search yielded no results.', 'bst'); ?>
</div>
<?php
endif; ?>
Replace the original loop with the custom WP_Query
loop.