Skip to content

Instantly share code, notes, and snippets.

@celticwebdesign
Created March 22, 2016 21:35
Show Gist options
  • Save celticwebdesign/6673d88f298f50a48241 to your computer and use it in GitHub Desktop.
Save celticwebdesign/6673d88f298f50a48241 to your computer and use it in GitHub Desktop.
Get a post of parent category (of another post)
<?php
$category = get_the_category();
// echo $category[0]->category_parent;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'post__not_in' => array( get_the_ID() ),
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $category[0]->category_parent
)
)
);
// The query
$child_query = new WP_Query($args);
// The loop
if( $child_query->have_posts() ) : while ( $child_query->have_posts() ) : $child_query->the_post(); ?>
<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class('post-2'); ?>>
<?php
echo "<h2>".get_the_title()."</h2>";
echo "<div class='date'>Posted on"
." <span>".get_the_time('F j, Y')."</span>";
echo "</div>";
// if ( has_post_thumbnail()) :
// echo "<a href='".get_the_permalink()."' title='".get_the_title()."'>
// <div class='featured_image'>";
// the_post_thumbnail('large');
// echo "</div>
// </a>";
// endif;
html5wp_excerpt('html5wp_index');
?>
</article>
<!-- /article -->
<?php
endwhile; endif;
wp_reset_postdata();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment