• Hi!

    I am trying to add a span to blog post titles, based upon their category on the website and I don’t want it to show up in the home page in the widgets section at the bottom. Here is my code:

    add_filter( ‘the_title’, ‘my_modify_title’, 10, 2 );
    function my_modify_title( $title, $id ) {
    if( in_category( ‘Fitness’, $id ) ) {
    $title = ‘<span class=”fitness”></span> ‘ . $title;
    } else if( in_category( ‘Recipe’, $id ) ) {
    $title = ‘<span class=”recipe”></span> ‘ . $title;
    } else if( in_category( ‘Life etc’, $id ) ) {
    $title = ‘<span class=”life”></span> ‘ . $title;
    } else if( in_category( ‘Soul’, $id ) ) {
    $title = ‘<span class=”soul”></span> ‘ . $title;
    }
    return $title;
    }

    I tried adding:

    !is_front_page() to the if and it isn’t working.

    add_filter( ‘the_title’, ‘my_modify_title’, 10, 2 );
    function my_modify_title( $title, $id ) {
    if( !is_front_page() && in_category( ‘Fitness’, $id ) ) {
    $title = ‘<span class=”fitness”></span> ‘ . $title;
    } else if( !is_front_page() && in_category( ‘Recipe’, $id ) ) {
    $title = ‘<span class=”recipe”></span> ‘ . $title;
    } else if( !is_front_page() && in_category( ‘Life etc’, $id ) ) {
    $title = ‘<span class=”life”></span> ‘ . $title;
    } else if( !is_front_page() && in_category( ‘Soul’, $id ) ) {
    $title = ‘<span class=”soul”></span> ‘ . $title;
    }
    return $title;
    }

    Please let me know if I’m doing something wrong here…

    The website is currently at: http://utopiandesigns.ca/barlinfitness/

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The problem is widgets that output post titles run their own queries, and is_front_page() is a wrapper for a WP_Query method. Thus is_front_page() is telling you the current query (the widget’s) is not a front page query, even though the widget is on the front page.

    That explains it, but doesn’t offer a solution. If you’re working on a theme, you could set a global flag reflecting the front page status first thing in a template while the main query is still valid, then use that global flag in your conditionals instead. Yeah, I know globals are a bad practice. There are ways around them of course, if globals bother you, you probably know of a few.

    If you’re working on a plugin, the same concept can be applied, except you need to work entirely from available hooks, you can’t conveniently place code in your theme templates.

Viewing 1 replies (of 1 total)

The topic ‘Function !is_front_page not working’ is closed to new replies.