I want meta description of currently executing page in the header.php file in wordpress.
Wordpress function "get_post_meta" is not useful for this. It only returns custom meta tags.
can anyone help me out.
I'm going out on a limb and assuming that you're generating meta description tags dynamically. If that's the case, this should get you what you need...
<?php
echo get_post_meta($post->ID, 'description', true);
?>
If, on the other hand, that's not what you're doing... that method will not work.
Related
I am quite new to WordPress so I am not sure how this would work.
Essentially I have the Yoast plugin handling my SEO related things. The main one giving problems is the description meta tag.
The site is bilingual and I would like the description meta tag to be translated as well. Yoast currently does not provide that option, unless I get another plugin which I do not want to get into.
As it stands, I am told that I can insert the description meta tag through the functions.php using add_action. This unfortunately does not work as it only adds another description meta tag.
Currently my code looks like this:
function insert_meta_tag_in_head () {
echo '<meta name="description" content="My New content" />';
}
add_action('wp_head', 'insert_meta_tag_in_head', 1);
So basically this just gives me a second description meta tag. I have also seen in other threads, that if I want to replace a tag, I should use the do_action function. Which I call as follows:
do_action('wp_head', 'insert_meta_tag_in_head');
This however does nothing.
What am I doing wrong? How can I change the contents of the description tag given to me through Yoast?
We've previously changed a Yoast title before, however, a description is something we have been working on too.
To sum-up the link above:
You need to filter the wpseo_metadesc action in order to display your
new description.
add_filter('wpseo_metadesc', 'new_desc_function');
I'm trying to link posts from WordPress to an associated post on Tumblr. I have the Tumblr id set as the value for a custom field in each WordPress post.
Here's what makes sense to me, but the link isn't capturing or outputting the value:
<h3><?php the_title(); ?></h3>
Is there a problem with the above code that anyone can see? Any help is, as always, greatly appreciated, thanks!
Also, I'm using the Ajax Load More plugin to load the posts to the page, not sure if that's why...
The meta key should be a string. You're not using $key so I'd remove that to avoid confusion.
Change this:
<?php $key="mykey"; echo get_post_meta($post->ID, tumblr_id, true); ?>
To:
<?php echo get_post_meta( $post->ID, 'tumblr_id', true ); ?>
Okay, so I realize what the problem is, because I'm using a function to load the posts to the page, global $post; needs to be declared within the loop.
Also, as #Nathan Dawson pointed out the meta key needed to declared as a string.
I'd like to have the possibility to define some posts in wordpress where the full post is shown and not an excerpt.
There are various solutions like:
Use a hidden html to declare this and code into the theme to either use the_content or the_excerpt
Hardcode into the theme (if postid == xx then the_content else the_excerpt)
Use post meta data and add "if" into theme to check for them
Create a plugin which adds the functionality automatically and also a checkbox "Always show full" into the post-editor.
The first one is easy but ugly, 2nd one should be doable with some googling but the 3rd one seems to be the most appealing to me but I have no idea how to achieve this.
Since usually in the template all i have is somewhere the_excerpt method from wordpress. I therefore should somehow inject some code there and check if the checkbox for the current post is set and then just use the_content instead. Is this possible at all or do I need to modify the theme anyway?
Thanks for your inputs.
I would use custom fields, it's more flexible, so you don't need to hard code the page ids.
<?php
$show_full_content = get_post_meta($post->ID, 'show_full_content', true);
if ($show_full_content == 'yes') {
the_content();
} else {
the_excerpt();
}
?>
You might be interested in ACF, it can make it more user friendly.
I am trying to incorporate an image slider shortcode into a custom WordPress theme template, so that the client will be able to paste the shortcode into the custom field and the template will read this and display it correctly. I do not want the client to paste the shortcode in the body of the post, as the slider needs to be displayed outside of the post wrapper (at full browser width).
I don't know much about php, so would really appreciate any help with this!
The code I have so far to display the slider via the template is:
<?php echo do_shortcode("[metaslider id=27]"); ?>
And to display the output of custom fields I have:
<?php echo get_post_meta($post->ID, 'slider', true); ?>
Each of these works on its own, but I need to combine them, so that the client doesn't have to edit the template just to add the shortcode. So I am thinking something like this should work:
<?php echo do_shortcode("[<?php echo get_post_meta($post->ID, 'slider', true); ?>]"); ?>
... but it doesn't.
Many thanks in advance for any help with this.
C
Simply pass the return value of get_post_meta (which would contain the shortcode as far as I understood) to the do_shortcode function as an argument:
do_shortcode(get_post_meta($post->ID, 'slider', true));
I've just followed this example from Wordpress and I have successfully added an extra Meta Box in Post interface, and the value is stored in DB.
Now my question is, how can I retrieve and display the content of this meta box?
I'm trying the following code:
$intro = get_post_meta($post->ID, 'post_intro', true);
echo $intro;
But I get nada. What am I doing wrong?
And while I'm here, does anybody know if I can place this extra meta box above the default text box in Wordpress post page?
is your snippet within the loop? If so use get_the_ID() instead of $post->ID.
it should look the this:
$intro = get_post_meta(get_the_ID(), 'post_intro', true);
echo $intro;
If you need to get you meta data outside the loop do this:
global $post;
$intro = get_post_meta($post->ID, 'post_intro', true);
echo $intro;
The reason you were getting nothing is because you have to globalize the $post variable if you want to access it. Always use the first method unless you have no choice. If your trying you use meta data for page templates please say so because I have a better solution for handling meta data in that situation.
Good luck!
As a side note I'd like to reference a WordPress Meta Box PHP Helper class that might help you and others when working with WordPress Meta Boxes.