Archived topic
Blog pages - Move Meta fields below Image
10 replies · Started by Paul on March 1, 2022
In order me to only target the blog pages via php to move meta fields below images, I have a portion of the code below. I did try this without the if statement and it left the meta below the title and added below the image, which is not what I want.
if (is_search() || is_archive()) {
add_action( 'after_setup_theme', function() {
remove_action( 'generate_after_entry_title', 'generate_post_meta' );
add_action( 'generate_after_entry_header', 'generate_post_meta', 15 );
} );}
I can hide the issues where the meta still exists below the title via css.
I have already changed the orientation in customizr to have Image below title.
What I need is assistance with the code above, so that I remove the meta below the title and also to verify my code is correct to just target the blog pages.
Hi Paul,
To clarify: By blog pages, do you mean the Blog index page and the archive pages?
If yes, you can modify the code to this -
add_action( 'after_setup_theme', function() {
if ( is_search() || is_archive() || is_home() ) {
remove_action( 'generate_after_entry_title', 'generate_post_meta' );
add_action( 'generate_after_entry_header', 'generate_post_meta', 15 );
}
} );
If you want to include single post pages, you can do it like this:
add_action( 'after_setup_theme', function() {
if ( is_search() || is_archive() || is_home() || is_single() ) {
remove_action( 'generate_after_entry_title', 'generate_post_meta' );
add_action( 'generate_after_entry_header', 'generate_post_meta', 15 );
}
} );
I do not want to target the home page, but the archive and search pages only. In that case, do I simply need the following?
add_action( 'after_setup_theme', function() {
if ( is_search() || is_archive()) {
remove_action( 'generate_after_entry_title', 'generate_post_meta' );
add_action( 'generate_after_entry_header', 'generate_post_meta', 15 );
}
} );
I don't want any of this code to affect the home page, so am I right with the code above?
Ah yes that's right. If you only want this to apply on search and archive pages then that code should suffice. :D
About the part "added below the image": Have you sorted this out?
If not, you may have to replace generate_after_entry_header with another hook or atleast set the priority to any value lower than 10 as post image is hooked to the same hook w/ priority of 10.
https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/content.php#L54-L61
I ran the following and now the meta fields like date and author are gone...
add_action( ‘after_setup_theme’, function() {
if ( is_search() || is_archive()) {
remove_action( ‘generate_after_entry_title’, ‘generate_post_meta’ );
add_action( ‘generate_after_entry_header’, ‘generate_post_meta’, 15 );
}
} );
What I am wanting is the meta fields like author and date to show below the image, not above it.
I thought the PHP code you gave me would do this, but it keeps the meta above the image, which I am hiding via CSS.
Ah I thought you wanted it above the image as you mentioned on this on the topic opener.
"... added below the image, which is not what I want."
Any chance you can provide a mockup image of how exactly you want things to be laid out? So I can use it as reference for any PHP or CSS I may have to write for you. :)
About the code:
This code is pretty simple. It basically just moves the post meta from its default hook - https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/inc/structure/post-meta.php#L510 - to generate_after_entry_header.
The part - add_action( 'generate_after_entry_header', 'generate_post_meta', 15 ); - should fit your preferred "What I am wanting is the meta fields like author and date to show below the image, not above it." as priority value of 15 means the post meta appears right after the image.
If it's not displaying as intended by the PHP snippet, can you link us to the page you're working on so I can observe what exact is happening to it?
What I am wanting is the Title at the top, image in the middle, and meta (date and author) at the bottom.
As you will see in the following link, I am hiding the entry-summary.
However, the meta fields are not showing with the PHP code you gave me.
https://www.dailycdev.com/category/devotionals/
Thanks for looking at this.
Ah I see.
We may be using the wrong hook for running this removal and re-adding.|
This one should work -
add_action( 'wp', function() {
if ( is_search() || is_archive() ) {
remove_action( 'generate_after_entry_title', 'generate_post_meta' );
add_action( 'generate_after_entry_header', 'generate_post_meta', 15 );
}
} );
That worked. Thanks.
No problem. Let us know if you need further help. :D