Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 1 character in body
Source Link
Álvaro Franz
  • 1.1k
  • 1
  • 9
  • 31

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.

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

Source Link
Álvaro Franz
  • 1.1k
  • 1
  • 9
  • 31

WP hook to add custom content after the_title()

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.