Retrieve value of custom meta box page - php

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);

Related

Using PHP variable inside do_shortcode() for WordPress

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.

How to use shortcode in php code to get value via shortcode for php code?

I'm trying to get postid via shortcode to get parent category of that post.
Here is my code
I tried this code for now but not get any luck, I don't know what is going wrong. Please consider my request I'm very new to PHP and WordPress.
<?php
$category_detail=get_the_category('[field parent-id]');//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}
?>
I want to print my post id like this
$category_detail=get_the_category('4');//$post->ID
I can't use PHP code to get post id as my other format is make with shortcodes so please help me in this. Thanks (Sorry for poor English)
Update : I tried this code also but no luck
<?php
$id = do_shortcode('[field parent-id]');
$category_detail=get_the_category($id);//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}
?>
The problem is this shortcode is trying to get the parent-id, but the parent ID of what? You need to tell It, so try this (untested)
global $post;
$parent = $post->post_parent;
$id = do_shortcode('[field '.$parent.']');
$category_detail=get_the_category($id);//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}

WordPress, Get Value from Meta Data object

I'm using a custom cropper, and I'm getting the data using the following:
$imageSingle = get_post_meta( get_the_ID(),'image',true);
It's returning the following when I print it out 'print_r':
{"original_image":"61","cropped_image":"61"}
I've tried the following but can't return the ID of the cropped image itself:
<?php echo $imageSingle[0]->cropped_image;?>
<?php echo $imageSingle[0]['cropped_image'];?>
Any help or tips would be greatly appreciated.
The data is JSON encoded..
so after
$imageSingle = get_post_meta( get_the_ID(),'image',true);
you do
$imageSingle = json_decode($imageSingle);
Now.. The value is in $imageSingle->cropped_image
get_post_meta() return a object, not an array, you should be remove [0], like this:
<?php echo $imageSingle->cropped_image; ?>

Get_post_meta Wordpress

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>';}?>

Check for custom fields

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.

Categories