0

Hey I am trying to send JSON data from (WRITE AN EXCERPT (OPTIONAL)) this section. Let say I pass this '{"a":"b"}' as Map now in the wordpress templete I tried to get this so I was able to get but when I decode this any display this so I does not see any thing please help as it took a lot of my time but I was not able to solve.

Here is My Template

<?php
/**
 * Template part for displaying course results in search pages
 *
 */
global $post;
$post_title         = get_the_title($post->ID);
$image_id           = get_post_thumbnail_id($post->ID);
$post_img_url       = tuturn_prepare_image_source($image_id, 612, 400);
$post_url           = !empty($post->ID) ? get_the_permalink($post->ID) : '';
// website extra features
$contents           = !empty($post) ? $post->post_content : '';
$extra              = !empty($post) ? get_the_excerpt($post) : '';




$author_id          = get_post_field('post_author', $post->ID);
$author_name        = get_the_author_meta('display_name', $author_id);
$post_date          = date_i18n(get_option('date_format'), strtotime(get_the_date())) ;
if ('course' !== get_post_type()) {
    return; // Skip displaying if the post type is not 'course'
}
?>
<div class="col-sm-12">
    <div class="tu-articleitem tu-artical-list">
        <?php if(!empty($post_img_url)){ ?>
            <figure>
                <img src="<?php echo esc_url($post_img_url); ?>" alt="<?php echo esc_attr($post_title); ?>">
            </figure>
        <?php } ?>
        <div class="tu-articleinfo">
            <?php echo get_the_term_list($post->ID, 'category', '<ul class="tu-taglinks tu-taglinksm"><li>', '</li><li>', '</li></ul>'); ?>
            <?php if (!empty($post_title)) { ?>
            <div class="tu-arrticltitle">
                <h4><a href="<?php echo esc_url($post_url) ?>"><?php echo esc_html($post_title) ?></a></h4>
            </div>
        <?php } ?>
        <?php if (!empty($contents)) { ?>
            <div class="tu-description">
                <p><?php echo wp_trim_words($contents, 10, '...'); ?></p>
            </div>
        <?php } ?>
        // this is the section where i decode json
        <?php if (!empty($extra)){
            echo $extra; // i get results from post EXCERPT
            $data = json_decode($extra); // when i decode i don't see
            echo $data; // any thing in the output
        }
        ?>
        </div>
    </div>
</div>

1 Answer 1

0

One of two things is happening here:

Invalid JSON

If the value of $extra isn't a properly-constructed JSON string, then json_decode( $extra ) is returning null. echo null; won't print anything to the screen.

Valid JSON

If $extra is a valid JSON string, When you json_decode() it, you'll get an object (specifically, a stdClass object). There's no way to echo a stdClass object; when I try that, I get a PHP warning:

PHP Warning:  Uncaught Error: Object of class stdClass could not be converted to string

So your echo( $data ); line is throwing a warning, which your server is most likely suppressing.

I'd recommend using var_dump( $data ); to see what's actually happening.

2
  • 1
    yes after doing like this var_dump( $data ); i got output which was null. as i was storing json like this '{"a":"b"}' so after storing json like this {"a":"b"} i got the perfect output and now i am able to get data from this Map Thanks
    – waqas023
    Jun 28, 2023 at 5:09
  • If this solved the problem for you, please feel free to mark the answer "Accepted". That way others who might have the same problem you're having will see that it's a solution.
    – Pat J
    Jun 28, 2023 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.