Forums

[resolved] Pull content from two categories in one category template? (10 posts)

  1. databell96
    Member
    Posted 10 months ago #

    I know that in order to pull in content from one category, you typically create a copy of the category.php file and reflect either the slug or the category number. Like category-4.php, for example. But I'm encountering a situation where I have a client who wants to have posts come in two columns with let's say the left being content from category 1 and the right, category 2. How can I pull in content from two categories into one category template? Or would I use something completely different?

  2. alchymyth
    The Sweeper
    Posted 10 months ago #

    how is the client to select which two categories are to go into one template?

    is that always the same combination?

    how is he going to link to that template?

  3. databell96
    Member
    Posted 10 months ago #

    It is always going to be the same two categories. As for linking, I would assume the typical category permalink structure. Unless you got a better idea.

  4. alchymyth
    The Sweeper
    Posted 10 months ago #

    you could create two category templates;
    each one with two loops; differently added custom query:

    rough sketch only:

    category-1.php

    /*category1 loop*/
    normal loop
    /*category2 loop*/
    some code to save the original query;
    $args = array('category__in' => array($cat2_id),
    'paged' => get_query_var('paged')
    );
    $loop2 = new WP_Query($args);
    if( $loop2->have_posts() .........etc
    some code to restore the original query;

    category-2.php

    /*category1 loop*/
    some code to save the original query;
    $args = array('category__in' => array($cat1_id),
    'paged' => get_query_var('paged')
    );
    $loop1 = new WP_Query($args);
    if( $loop1->have_posts() .........etc
    some code to restore the original query;
    /*category2 loop*/
    normal loop

    so whenever someone clicks a cat1-permalink, it will use category-1.php and show two loops;
    anytime someone clicks a cat2-permalink, it will use category-2.php and show two loops (which look the same as the other two loops)

    the result should be more-or-less the same apart from pagination, if the cats have a different amount of posts.

  5. databell96
    Member
    Posted 10 months ago #

    Well, the two categories only need to show up on the category-1.php template only. Know you weren't aware of that, so I'm filling in the details now.

    Secondly, I pasted in your code, and then added the code for the loop. Must have done something incorrect because it's got a PHP error in the middle of the second loop. So I took the code and pasted it into PasteBin. The link is as follows:

    http://pastebin.com/Lu5np43z

    Just so you know the other category ID is 42 so I thought I should change $cat2_id to $cat42_id.

  6. alchymyth
    The Sweeper
    Posted 10 months ago #

    as the line 'rough sketch only:' indicated, this was not meant for copy/paste - you need to turn this into 'real' code first.

    so I thought I should change $cat2_id to $cat42_id.

    not really, probably more, add a line: $cat42_id = 42;

    here is a corrected version; untested.

  7. databell96
    Member
    Posted 10 months ago #

    Where's the corrected version? I don't see it.

  8. databell96
    Member
    Posted 10 months ago #

    OK, I just tried your method but it's posting the same content from category 1 in both of my div's. Again, this is not using the corrected version, rather attempting some corrections of my own.

  9. alchymyth
    The Sweeper
    Posted 10 months ago #

    Where's the corrected version? I don't see it

    if forgot to paste the link to the pastebin; here it is:

    http://pastebin.com/PNGP93ew

  10. databell96
    Member
    Posted 10 months ago #

    Yep, that's it. Thanks so much for sharing that. It's so difficult to find information on how to accomplish that feat.

Reply

You must log in to post.

About this Topic