I want to add an icon in title posts only in posts with specific tag.
Is this possible?
I've already tried these codes, but they didn't solve my problem:
Approach #1
if (is_tag('162')) {
function new_title( $title ) {
$new_title = 'icon-url' . $title;
return $new_title;
}
add_filter( 'the_title', 'new_title' );
}
Approach #2
add_filter( 'the_title', 'modify_post_title' );
function modify_post_title( $title ) {
if ( is_tag('162') && $title == 'Existing Title' )
$title = '<span>Existing</span> Title';
return $title;
}
I want to add an icon in title posts only in posts with specific tag.
Is this possible?
I've already tried these codes, but they didn't solve my problem:
Approach #1
if (is_tag('162')) {
function new_title( $title ) {
$new_title = 'icon-url' . $title;
return $new_title;
}
add_filter( 'the_title', 'new_title' );
}
Approach #2
add_filter( 'the_title', 'modify_post_title' );
function modify_post_title( $title ) {
if ( is_tag('162') && $title == 'Existing Title' )
$title = '<span>Existing</span> Title';
return $title;
}
The problem with your first approach is that you check conditional tags too early. is_tag
will work only after the $wp_query
is computed, so there is no point in checking it directly in functions.php
file.
And there is one more problem with both approaches - you use wrong conditional tag...
is_tag
checks if a Tag archive page is being displayed... So it's not what you're looking for.
And here's a solution:
function add_something_to_post_title( $title ) {
if ( has_tag( 162 ) ) {
$title = 'something ' . $title;
}
return $title;
}
add_filter( 'the_title', 'add_something_to_post_title' );