Archived topic
Displaying date & author below featured image in pages
16 replies · Started by Alessio on August 13, 2020
Hey,
I'm currently using this PHP code to make the featured image clickable (using WP Featherlight) and to add the date and author:
add_filter( 'generate_single_featured_image_output', function( $output, $image_html ) {
printf(
'<div class="featured-image">
<a href="%1$s">
%2$s
</a>
</div>
<div class="sdate">
%3$s
</div>
',
get_the_post_thumbnail_url(),
$image_html,
generate_posted_on(),
);
}, 10, 2 );
However, the date & name is shown above the featured image:

I want it be shown below the featured image, though.
Also, can anyone explain what the }, 10, 2 ); in the PHP-code does?
Thank you!!
Hi there,
Any chance you can link us to the site in question?
You can edit the original topic and use the private URL field.
Let me know :)
Hi,
I've edited it. Both password and username is 111111
Hi there,
try this function to move the date:
add_action( 'wp', function() {
if ( is_single() ) {
remove_action( 'generate_after_entry_title', 'generate_post_meta' );
add_action( 'generate_before_content', 'generate_post_meta', 15 );
}
} );
the 10 , 2 );
10 = the priority in which function callbacks are executed when attached to a hook. There could be more than one function attached. The one with the lower number gets executed first.
2 = is the number of accepted arguments the add_filter requires. In this case 2 which are $output & $image_html.
Hi there,
the function sadly doesn't have any effect at all. Maybe because it's a page and not a post?
aah... how are you displaying the date and author on the single page?
With my php code:
add_filter( 'generate_single_featured_image_output', function( $output, $image_html ) {
printf(
'<div class="featured-image">
<a href="%1$s">
%2$s
</a>
</div>
<div class="sdate">
%3$s
</div>
',
get_the_post_thumbnail_url(),
$image_html,
generate_posted_on(),
);
}, 10, 2 );
I've added the generate_posted_on() at the bottom. And tried to insert it like this:
<div class="sdate">
%3$s
</div>
Hi there,
generate_posted_on() won't work in a filter like that.
Instead, you should do something like this:
add_action( 'generate_after_entry_header', function() {
if ( is_singular( 'page' ) ) {
generate_posted_on();
}
}, 20 );
Hi,
this adds the date before the content:

How can I add it right after the featured image though?
Where is the featured image set to display? After the title?
Yep, after the title!

Try the updated code here: https://generatepress.com/forums/topic/displaying-date-author-under-featured-image-in-pages/#post-1403214
That's working, thank you!!
How can I put the date & author inside of a div?
Like this:
add_action( 'generate_after_entry_header', function() {
if ( is_singular( 'page' ) ) {
echo '<div class="your-div-class">';
generate_posted_on();
echo '</div>';
}
}, 20 );