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 – Jim-miraidev Nov 8 '17 at 22:02
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, privacy policy and cookie policy

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