[Resolved] How to add post meta after post title

Home Forums Support [Resolved] How to add post meta after post title

Home Forums Support How to add post meta after post title

  • This topic has 9 replies, 2 voices, and was last updated 2 years ago by Fernando.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2269860
    iamarghya

    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…

    #2269875
    Fernando
    Customer Support

    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!

    #2269880
    iamarghya

    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?

    #2269894
    Fernando
    Customer Support

    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!

    #2269900
    iamarghya

    oh ok got it

    #2269905
    Fernando
    Customer Support

    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.

    #2269909
    iamarghya

    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…😅

    #2269915
    Fernando
    Customer Support

    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;
    }
    #2269969
    iamarghya

    Yep, perfect thanks.

    #2269970
    Fernando
    Customer Support

    You’re welcome iamarghya!

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.