admin管理员组文章数量:1130349
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;
}
Share
Improve this question
edited Nov 18, 2018 at 20:11
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Nov 18, 2018 at 19:11
En Código WPEn Código WP
213 bronze badges
5
- And what have you tried already? – Krzysiek Dróżdż Commented Nov 18, 2018 at 19:14
- if(is_tag('162')){ function new_title( $title ) { $new_title = 'icon-url' . $title; return $new_title; } add_filter( 'the_title', 'new_title' ); } – En Código WP Commented Nov 18, 2018 at 19:16
- 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;} – En Código WP Commented Nov 18, 2018 at 19:17
- OK, and where exactly should that icon be visible? On single post only? On all archive pages? – Krzysiek Dróżdż Commented Nov 18, 2018 at 19:42
- In single and archive pages. I have some different posts I want mark in these titles – En Código WP Commented Nov 18, 2018 at 20:07
1 Answer
Reset to default 0The 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' );
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;
}
Share
Improve this question
edited Nov 18, 2018 at 20:11
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Nov 18, 2018 at 19:11
En Código WPEn Código WP
213 bronze badges
5
- And what have you tried already? – Krzysiek Dróżdż Commented Nov 18, 2018 at 19:14
- if(is_tag('162')){ function new_title( $title ) { $new_title = 'icon-url' . $title; return $new_title; } add_filter( 'the_title', 'new_title' ); } – En Código WP Commented Nov 18, 2018 at 19:16
- 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;} – En Código WP Commented Nov 18, 2018 at 19:17
- OK, and where exactly should that icon be visible? On single post only? On all archive pages? – Krzysiek Dróżdż Commented Nov 18, 2018 at 19:42
- In single and archive pages. I have some different posts I want mark in these titles – En Código WP Commented Nov 18, 2018 at 20:07
1 Answer
Reset to default 0The 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' );
本文标签: functionsIs possible add icon in title posts only in specific tag
版权声明:本文标题:functions - Is possible add icon in title posts only in specific tag? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749171151a2327026.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论