WordPress.org

Forums

How to select a category AND all his child (14 posts)

  1. Carlo
    Member
    Posted 4 years ago #

    I'm styling the loop of my blog, giving to all categories and relative child a different default color for links and image border.

    To select a category i use ( in_category('5'), but the loop returns me only the parent category's post, hiding me all his child category's post.

    How can i select also the child category inside the command ( in_category('5') ?

    Thanks

  2. caugb
    Member
    Posted 4 years ago #

    $subcat = get_categories('child_of=5');
    $ids = array('5');
    foreach($subcat as $c) $ids[] = (string)$c->term_id;
    [...]
    if(in_category($ids)) ...
  3. Carlo
    Member
    Posted 4 years ago #

    sorry, i'm not a "php hero". how can i integrate your code inside this?

    <?php if ( in_category('47') ) { ?>

  4. caugb
    Member
    Posted 4 years ago #

    [please keep marking your code using the code button or backticks]

    replace
    <?php if ( in_category('47') ) { ?>
    by
    <?php
    $subcat = get_categories('child_of=47');
    $ids = array('47');
    foreach($subcat as $c) $ids[] = (string)$c->term_id;
    if ( in_category($ids) ) { ?>

    >> it should work..

  5. Carlo
    Member
    Posted 4 years ago #

    Yup! Great, it works! You're definitely a Php hero!

    Just one thing. I used this inside a if...elseif...else cycle, with 4 cases, example:

    if
    <content>
    elseif
    <content>
    elseif
    <content>
    else
    <content>

    If a use your code for the first if everything is fine. If i use it in the second elseif, obviously replacing if with elseif, i receive this error:

    Parse error: syntax error, unexpected T_ELSEIF in /home/content/74/5764274/html/wp-content/themes/collective/includes/loop.php on line 79

    What happened?

  6. Carlo
    Member
    Posted 4 years ago #

    code i've used for the elseif:

    <?php }
    $subcat = get_categories('child_of=4');
    $ids = array('4');
    foreach($subcat as $c) $ids[] = (string)$c->term_id;
    elseif ( in_category($ids) ) { ?>
  7. Carlo
    Member
    Posted 4 years ago #

    I used this inside a if...elseif...else cycle, with 4 cases, example:

    if
    <content>
    elseif
    <content>
    elseif
    <content>
    else
    <content>

    If a use your code for the first if everything is fine. If i use it in the second elseif, obviously replacing if with elseif, i receive this error:

    Parse error: syntax error, unexpected T_ELSEIF in /home/content/74/5764274/html/wp-content/themes/collective/includes/loop.php on line 79

    What happened?

    code i've used for the elseif:

    <?php }
    $subcat = get_categories('child_of=4');
    $ids = array('4');
    foreach($subcat as $c) $ids[] = (string)$c->term_id;
    elseif ( in_category($ids) ) { ?>
  8. Carlo
    Member
    Posted 4 years ago #

    anyone?

  9. alchymyth
    Forum Moderator
    Posted 4 years ago #

    you cant just add an 'elseif' statement into the middle of a 'foreach' loop.

    your code snippet is too short to figure out what you have intended;

    can you paste the code of the full file into a http://pastebin.com/ and post the link to it here?

  10. Carlo
    Member
    Posted 4 years ago #

    Thanks so much.
    Here's the code http://pastebin.com/24rJrzyh

  11. alchymyth
    Forum Moderator
    Posted 4 years ago #

    my mistake - (forget my comment about the 'foreach' - that code is ok)

    this is more complex, as you need to create the list of sub categories for each of your 'elseif' statements; and you probably need to do this before the first 'if'.

    i'll work something out - however, it will take a while ;-)

  12. alchymyth
    Forum Moderator
    Posted 4 years ago #

    http://pastebin.com/ZparuzC4

    basically, i used this code

    $subcat = get_categories('child_of=4');
    $ids = array('4');
    foreach($subcat as $c) $ids[] = (string)$c->term_id;

    four times with the different cat ids, and instead of $ids i used $ids4 (and respectively with the other numbers)
    and also changed from ( in_category($ids) ) in the 'elseif' statements to ( in_category($ids4) ) etc

  13. Carlo
    Member
    Posted 4 years ago #

    First of all, thank you very much for your effort and for you valuable time! If we will ever meet i definitely owe you a beer!
    The code is superb and it works nicely.

    I just have one small issue, if i may.

    Take a look at how i've implemented it: http://pastebin.com/pfAcaFJc

    I replicate your loop two times, to put an adv banner between the first four posts and the other four.

    Now, instead of showing 4 posts / adv banner / 4 posts, it shows 3 posts / adv banner / 3 posts.

    There is also a way to display the 4 posts / adv banner / 4 posts scheme without having to replicate your code 2 time?

    Many thanks again

  14. alchymyth
    Forum Moderator
    Posted 4 years ago #

    Now, instead of showing 4 posts / adv banner / 4 posts, it shows 3 posts / adv banner / 3 posts.

    i am not familiar with this style of ending the loop;
    either the $count does have no effect, then check if the number of posts per page in 'settings' 'reading' is set to 3 - cange to 4;
    or the $count does effect the loop, then try to change the ($count < '4') to ($count <= '4').

    this is a attempt to pack all into one block:
    http://pastebin.com/icGcA8DB
    (untested, therefore make a backup copy of your working code before trying to use this)

Topic Closed

This topic has been closed to new replies.

About this Topic