• On my single.php page I want to have the tab of the article’s corresponding category highlighted.

    For example: https://embeddedcomputingsystems.com/

    If the article is from “Boards” (the second tab next to “Home”) then I’d like the Boards tab to be in the active state. I’m am guessing I’d use some sort of php if statement, like if category=3 then class=”active” but am unsure of how to write this. I know nothing about php!

    Can someone please help?

    Thanks,

    Jon

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter dbunit16

    (@dbunit16)

    I was able to find this code:

    <?php
    if (is_category(9)) {
       // looking for category 9 posts
       include(TEMPLATEPATH . '/single2.php');
    } else {
       // put this on every other category post
       include(TEMPLATEPATH . '/single1.php');
    }
    ?>

    Can I just keep adding if statements? for each category before the else?

    Thread Starter dbunit16

    (@dbunit16)

    In other words… is this valid:

    <?php
    if (is_category(1)) {
       // looking for category 1 posts
       include(TEMPLATEPATH . '/single1.php');
    if (is_category(9)) {
       // looking for category 2 posts
       include(TEMPLATEPATH . '/single2.php');
    if (is_category(9)) {
       // looking for category 3 posts
       include(TEMPLATEPATH . '/single3.php');
    if (is_category(9)) {
       // looking for category 4 posts
       include(TEMPLATEPATH . '/single4.php');
    } else {
       // put this on every other category post
       include(TEMPLATEPATH . '/single.php');
    }
    ?>

    Almost…
    if
    elseif
    elseif
    elseif
    else

    On my single.php page I want to have the tab of the article’s corresponding category highlighted.

    For posts, use in_category instead of is_category.

    Thread Starter dbunit16

    (@dbunit16)

    Great! I will try this! Where would I place this code?

    Thread Starter dbunit16

    (@dbunit16)

    I have tried the above code

    <?php
    if (is_category(11)) {
       // looking for category 11 posts
       include(TEMPLATEPATH . '/single11.php');
    elseif (is_category(9)) {
       // looking for category 2 posts
       include(TEMPLATEPATH . '/single2.php');
    elseif (is_category(9)) {
       // looking for category 3 posts
       include(TEMPLATEPATH . '/single3.php');
    elseif (is_category(9)) {
       // looking for category 4 posts
       include(TEMPLATEPATH . '/single4.php');
    } else {
       // put this on every other category post
       include(TEMPLATEPATH . '/singlegeneral.php');
    }
    ?>

    But am getting this error “Parse error: parse error, unexpected T_ELSEIF in /home/content/a/z/e/azelasko/html/wp-content/themes/embedded411/single.php on line 5”

    Thread Starter dbunit16

    (@dbunit16)

    brackets weren’t closed! Thank you for your help!

    iridiaz, that in_category was key! Thank you!

    That should be work fine

    <?php
    $post = $wp_query->post;
    if ( in_category('5') ) {
    include(TEMPLATEPATH . '/single2.php');
    }elseif ( in_category('3') ) {
    include(TEMPLATEPATH . '/single1.php');
    } else {
    include(TEMPLATEPATH . '/single.php');
    }
    ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘single.php and Categories’ is closed to new replies.