admin管理员组文章数量:1122832
I want to display a custom HTML snippet after a page or post title.
Any theme which follows the WP guidelines will output the post or page title using the_title()
. For example, the TwentyTwentyOne WP theme does it as follows:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header alignwide">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
<?php twenty_twenty_one_post_thumbnail(); ?>
</header>
...
So, my first attempt has been to use the_title filter.
function filter_the_title( $title ) {
return $title . '<h2>Whatever</h2>';
}
add_filter( 'the_title', 'My\Namespace\filter_the_title' );
But this does filter the title text itself, not the whole output, so my custom HTML will be inside the <h1>
tag.
How may I add custom content after the_title()
output, without touching the theme files (should work as a plugin for any standard WP theme).
I want to display a custom HTML snippet after a page or post title.
Any theme which follows the WP guidelines will output the post or page title using the_title()
. For example, the TwentyTwentyOne WP theme does it as follows:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header alignwide">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
<?php twenty_twenty_one_post_thumbnail(); ?>
</header>
...
So, my first attempt has been to use the_title filter.
function filter_the_title( $title ) {
return $title . '<h2>Whatever</h2>';
}
add_filter( 'the_title', 'My\Namespace\filter_the_title' );
But this does filter the title text itself, not the whole output, so my custom HTML will be inside the <h1>
tag.
How may I add custom content after the_title()
output, without touching the theme files (should work as a plugin for any standard WP theme).
1 Answer
Reset to default 1You need to introduce your own hook (that's how I'd do it). I've added a custom hook in the following snippet:
<header class="entry-header alignwide">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
<?php do_action('custom_after_title_hook'); ?>
<?php twenty_twenty_one_post_thumbnail(); ?>
</header>
In order for this to work, you need to have:
- Access to the header.php
- OR overwrite it in a child-theme.
Last thing is easy: just call the function in your functions.php file like this:
<?php
function custom_after_title_hook(){
$some_var = '<span>some text after title</span>';
return $some_var;
}
add_action('my_custom_action', 'custom_after_title_hook');
?>
You can also add this to a plugin, but then you need to work with 'template_include'.
本文标签: WP hook to add custom content after thetitle()
版权声明:本文标题:WP hook to add custom content after the_title() 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736296383a1929780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
the_title
only has a filter for the content and not the complete structure, kind of annoying I agree. You could create your own function that usesget_the_title
, or just useget_the_title
. PHP also has runkit_function_redefine but that will require you to havePECL runkit
and php version >= 7.0 – Buttered_Toast Commented Aug 12, 2021 at 6:39the_title()
, so even if you could filter outside them you still wouldn't be outside the heading tags on many themes. – Jacob Peattie Commented Aug 12, 2021 at 7:52