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

    (@cccamuseme)

    I figured this out by adding the following code to my functions.php

    add_action( 'the_title', 'title_color', 10, 2 );
    function title_color( $title, $post_id ) 
    {
        if( has_category( 4, $post_id ) ) {
            $title = '<span class="red">' . $title . '</span>';
        }
    
        return $title;
    }
    Thread Starter Chris Cook

    (@cccamuseme)

    Thanks

    Basically what I am trying to achieve is to have the title changed of posts which are in category number 30.

    My code is:

    function adddd($title) {

    if(has_category(’30’,$post->ID)){
    $title = ‘Prefix ‘.$title;
    }

    return $title;
    }
    add_action(‘the_title’,’adddd’);
    The code works, but has one issue. When I am inside the post which has that category, title is being changed for all other pages (which are called through the_title) too.

    How can I change the title only to post titles which have that category, no matter what page I am on?

    =====================
    channel partner india, channel partner opportunity

    Thread Starter Chris Cook

    (@cccamuseme)

    Try this

    
    function adddd($title) {
    
    if(has_category(30,$post->ID)){
    $title = ‘Prefix ‘.$title;
    }
    
    return $title;
    }
    add_action(‘the_title’,’adddd’);
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change post title based on category’ is closed to new replies.