WordPress.org

Forums

single.php and Categories (9 posts)

  1. dbUNIT16
    Member
    Posted 6 years ago #

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

    For example: http://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

  2. dbUNIT16
    Member
    Posted 6 years ago #

    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?

  3. dbUNIT16
    Member
    Posted 6 years ago #

    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');
    }
    ?>
  4. moshu
    Member
    Posted 6 years ago #

    Almost...
    if
    elseif
    elseif
    elseif
    else

  5. iridiax
    Member
    Posted 6 years ago #

    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.

  6. dbUNIT16
    Member
    Posted 6 years ago #

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

  7. dbUNIT16
    Member
    Posted 6 years ago #

    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"

  8. dbUNIT16
    Member
    Posted 6 years ago #

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

    iridiaz, that in_category was key! Thank you!

  9. hamidelgendy
    Member
    Posted 6 years ago #

    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');
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic