Reputation: 4686
I am currently working on a plugin. As such, I inserted a block of code in the header with a function. The code is supposed to be pulling data from custom fields but it returns empty tag. However, it works if I add it directly to the header. Any ideas what the problem is? My code looks like below.
<?php
function pyramid() {
?>
<meta name="description" content="<?php $desc = get_post_meta($post->ID, 'description', true); if($desc) { echo $desc; } else {
// do nothing;
} ?>" />
<?php
};
add_action( 'wp_head', 'pyramid' );
?>
It returns the result below when added through the plugin.
<meta name=description content>
Upvotes: 0
Views: 38
Reputation: 9509
global $post;
needs to be added to the pyramid function.
Alternatively you can also use get_the_ID()
instead of $post->ID
Also, if you wanted to, you can shorten your pyramid function to just
function pyramid() {
global $post;
?><meta name="description" content="<?= get_post_meta($post->ID, 'description', true) ?: '' ?>" /> <?php
};
add_action( 'wp_head', 'pyramid' );
Upvotes: 1