Archived topic
Replace existing post meta display
3 replies · Started by Kelvin on August 4, 2021
Dear Sir/Madam,
Please refer to screenshot https://prnt.sc/1j9zinq, I know how to add elements to page but don't know how to replace.
I have posts with different categories, how can I replace the post date with custom test with custom field content?
For example, I want to replace the post date with "Course code: xxx-xxxxx" for specific categories but not all. Of course if category is not the list of specific categories, keep the post date there.
Please note xxx-xxxxx is the content from Toolset custom field.
Hi Kelvin,
Have you tried using Block element - post meta template?
https://docs.generatepress.com/article/block-element-post-meta-template/
And here's another video that shows how to add custom filed dynamic data to the block element - page hero.
But it would be the same for block element - post meta template.
https://youtu.be/H6wHd83DfDI
Let me know if this helps :)
Dear Ying,
Thanks for your reply. I tried but had no luck, is there any wrong setting I made?
Here is the setting I made from the Elements https://prnt.sc/1jax94f
I did add a Block Elements and select the Type as Post Meta Template, Location is default After post title and replaces default primary post meta, these elements only be applied on specific category, course/4645/ is the specific category
Please advise what I did wrong.
Best regards,
Kelvin.
Can you share the contents of the block element - post meta template you're using?
If you're trying to replace the rendered element of post dates in a block element, we can do filters on it.
Example:
add_filter( 'generate_dynamic_element_text', function( $post_date, $block ){
$category = 'your category here'
if ( in_category($category) && is_singular && 'post-date' === $block['attrs']['gpDynamicTextType'] && empty($block['attrs']['gpDynamicDateType']) ) {
$post_date = sprintf(
'<span class="course-code-wrap>Course code: <span class="course-code">%1$s</span></span>',
get_field('course_code', get_the_ID())
);
}
return $post_date;
},15,2);
You may have to replace get_field('course_code', get_the_ID()) with whatever function Toolset custom field uses to fetch the field value.
You'll also have to change the value of $category = ''; to the category you want this applied on.