WordPress, Get Value from Meta Data object - php

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

Related

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 Advanced Custom Field gallery doesn't return an array

I have ACF Plugin installed and I have a gallery filed in my post. I've tried all these docs but still getting the error :
Invalid argument supplied for `foreach()`
this happens because the input of the for each is not an array!
Do you have any clue what's wrong with this?
Do you think if I have to set something while I've defined the custom field?
<?php
$images = get_field('mygall');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<?php echo wp_get_attachment_image( $image['ID'], $size ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I think your problem comes from the fact that you are using get_field() instead get_fields(). That's way you don't get an array.
If it still doesn't work check the documentation for get_fields() here. Try to debug it like using only get_fields() and see what is the output. If it is an empty array then it means that you are calling the function out of the loop and it can't get the post id. So do a second test with manually setting the post id like get_fields(123); and check the results. If there are no results then there is something wrong with that post. And if there are results then you can do a final test with checking what will be the result of get_fields(123, 'gallery').
All the above debugging can be wrapped in something like:
echo '<pre>';
print_r( get_fields(123) );
echo '</pre>';
Basically this will give you some idea of what is the structure of the data that you get from this function and how you can manipulate it so to get what you need.

Retrieve value of custom meta box page

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

Use value inside another PHP function

In WordPress i'm currently using this function from a plugin <?php the_field('event_boat'); ?> to output the post ID for the selected field, which happens to be 5755 in this example.
As the plugin only allows me to output the post ID is it possible to incorporate the value from that function inside <?php echo get_permalink(); ?> to get the permalink based on the post ID?
You can pass the ID as a parameter in the get_permalink function, either by storing the ID value in a new variable, or just passing in the ACF-function directly as a parameter.
$post_id = get_field('event_boat');
echo get_permalink($post_id) // echoes out link for ID 5755
I'm using get_field() instead of the_field() because the_field() will echo out the value, We just want to pass it along.
We might aswell just do:
echo get_permalink(get_field('event_boat'));
This should work fine. :)
<?php $a = get_permalink(get_field('event_boat')); echo $a; ?>
<?php $a =get_permalink(get_field('event_boat')); echo $a; ?>
This should work fine. :)
<?php echo get_permalink(get_field('event_boat')); ?>

Syntax for using echo inside an echo (wordpress slider)

this is the piece of code I'm not sure how to deal with:
<?php echo get_post_meta($post->ID, 'smartslider2', true); ?>
<?php echo do_shortcode('[smartslider2 slider="InsertHere"]'); ?>
I simply want to insert the first echo instead of InsertHere. The first echo should output the content of a custom field. The second should recall the slider with the specific number inserted in the custom field. When trying different possibilities I only get errors.
Can anybody help?
Thank you :)
Don't echo the first value, use it directly inside your second echo.
<?php
echo do_shortcode('[smartslider2 slider="'.get_post_meta($post->ID, 'smartslider2', true).'"]');
?>
Or you can easily put that first value in a variable and use that variable in the second line
<?php $slider = get_post_meta($post->ID, 'smartslider2', true); ?>
<?php echo do_shortcode('[smartslider2 slider="'.$slider.'"]'); ?>

Categories