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).

Share Improve this question edited Aug 12, 2021 at 7:25 Álvaro Franz asked Aug 11, 2021 at 18:06 Álvaro FranzÁlvaro Franz 1,0901 gold badge9 silver badges31 bronze badges 3
  • 1 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 uses get_the_title, or just use get_the_title. PHP also has runkit_function_redefine but that will require you to have PECL runkit and php version >= 7.0 – Buttered_Toast Commented Aug 12, 2021 at 6:39
  • @Buttered_Toast Thanks for this interesting information. – Álvaro Franz Commented Aug 12, 2021 at 7:44
  • 1 Themes don't necessarily always use the arguments for the_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
Add a comment  | 

1 Answer 1

Reset to default 1

You 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:

  1. Access to the header.php
  2. 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()