Site logo

Archived topic

How to add post meta after post title

9 replies · Started by iamarghya on June 30, 2022

Viewing posts 1–10 of 10

So, on this website in this latest job section see there is the last date post meta with the post title, and in the 2nd link you can see I created the same post-title concept and also I have the needed post-meta but how can I add the post meta with the post title just like the 1st website shows...

You can add it through a Headline Block. Headline Blocks can retrieve Post meta.

If however you want them appear as one link, you would need custom code for that.

For instance, it’s doable if you add my-title to the Class list of the Headline Block as such: https://share.getcloudapp.com/8Lu0pZd0, then add a snippet like this:

add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
	if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-title' ) !== false ) {
		$my_meta = get_post_meta(get_the_ID(),'meta-date');
		if($my_meta) {
			return $content . " " . $my_meta[0];
		} else {
			return $content;
		}
	}
	
	return $content;
}, 10, 3);

You would need to replace meta-date with the meta field name of your date. Then, depending if the return value of your post meta is an array or a string, you might also need to modify this part of the code: $my_meta[0]

Hope this helps!

Perfectly perfect now If I just want to style that date like giving a red background how can I select that date? to use in CSS?

Yes, you can use CSS, but we'll need to add a class to the post meta.

You can modify the PHP snippet into something like this:

add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
	if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-title' ) !== false ) {
		$my_meta = get_post_meta(get_the_ID(),'meta-date');
		if($my_meta) {
			return $content . ' <span class="my-meta">' . $my_meta[0] . '</span>';
		} else {
			return $content;
		}
	}
	
	return $content;
}, 10, 3);

Then, add this CSS:

span.my-meta {
    background-color: #ff0000;
}

Hope this helps!

oh ok got it

Have you modified the PHP? Viewing the site from my end, it seems the span element hasn’t been added yet which is why the CSS isn't working.

Kindly let us know.

No No, I didn't do it at the first then I did it only. One last question, as u can see every post title has a border-bottom what CSS do I need to write for the last child having no border-bottom only the last post will have no border-bottom I tried with last-child but couldn't work out with it so at last, I asked...😅

I see.

Here’s a CSS you can try:

.gb-container.cards > .gb-inside-container > .gb-grid-wrapper > .gb-grid-column:last-of-type > .gb-container {
    border:none;
}

Yep, perfect thanks.

You’re welcome iamarghya!

This archived topic is closed to new replies.