Site logo

Archived topic

Help with custom meta after title post

11 replies · Started by fellows2020 on March 28, 2020

Viewing posts 1–12 of 12

Hello,

I would like to display meta data directly below single post titles in the format of "by [author] | [month, day, year] | This post may contain affiliate links. Learn more. [where Learn more is linked to my disclosures page.]

It appears there is a way to do this via hooks (after_entry_title), but I'm just having trouble figuring it out. Could you walk me through the steps?

Thanks very much.

Hi there,

Add this PHP snippet first:

add_filter( 'generate_header_entry_meta_items', function() {
    return array(
        'author',
        'date'
    );
} );
add_filter( 'generate_inside_post_meta_item_output', function( $output, $item ) {
	if ( 'date' === $item ) {
        return ' | ';
    }
    return $output;
}, 50, 2 );

Then create a hook element with this as your content:
<div class="affiliate">affliate content here</div>

Choose the after_entry_title hook then display to Post > All Posts.

Let me know when this is done and I can provide some CSS :)

This is great. Thanks for your help so far. Almost there. Single posts now display the following:

by [author] | [date]
[affiliate content.]

Could you please let me know:
1. How to put the [affiliate content] on the same line as the "by [author] | [date]
2. How to add a divider (|) after the date and before the [affiliate content]
3. How to make the font size for the [affiliate content] the same as the author and date meta data. (Right now, the affiliate content metadata font size is larger.)

Thank you!

Add this CSS:

.entry-meta, .affiliate {
    display: inline-block;
}
.affiliate {
    font-size: 85%;
}
.affiliate:before {
    content: " | ";
}

Hi, One last thing: On the homepage (which shows a list of my most recent posts), the only meta data I am displaying is the date. With the changes you suggested above, there is now a " | " preceding the date. Could you let me know how to remove that from the display on the homepage, while keeping the format on single posts? (I edited the link in my initial ticket to send you to the homepage.) Otherwise, it looks great. Thanks for all of your help.

Hi there,

in this code Leo provided:

add_filter( 'generate_inside_post_meta_item_output', function( $output, $item ) {
	if ( 'date' === $item ) {
        return ' | ';
    }
    return $output;
}, 50, 2 );

Change if ( 'date' === $item ) { to if ( is_single() && 'date' === $item ) {

Hi David, I changed the code, but it didn't do anything. (I cleared my cache, but still nothing.)

try changing it to:

if ( is_single() && 'date' === $item ) {

Hi David,

That's the same code you suggested the first time around. When I tried putting into my functions file in place of if ( 'date' === $item ) { , it did not make any difference. Is there something else I can try? Thank you.

Its not the same - i changed it in both places above. Give it a try and let me know.

It worked. thank you!

Awesome :)

This archived topic is closed to new replies.