skip to Main Content

This code is working fine and pulling the blog posts but the only problem is that i want the data to be in a json array so that i can create an api with that and show posts outside…

<?php
require('/home/dealicopter/public_html/wp-load.php');
function wpdocs_custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );
$args = array(
    'posts_per_page' => 10,
    'cat' => 7,
);
query_posts($args); if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php 
    $permalink = the_permalink();
    $title = the_title();
    $age = array("id"=>$permalink, "title"=>$title); 
    ?>
    <?php echo json_encode($age); ?>
<?php endwhile; else: echo "[]"; endif; ?>

<?php wp_reset_query(); ?>

2

Answers


  1. WordPress comes with an build-in API that returns JSON. you can hook in this as follows

    https://example.com/wp-json/wp/v2/posts/?filter%5Bposts_per_page%5D=-1

    API Documentation

    if you want to make custom code I recommend using wp_send_json() create a new template file in you theme / child theme with the following code then create a new page and select the tempalte file in the page attribute

    <?php
    /*
    Template Name: My Template Name
    */
    
    add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );
    $args = array(
        'posts_per_page' => 10,
        'cat' => 7,
    );
    
    $output = array();
    query_posts($args);
    
    if (have_posts()) :
      while (have_posts()) : the_post();
        $permalink = get_the_permalink();
        $title = get_the_title();
        $output[] = array("id"=>$permalink, "title"=>$title); 
      endwhile;
    endif;
    wp_send_json($output);
    
    Login or Signup to reply.
  2. Add this code to the function.php theme file

    <?php
    function get_post()
    {
        $args_query = array(
            'post_type' => array('post'),
            'post_status' => array('publish'),
            'nopaging' => true,
            'order' => 'DESC',
        );
    
        $query = new WP_Query($args_query);
        $data = null;
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                $data[] = array('title' => get_the_title(), 'content' => get_the_content(), 'link' => get_the_permalink());
            }
        }
        wp_reset_postdata();
    
        if ($data != null) {
            echo json_encode(array('success' => true, 'data' => $data));
        } else {
            echo json_encode(array('success' => false, 'msg' => 'post not found'));
        }
    
    
        wp_die();
    }
    add_action('wp_ajax_get_post', 'get_post');
    add_action('wp_ajax_nopriv_get_post', 'get_post');
    ?>
    

    Javascript file code:

    <script>
        (function($) {
            $('button').click(function(e) {
                e.preventDefault();
                $.ajax({
                    type: "post",
                    url: "https://********.com/wp-admin/admin-ajax.php",
                    data: {
                        'action': 'get_post'
                    },
                    dataType: "json",
                    success: function(response) {
                        console.log(response)
                    }
                });
            });
        })(jQuery)
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search