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>';}?>
Related
Is it possible to only show the code below if there's a category description?
<h2>About <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></h2>
<?php $catID = get_the_category(); echo category_description ( $catID[0] ); ?>
I have these codes on my single posts in Wordpress but sometimes, I forgot to add descriptions on a new category that I added. So when a user visits the post, they will just see the word About Category and no description at all, making it looks like an incomplete article.
I'm not a developer and I'm also not familiar with PHP. I only added that code on the single.php to show the description. But I want it not to show when there's no description available.
Hope someone can give me the exact code to make it work.
Thanks!
Good practice to assign your values into variables at the top of your script before entering into HTML whenever possible. This will help prevent you making redundant calls and to debug your code better. Once you have your assigned values, you'll want to check if the value is empty. I am assuming the code you presented will always return some kind of value for index 0.
<?php
$cat = get_the_category();
$cat_0 = $cat[0];
$cat_0_name = $cat_0->cat_name;
$cat_0_desc = category_description($cat_0);
?>
<?php if(!empty($cat_0_desc)): ?>
<h2>About <?php echo $cat_0_name; ?></h2>
<?php echo $cat_0_desc; ?>
<?php endif; ?>
I should like to point out that I am choosing to use an alternative syntax for control structures versus the traditional brace control. This will make your code more readable and easily debugged when mixed in with HTML.
If your code is still throwing you errors, I would suggest you check your error logs as something would be happining during the get_the_cateory() call or that it's not returning any values resulting in error with $cat[0].
<?php if ($cat = get_the_category() && count($cat)>0) { ?>
<!-- <?php echo print_r($cat,1); ?> -->
<h2>About <?php echo $cat[0]->cat_name;?></h2>
<?php echo category_description ($cat[0]->cat_id); ?>
<?php } ?>
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);
I want to style wordpress posts differently, so I decide it to based on post id number odd and even to have different CSS class. So I wrote this code to check is id of post odd or even in content.php:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $num = the_ID(); ?>
<?php if($num % 2 == 0){
echo "<h1> It's even </h1>";
}else{
echo "<h1>It's odd</h1>";
}
?>
.....
</article>
Problem is that it always says "It's even", even it is odd, For example, post number is 33, and it says that is even. So, what is the problem with my approach, is there any better way to achieve what I need ?
=== 0
You're or need to compare same types, not different types.
Assuming the_ID() is from wordpress I'm hoping maybe changing the function would net you the results you want as the code above in theory works from my testing but the_ID() as commented would most likely be the issue.
Try using get_the_ID() as it returns the ID of the post you are on.
the_ID():
Note: This function displays the ID of the post, to return the ID use get_the_ID().
From this I am assuming the_ID() also produces an echo '';
https://developer.wordpress.org/reference/functions/get_the_id/
<?php $num = get_the_ID(); ?>
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);
Basically, on my website I'll be embedding two different types of objects, one will be a Youtube embed, one will be a soundcloud embed. I'll be doing this by using custom fields on Wordpress and pulling in through the post_meta.
This is the current code that I have (which pulls in the current 'song' field, which is YouTube)
<?php echo get_post_meta($post->ID, 'song', true); ?>
When I try adding that and this:
<?php echo get_post_meta($post->ID, 'soundcloud', true); ?>
Nothing actually happens, is there a way to make it so it will check if the fields are null or not and then display the one that is in fact, not null.
Any help is appreciated, thanks
Something like
<?php
$song = get_post_meta($post->ID, 'song', true);
if (empty($song)) {
echo get_post_meta($post->ID, 'soundcloud', true);
}
else {
echo $song;
}
?>
should do the trick. Though if both are null it won't output anything.