【问题标题】:How to fetch wordpress posts through query() and output them in json?如何通过 query() 获取 wordpress 帖子并以 json 格式输出?
【发布时间】:2021-08-06 18:44:02
【问题描述】:

这段代码工作正常并拉动博客文章,但唯一的问题是我希望数据位于 json 数组中,以便我可以使用它创建一个 api 并在外部显示帖子......

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

【问题讨论】:

    标签: php android arrays wordpress jsonp


    【解决方案1】:

    Wordpress 带有一个返回 JSON 的内置 API。你可以按如下方式钩住它

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

    API Documentation

    如果您想制作自定义代码,我建议使用wp_send_json() 在您的主题/子主题中使用以下代码创建一个新模板文件,然后创建一个新页面并在页面属性中选择模板文件

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

    【讨论】:

      【解决方案2】:

      将此代码添加到function.php主题文件中

      <?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 文件代码:

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

      【讨论】:

        猜你喜欢
        • 2020-11-23
        • 1970-01-01
        • 2017-04-23
        • 2012-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多