• I’m using Conditional Tags on my single.php to use Category Templates for my single post view.

    Right now, I’m doing it with this code:

    <?php
    $post = $wp_query->post;
    if (in_category(15)) {
       include(TEMPLATEPATH . '/category-3_sub.php');
    } else {
       include(TEMPLATEPATH . '/index.php');
    }
    ?>

    There’s just one problem. The Conditional Tag is just asking for the child category, and I would like to ask for the parent category.

    I’m a total php newbie, so would be nice if somebody could help me out. Thanks a lot.

    Karina

Viewing 7 replies - 1 through 7 (of 7 total)
  • What about:

    if (in_category( array( 15,20,233 ) ) {

    Thread Starter esferapublica

    (@esferapublica)

    Hey MichaelH,

    I’m not sure if I got that thing with the array right. 15,20,233 are the parent categories? If so: I tried to write: (3 is the parent)

    if (in_category( array( 3 ) ) {

    but it didn’t work.

    The problem is, I have about 10 dynamic subcategory ID’s that are going to change constantly. That’s why I just can define the parent category as a fix parameter. The rest I practically don’t know.

    Sorry, as I said, my knowledge in PHP is very basic. : /

    thanks, Karina

    Not sure what you are trying to do so will offer this:

    <?php
    // display category id of the parent of category 15
    $include = '15';
    $taxonomy = 'category';
    $term_args = array(
      'include' => $include,
      'hide_empty' => false,
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $terms = get_terms( $taxonomy, $term_args );
    echo 'parent is '. $terms[0]->parent;
    ?>

    Thread Starter esferapublica

    (@esferapublica)

    sorry, this still doesn’t work.

    I try to explain it better and with online examples:

    I have a list of posts on a category-ID.php page. In this case the list of posts is ?Intro? and ?Aufgabenstellung? on the left column of the content box.
    ?cat=11

    When I click on one post it obviously opens in the index.php template.
    ?p=44

    I want it to open in the category-ID.php of the parent category, that it looks like this again.
    ?cat=11

    So what I need/tried:

    I made a single.php and added Conditional Tags which asked the following:

    If this post is in parent category X, include this template, elseif this post is in parent category Y, include that template.

    In the conditional tags I just found the term:
    if (in_category(4) ) {
    which doesn’t work for me, because it just asks for the category of the post, and not the parent category. I can’t use this, because I’m constantly adding a lot of subcategories with different IDs, and can’t always add them in the code. The only thing that is fix, is the parent category. Thats why the thing you posted doesn’t work, because I again would have to add the ID of the subcategory in my code, which I not always know.

    Phuu, I hope i suceeded to kind of make this clear. Sorry, if this is a little bit confusing. But I hope somebody could help.

    Thanks a lot, Karina

    I don’t believe you can do that, but maybe someone else has an idea.

    One last idea–you could configure your single.php to look different based on that post’s categories.

    Thread Starter esferapublica

    (@esferapublica)

    that’s confusing. if there is a possiblity to read out the subcategory ID, there has to be some way to find out the parentcategory ID.

    anbody an idea how to script this???

    PS: it doesn’t work just to edit the single.php, because i need several different templates, and not just one.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Conditional tag asking for parent category’ is closed to new replies.