Archived topic
I want to edit meta data for each post.
16 replies · Started by Usman on July 9, 2022
Hey everyone,
I want to change my metadata from (private link) to https://www.imblogger.in/essential-blogging-tools-arsenal/
Looking forward if someone assist me better.
Hi there,
This should help:
https://docs.generatepress.com/article/block-element-post-meta-template/
Thank you @Leo, yes, it helped.
I updated my meta appearance. Now, I have 2 queries:
- I was using the estimated read time feature with custom PHP code. How can I add this manually now?
- How can I add | or - for space purposes. Should I add a paragraph in between for that?
You can check the appearance, added the link in private. Please also review the appearance of the mobile screens.
Thanks :)
* UPDATE
- I have added the spaces correctly.
- I want to replace the published date with the updated date. If I do so, the date disappears from the posts that haven't any last modified date data.
Looking Forward.
Thanks
Hi Usman,
Let’s tackle the date first, then proceed with the estimated time afterwards.
First, add my-date-headline to the class list of the Headline Block for the modified date as such: https://share.getcloudapp.com/Wnu7DjAl
Then, add this PHP:
function db_rerender_modify_date( $block_content, $block ) {
if ( ! is_admin() && ! empty( $block['attrs']['className'] ) && 'my-date-headline' === $block['attrs']['className'] ) {
if ( get_the_date() == get_the_modified_date() ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished"> %2$s</time>';
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() )
);
$block_content = sprintf( '<span class="posted-on">%s</span> ',
$time_string
);
}
return $block_content;
}
return $block_content;
}
add_filter( 'render_block', 'db_rerender_modify_date', 10, 2 );
Adding PHP reference: https://docs.generatepress.com/article/adding-php/#code-snippets
Kindly let us know how it goes.
Thanks, @Fernando, it worked.
But, it is causing appearance/structure issues on posts that haven't any modified date data. I added private link.
Thanks again. :)
I see. Can you try this snippet instead:
add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-date-headline' ) !== false ) {
if ( get_the_date() == get_the_modified_date() ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished"> %2$s</time>';
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() )
);
$content = sprintf( '<span class="posted-on">%s</span> ',
$time_string
);
}
return $content;
}
return $content;
}, 10, 3);
Make sure the Dynamic Data is added through the Block Settings as such: https://share.getcloudapp.com/z8umQzkD
Kindly let us know how it goes.
Thanks again, @Fernando, now, the date option is working fine.
Before I was using this code for estimated time feature:
function tu_estimated_reading_time() {
$post = get_post();
$content = $post->post_content;
$wpm = 300; // How many words per minute.
$clean_content = strip_shortcodes( $content );
$clean_content = strip_tags( $clean_content );
$word_count = str_word_count( $clean_content );
$time = ceil( $word_count / $wpm );
return $time . ' minutes';
}
add_filter( 'generate_post_author_output', function( $output ) {
$output .= '<span class="read-time"> · Read time: ' . tu_estimated_reading_time() . '</span>';
return $output;
} );
How do you want it to appear? Attached to the date or a Headline Block of its own?
See: https://share.getcloudapp.com/12uLzWbA
Kindly let us know.
I can replace the category block with the estimated time if necessary otherwise on the last (after that category tags).
I see. Well, you don’t need to replace the Category Block. Just create a new Headline Block with this text: text-to-replace. Then, add this class to the Headline: my-estimated-headline.
Example: https://share.getcloudapp.com/yAu1QOwW
Then, add this PHP snippet:
function db_rerender_add_estimated_time( $block_content, $block ) {
if ( ! is_admin() && ! empty( $block['attrs']['className'] ) && 'my-estimated-headline' === $block['attrs']['className'] ) {
$post = get_post();
$content = $post->post_content;
$wpm = 300;
$clean_content = strip_shortcodes( $content );
$clean_content = strip_tags( $clean_content );
$word_count = str_word_count( $clean_content );
$time = ceil( $word_count / $wpm );
$my_search = 'text-to-replace';
$my_replace = $time . ' minutes';
$block_content = str_replace($my_search, $my_replace, $block_content);
return $block_content;
}
return $block_content;
}
add_filter( 'render_block', 'db_rerender_add_estimated_time', 10, 2 );
Kindly let us know how it goes.
Yes, it worked fine. Thanks, man. :)
Please revise it for mobile users too. It is trying to adjust the entire script in one line which is causing issue for mobile users. Shouldn't it go to second line according to screen sizes?
The code I provided shouldn’t affect your layout. Do you have inline-width enabled on Mobile? Can you try disabling it?
Mobile inline-width is disabled for each block but enabled for the entire container. At the container level, there isn't any option to disable inline-width for mobile.
I see. You can either move the inline-width setting to the Headline Blocks, or add this CSS through Appearance > Customize > Additional CSS:
@media (max-width: 768px) {
.gb-container.gb-container-8a25fc79 > .gb-inside-container {
flex-wrap: wrap;
}
}
This allows wrapping for your Container Block on mobile.
Hope this clarifies!