• Resolved bridgeburner

    (@bridgeburner)


    Is there a way I can include posts on a page that have two specific categories attached to them. So they only show up if they have those two categories attached and don’t show if they have just one of the two?

    For example: I’m creating a site with pages for multiple artists. Each artist can post their albums under their own category (Artist1) and in the category Albums. So I want the posts to show of their own albums on their own pages and not on the other artists pages.

    Is their a way to solve this without having to exclude the other categories? I’m now using this query_posts tag:

    <?php query_posts('category_name=Artist1&category_name=Albums'); ?>

    But this calls up posts containing one of the two categories.

    Thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • Try:

    <?php query_posts('cat=1,2'); ?>

    Where 1 and 2 are the numeric category IDs for Artist1 and Albums.

    I cannot get into the allablog site at accessromance.com and got directed to this site to check it out. Tried my computer at home and at library so have figure the problem is at access’ end???

    thanks.
    Pat

    Thread Starter bridgeburner

    (@bridgeburner)

    I’ve tried this and it includes the posts that have category 1 OR 2. And I only want the posts that have both 1 AND 2. I’ve also tried this:

    <?php query_posts('cat=1+2'); ?>

    But that also hasn’t the desired effect. Sorry if I wasn’t clear enough.

    patoct, please start your own forum thread about your problem.

    bridgeburner, sorry I misunderstood your request. If we’re to avoid a custom SQL query for this we’ll have to assign a query_posts() of each category to separate array variables, merge those arrays, then use in_category() to test on which posts to display:

    <?php
    $artist1 = query_posts('cat=1');
    $albums = query_posts('cat=2');
    $theposts = array_merge($artist1, $albums);

    foreach($theposts as $post) :
    if(in_category(1) && in_category(2)) :
    setup_postdata($post);
    ?>

    ~ Post content is displayed here ~

    <?php endif; endforeach; ?>

    All this would replace The Loop within your template.

    Thread Starter bridgeburner

    (@bridgeburner)

    Tested and it works, thanks you very very much Kafkaesqui. You’re my personal hero!

    Thread Starter bridgeburner

    (@bridgeburner)

    Looks like I might have cheered a bit too early, because this method makes the posts show up double. Probably once for category 1 and once for category 2. Is there a way to prevent this? Thanks anyway for the help!

    Yeah, I should have noticed that. Change the first code block (above ~ Post content…) to this:

    <?php
    $artist1 = query_posts('cat=1');
    $albums = query_posts('cat=2');
    $queried_posts = array_merge($artist1, $albums);

    $post_ids[] = array();
    foreach($queried_posts as $post) {
    if(!in_array($post->ID, $post_ids)) {
    $post_ids[] = $post->ID;
    $theposts[] = $post;
    }
    }

    foreach($theposts as $post) :
    if(in_category(1) && in_category(2)) :
    setup_postdata($post);
    ?>

    Thread Starter bridgeburner

    (@bridgeburner)

    Once again, you’re my hero. Works perfect now. Keep on rocking in the free world!

    Is there a possible variation on this solution that might help me with something I’d like to do?

    I have a blog that doesn’t have new posts most days, and so I have a static page as the main page for the site (i.e., no loop). The posts themselves are stories (it’s a family-story site); I use categories like “storyteller” and “character” (and have a subcat for each person who’s a storyteller or a character).

    I’d like to modify my current static front page. One thought is to show an excerpt from the most recent post (or most recent X posts).

    I already have a latest-post plugin in the sidebar, but it’d be good to have something like a mini-loop on the static front page.

    Any suggestions?

    The site: https://www.cousinagamfhein.net

    Kafkaesqui,

    I am looking for something like these codes you provided. Now I am think how do I:
    1. Let the use select 2 categories on the sidebar;
    2. Pass the selection to these codes in index.php?

    Please enlighten me. Thanks

    If I add something like

    $artist1 = query_posts(‘cat=1’);
    $albums = query_posts(‘cat=2’);
    $numbertoshow = query_posts (‘showposts=10’);
    $queried_posts = array_merge($artist1, $albums, $numbertoshow);

    Does it means that it will show-me the last 10 posts that are on both categories?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Include posts with two specific categories on a page’ is closed to new replies.