• Hey,
    I’m sure this is easy, but I need to basically insert some custom text above an entry if I post to the “something” category.
    My current code is:
    <div id="content">
    <?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
    <?php the_date('','<span class="style1">','</span>

    '); ?>
    I need to put a ‘<div id=”diddle”>’ below ‘<div id=”content”>’ IF I post an entry to the “something” category…
    As I said, I’m sure this is simple…

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter jeremywright

    (@jeremywright)

    Huh, doesn’t seem to be doing it…
    Code:
    <?php if(in_category('Sponsored Posts') == true) { ?>
    hello there
    <?php } ?>

    Example URL: https://www.ensight.org/archives/2003/08/20/test-ad-post
    Am I doing something wrong?

    You aren’t doing it wrong – the “in_category” function isn’t a template friendly function it seems. It checks for category based on category ID, instead of name. I rewrote it to check via either attribute, if you would like.
    Open up wp-includes/template-functions-category.php and goto the end, you will see the
    function in_category($categor) {
    Replace that whole function with:
    function in_category($category, $by='id') { // Check if the current post is in the given category
    // updated by Jeff Minard to include a keyword search ability,
    // cause nobody is going to check via category number - duh.
    global $post, $category_cache;
    $cats = '';
    foreach ($category_cache[$post->ID] as $cat) :
    if($by == 'id') {
    $cats[] = $cat->category_id;
    } else if ($by == 'name') {
    $cats[] = $cat->cat_name;
    }
    endforeach;
    if ( in_array($category, $cats) )
    return true;
    else
    return false;
    }

    THEN this should work:
    <?php if( in_category(‘General’, ‘name’) ) { ?>
    it is in the General Category
    <?php } ?>
    A few things to note:

    • This function is backwards compatible, so no worries there.
    • Category names are case sensitive, so be careful to quote it correctly.

    Well, ok, by a few I meant two – but still. Good luck!

    Thread Starter jeremywright

    (@jeremywright)

    Awesome, that worked perfectly, thanks!!!

    No problem, this is something that should be submitted as a bug or something like that. Hrm…

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Category Customizations’ is closed to new replies.