I have a custom field, "price" and I want to pull that custom field for each individual post.
I have the code:
<?php $price = get_post_meta($post->ID, 'Price', true); ?>
<?php echo $price; ?>
But this is pulling the value of the first post and applying it to all the posts. Can someone help me understand this code and understand why it's applying the same field value to all my posts? How can I make it pull the individual value for each post?
Thank you all, I'm really new at this.
try to put this line at the start of script:
global $post;
thn try with your code:
<?php $price = get_post_meta($post->ID, 'Price', true); ?>
<?php echo $price; ?>
thanks
I have already checked it.
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'yourcustomfield', true);
wp_reset_query();
?>
Related
I am using HTML5Blank as a starting theme and it comes with this function which returns 40 chars excerpt:
<?php html5wp_excerpt('html5wp_custom_post'); ?>
My main blog page is more complex, so I am using array to store values into it and echo them where I need them:
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $post_titles[$counter] = get_the_title($post->ID); ?>
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post_id); ?>
<?php $post_permalinks[$counter] = get_the_permalink($post->ID); ?>
<?php $post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid')); ?>
<?php $counter++; ?>
<?php endwhile; ?>
All other fields work and I can echo them, but I don't have any idea how to make excerpt work because it isn't echoing anywthing with:
<?php echo $post_excerpts[0]; ?>
First of all I notice, that you are using a variable called $post_id, which is not defined as far as I can see. You need to add a $post_id = $post->ID; before or alternatively you can use instead:
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post->ID); ?>
Maybe this already solves your problem. But to make sure, I will take it further.
I took a look at the functions.php of the HTML5-Blank-Theme: https://github.com/html5blank/html5blank/blob/master/src/functions.php
// Create 40 Word Callback for Custom Post Excerpts, call using html5wp_excerpt('html5wp_custom_post');
function html5wp_custom_post($length)
{
return 40;
}
So the function only returns the value of 40. I guess you can simply use the html5wp_excerpt() like this:
html5wp_excerpt(40);
Maybe there is something wrong with the html5wp_custom_post, so you can get rid of it to test this. And I also think, why use an addtional function, if it only returns a number... you can easily set it in the function call.
I don't know if this functions accepts an post ID as a parameter. So maybe it can only be used inside of a single.php page. I can't find a documentation about this, maybe you can do some research and find it out.
Here is another way to achieve it:
You can use the get_the_title() function that will accept the post id. Unfortunately, the get_the_excerpt() does not accept it.
So we first need to get the post object and then apply a filter to get the excerpt of the post. Do this by putting this code inside your while loop:
<?php $current_post = get_post($post->ID); ?>
You now have the current post as an object. In the next line, we apply the filter and save the result to the array at the right index position:
<?php $post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt); ?>
I wonder why there are so many php opening and closing tags, so you could make your code more readable:
<?php while ($the_query->have_posts()) : $the_query->the_post();
$current_post = get_post($post->ID);
$post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_titles[$counter] = get_the_title($post->ID);
$post_permalinks[$counter] = get_the_permalink($post->ID);
$post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid'));
$counter++;
endwhile; ?>
Just as an additional info, you could also use only the filters, should work with your post object:
$post_titles[$counter] = apply_filters('the_title',$current_post->post_title);
EDIT:
You can trim your excerpt to a certain length with mb_strimwidth (read more: https://www.php.net/manual/en/function.mb-strimwidth.php) :
$current_post = get_post($post->ID);
$trim_excerpt = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_excerpts[$counter] = mb_strimwidth($trim_excerpt, 0, 40, '...');
EDIT 2:
Maybe you should check if you are getting your post object. The fact that you are always seeing the same excerpt, maybe means you get the excerpt of the current page (not the post in your query).
$current_id = get_the_id();
$current_post = get_post($current_id);
first - sorry about the title, not sure exactly how to name what I need.
Here's what I'd like to do:
1) Get the post ID
2) Get the Custom Field from said post id
3) Display shortcode with the custom field's value
Here's what I have:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'id-del-instructor', true);
wp_reset_query();
?>
With this, I am able to get and display the shortcode's value - although I need to store it for later use rather than display it.
Then, I found this to display the shortcode with PHP:
<?php echo do_shortcode("[awsmteam id="XXX"]"); ?>
I have tried combining these two codes but every time it breaks my site.
Basically, where it says XXX, I need the value from the shortcode. It's probably something simple to achieve but I have been looping around and can't get my head around it.
Help? :) Thanks so much!
RESOLVED with this code - thanks #Alon Eitan:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$meta = get_post_meta($postid, 'id-del-instructor', true);
wp_reset_query();
echo do_shortcode('[awsmteam id="' . $meta . '"]');
?>
If you're doing this inside a page template, you could simply do.
$meta = get_post_meta(get_the_ID(), 'id-del-instructor', true);
echo do_shortcode('[awsmteam id="' . $meta . '"]');
rather than call the global $wp_query and go through those other steps.
I have added a custom meta box, once i update the data, it saved successfully. But now i am trying to fetch using this code, but it is not getting content of meta box saved. Please help me out.
<?php
global $post;
$code = get_post_meta( $post->ID, 'caption_code_footer', true );
if($code != ''){
echo $code;
}
else { ?>
I am lorem Ipusm Text
<? } ?>
This code appears okey, so you need debug a bit....It's sound like $post->ID has wrong value... try echo it. echo $post->id; or all the object var_dump($post);
Also Check If you arent in right context. For example in category context you need to use the loop...
I think something error on update your meta box please try this..
$footer=$_POST['caption_code_footer'];
update_post_meta($post_id,'caption_code_footer',$footer);
for echo the value updated..
get_post_meta($post->ID, 'caption_code_footer', true);
Hello stackoverflow people, I need help. I'm using wordpress and somehow I can't use this function:
$post_id = 12;
echo get_post($post_id)->post_excerpt;
Somehow it prints nothing. Here is the full div. Can you help me to solve this problem?
<div id="block1">
<div class="inblock1">
<h2>About boots</h2>
<p><?php $post_id = 12;
echo get_post($post_id)->post_excerpt; ?> </p>
apac
</div>
</div>
It sounds like you don't actually have an excerpt set for this post. You can always use a conditional to test it, and output a custom excerpt (from the post_content) if one doesn't exist:
$my_post = get_post($post_id);
// If the excerpt is empty, generate one from post_content, else display the saved excerpt
echo empty($my_post->post_excerpt) ? wp_trim_words($my_post->post_content, 55, '...') : $my_post->post_excerpt;
Read more about wp_trim_words() in the Codex.
You may need to use setup_postdata() because <?php $post_id = 12; echo get_post($post_id)->post_excerpt; ?> will return excerpt if you have excerpt data in excerpt field but below code will return data from content if you don't have data in excerpt field.
$post_id = 12;
$tempVar = $post;
$post = get_post($post_id);
setup_postdata($post);
the_excerpt();
wp_reset_postdata();
$post = $tempVar;
Need help with a wordpress get_meta_post.
I need to display a div only if the custom-field promo is found in the get_meta_post. If true this is suppose to echo the :
<?php get_post_meta(get_the_ID('promo', true)
<div class="packagePromoItem">Promotion</div>
?>
Assuming that's your actual code, you have several typos, or major misunderstandings about how PHP works. This should work (using alternative syntax, which I think is a little more readable for this):
<?php $promo = get_post_meta(get_the_ID(), 'promo', true); ?>
<?php if ($promo): ?>
<div class="packagePromoItem">Promotion</div>
<?php endif; ?>
I've also assigned the promo post meta to its own variable so it's easier to follow.
You're using get_the_ID wrong. Get the ID does not accept any parameters and gets the ID of the current post. If you need to check if the post has meta 'promo', then just check if get_post_meta returns null/false.
I'm not sure what you're asking through your example though. If you're trying to echo out the post meta:
<?php if (get_post_meta(get_the_ID(), 'promo', true))) { echo'<div
class="packagePromoItem">' . get_post_meta(get_the_ID(), 'promo', true) .
'</div>';}?>