0

I am trying to add a "category specific" graphic before the post title of that category. Each category would have a separate graphic. This is what I have so far, any ideas?

function new_title( $title ) {
    $new_title = '⚠️ ' . $title;
    return $new_title;
}
add_filter( 'the_title', 'new_title' );
  • The above adds the graphic to all titles
1
  • you could add a custom image field to the taxonomy, then for each post find the category image Commented Nov 8, 2017 at 22:02

1 Answer 1

0

Try the following code:

function new_title( $title ) {
    if(in_the_loop() && !is_singular('page')) {
        $picArray = array(
            '3' => '⚠',
            '5' => '⛅',
        );
        $cats = get_the_category();
        $cat = $cats[0]->term_id;
        $title = $picArray[$cat] . ' ' . $title;
    }
    return $title;
}
add_filter( 'the_title', 'new_title' );

First, we check if we are in the loop, and our post is not a page. Variable $picArray is an associative array of {category id} => {emoji's html identity} pairs. Of course, you have to build this array to match your values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.