[resolved] Getting category posts from different parents on a category page (2 posts)

  1. Joshuwar
    Posted 6 years ago #

    Hi, I'm trying to get wordpress to display posts from seperate parent categories on a category page.

    For example, the main category could be 'John' (a child category of 'artists', for example), and on this category page, I want seperate lists of posts associated with his name, like 'News About John' (posts in both 'John' category and in 'News' category) or 'Pictures by John' (posts in both 'John' and 'Pictures') or whatever.

    I've been fiddling with this intention for ages, and I've arrived at code which works some of the time. This is pretty confusing for me (I'm still learning php). Here's what I've got...

    $pictures = 11;
    $news = 6;
    $biography = 7;
    $category = get_the_category(); //get all categories for this post
    echo 'first category is ' . $category[0]->cat_ID;
    <?php query_posts(array('category__and' => array($category[0]->cat_ID,$biography))); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2 class="medtitle"><?php the_title(); ?></h2>
    <?php the_content(); ?>
    <?php endwhile; endif; ?>
    <h3>News About This Person:</h3>
    <?php query_posts(array('category__and' => array($category[0]->cat_ID,$news))); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?><br />
    <?php endwhile; endif; ?>
    <h3>Pictures by this Person:</h3>
    <?php query_posts(array('category__and' => array($category[0]->cat_ID,$pictures))); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    <?php the_excerpt() ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Read More</a>
    <?php endwhile; endif; ?>

    What seems to happen is that sometimes the 'first category' thing seems to misfire and think that it's on another category (ie. the parent of the category which the page is). I'm totally at a loss with it, so any assistance with the above, or new directions, or anything at all, would be massively appreciated.

    Also if you think you can help but I've been a bit inarticulate, I will try to explain better.



  2. MichaelH
    Posted 6 years ago #

    Consider using get_posts or new WP_query instead of query_posts.

    Here's one example:

Topic Closed

This topic has been closed to new replies.

About this Topic