Site logo

Archived topic

custom field in blog archive

10 replies · Started by Alfonso on January 22, 2021

Viewing posts 1–11 of 11

Hello,

I have created a custom field to display a subtitle on my blog.

This subtitle is displayed correctly in my single post, but in my archive pages it doesn't sync well, I get the result from another post, how can I fix it?

You can check it with this screenshots:

Single page custom field is displaying fine:

https://snipboard.io/Tne5yI.jpg

But not in Archive pages:

https://snipboard.io/zZ8cDl.jpg

This value is not correct, is the same like post below.

Hi there,

what code are you using to output the Custom Field ?

To get the Post Meta on archive page you need to use the Object ID:

$metaValue = get_post_meta( get_queried_object_id(), 'MetaKey', true);

Hi there,

I've updated my snippet with this code:

add_action( 'wp_print_footer_scripts', function () {

$subtitulo = get_post_meta( get_queried_object_id(), 'Subtitulo', true);
if ( $subtitulo ):
?>
<script>
(function( $ ) {
let str = '<span class="subtitulo-blog">';
str += '<?= $subtitulo ?>';
str += '</span>';
$(str).insertAfter('.entry-title');
})( jQuery );
</script>
<?php
endif;

} );

But is empty now

Any reason for using jQuery to insert the element ?
As you can simply do this:

add_action('generate_after_entry_title', function(){
	$subtitulo = get_post_meta( get_the_ID(), 'subtitulo', true);
	if ($subtitulo) {
		echo '<span>' .$ubtitulo. '</span>';
	}
},5);

Hi there, with this snippet this is empty too, is not working :(

I amended the code above - can you try that.

Thank you David, i've updated the code but is the same result :(

I've updated the snippet and now is working fine

Phew... glad we got there :)

This archived topic is closed to new replies.