简体   繁体   中英

WORDPRESS: How do set my Region Category to display all SANTA FE posts but only if they are also marked with the DAY TOURS category?

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 class="row pillar-row">
   
    <div style="padding:0; background-image:url('<?php the_post_thumbnail_url(); ?>'); background-size: cover; background-position:top center;" class="col-md-6 <?php if( $wp_query->current_post%2 == 1 ){ echo ' pull-left';}else{ echo ' pull-right';} ?>"><img src="<?php echo get_stylesheet_directory_uri() ?>/pillars/spacer.gif" class="img-responsive pillar-spacer" /></div>
     <div class="col-md-6 pillar-description">
         <div class="post-header">
            <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'); ?> &nbsp;&nbsp;<em>(1 = Easy &nbsp;|&nbsp; 3 = Moderate &nbsp;|&nbsp; 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(); ?>" class="btn btn-lg btn-default">TOUR DETAILS</a></p>
      </div>
             
   
        </div><!--row-->
        
        
     
    </article>
<?php endwhile; else: ?>
    <div class="alert alert-warning">
        <i class="glyphicon glyphicon-exclamation-sign"></i> <?php _e('Sorry, your search yielded no results.', 'bst'); ?>
    </div>
<?php endif; ?>

I am not sure how to parse by a second category...

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 class="alert alert-warning">
    <i class="glyphicon glyphicon-exclamation-sign"></i> <?php _e('Sorry, your search yielded no results.', 'bst'); ?>
  </div>

<?php
endif; ?>

Replace the original loop with the custom WP_Query loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM