I have a custom post type named city guide . The child post articles are displayed within the parent page.Child posts has custom category named as newyork and paris.
<div class="container">
<?php while( have_posts() ): the_post(); ?>
<div class="row main-content-wrap">
<div class="col-md-12 long-post-sections-wrapper">
<?php while( $city_guide->have_posts() ): $city_guide->the_post();
$terms = get_the_terms( $post->ID , 'city-guide-cities-category' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
print $term->name ;
} }
?>
<div <?php post_class('city-guide-row row' . classes_related($wp_query->current_post) ); ?>>
<div class="col-sm-8">
<h3 id="<?php echo esc_attr( get_post()->post_name ); ?>"><?php the_title(); ?></h3>
<?php the_post_thumbnail(); ?>
<div class="img-caption"><?php echo get_post(get_post_thumbnail_id())->post_excerpt; ?></div>
<?php the_content(); ?>
</div>
<div class="col-sm-4 map-container">
<!-- google map -->
<div class="map-wrapper">
<div class="mobile-map-overlay" onClick="style.pointerEvents='none'"></div>
<iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0"width="100%" height="260" src="https://maps.google.com/maps?hl=en&q=<?php echo get_field('address',get_the_ID()); ?>&ie=UTF8&t=roadmap&z=6&iwloc=B&output=embed&z=16"></iframe>
</div>
<h5>Address</h5>
<?php echo get_field('address'); ?><br/>
<?php echo get_field('address_2'); ?>
<h5>Contact</h5>
<?php if(get_field('contact_number')):
echo '<a href="tel:' . get_field('contact_number') . '">' . get_field('contact_number') . '</a>';
endif; ?><br/>
<?php if(get_field('website')):
echo '<a href="' . get_field('website') . '" target="_blank">' . get_field('website') . '</a>';
endif; ?><br/>
<?php if(get_field('email')):
echo '<a href="mailto:' . get_field('email') . '">' . get_field('email') . '</a>';
endif; ?>
</div>
<div class="clearfix"></div>
<hr class="city-row-separator"/>
<?php endwhile; ?>
</div>
</div>
<?php endwhile; ?>
I have printed print $term->name ; inside while so each child article is showing its own categories.
What i want to achieve is display newyork posts first and then paris.
if ( $term->name == 'newyork'){
}
elseif ( $term->name == 'paris'){
}
did not result what i expected.how can I achieve ?Please help
Edit:
tried this returnd nothing.
<?php
foreach ($NY_array as $key => $value) {
$args = array(
'post__in' => $NY_Posts,
);
$posts = get_posts($args);
foreach ($posts as $p) : ?>
<div <?php post_class('city-guide-row row' . classes_related($wp_query->current_post) ); ?>>
<div class="col-sm-8">
<h3 id="<?php echo esc_attr( get_post()->post_name ); ?>"><?php $p->post_title; ?></h3>
<?php the_post_thumbnail(); ?>
<div class="img-caption"><?php echo get_post(get_post_thumbnail_id())->post_excerpt; ?></div>
<?php the_content(); ?>
</div>
<div class="col-sm-4 map-container">
<!-- google map -->
<div class="map-wrapper">
<div class="mobile-map-overlay" onClick="style.pointerEvents='none'"></div>
<iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0"width="100%" height="260" src="https://maps.google.com/maps?hl=en&q=<?php echo get_field('address',get_the_ID()); ?>&ie=UTF8&t=roadmap&z=6&iwloc=B&output=embed&z=16"></iframe>
</div>
<h5>Address</h5>
<?php echo get_field('address'); ?><br/>
<?php echo get_field('address_2'); ?>
<h5>Contact</h5>
<?php if(get_field('contact_number')):
echo '<a href="tel:' . get_field('contact_number') . '">' . get_field('contact_number') . '</a>';
endif; ?><br/>
<?php if(get_field('website')):
echo '<a href="' . get_field('website') . '" target="_blank">' . get_field('website') . '</a>';
endif; ?><br/>
<?php if(get_field('email')):
echo '<a href="mailto:' . get_field('email') . '">' . get_field('email') . '</a>';
endif; ?>
</div>
<div class="clearfix"></div>
<?php get_template_part( 'templates/shop-the-story-city-guide', 'single' ); ?>
</div>
<hr class="city-row-separator"/>
<?php
endforeach;
}
?>
Let me guide you through simple code as per your current code
Run the loop and collect the posts in NewYork and Paris in separate array as
$NY_array
and$PA_array
and their Posts Ids also in separate arrays
$NY_Posts
and$PA_Posts
Now run the 2 separate loops first for Newyork and then for Paris
Similarly for the PA_array