【问题标题】:How do you output JSON using wp_Query in wordpress?如何在 wordpress 中使用 wp_Query 输出 JSON?
【发布时间】:2011-10-01 21:30:21
【问题描述】:

我正在尝试使用 wordpress 发布数据和 meta_key 值来输出或创建 json。

这是我正在使用的代码,但它的 JSON 格式不正确。

$query = new WP_Query('cat=4&meta_key=meta_long');

echo "var json=". json_encode($query);

关于如何做到这一点的任何想法?

【问题讨论】:

    标签: php arrays json wordpress


    【解决方案1】:

    试试这个:

    $query = new WP_Query('cat=4&meta_key=meta_long');
    
    echo "var json=". json_encode($query->get_posts());
    

    【讨论】:

      【解决方案2】:

      Femi 的方法很棒,但如果您的目标是使用 JS 文件中的 WP_Query 数据,那么我建议您查看 wp_localize_script 函数。

      /**
       * WP_Query as JSON
       */
      function kevinlearynet_scripts() {
      
          // custom query
          $posts = new WP_Query( array(
              'category__in' => 4,
              'meta_key' => 'meta_long',
          ) );
      
          // to json
          $json = json_decode( json_encode( $posts ), true );
      
          // enqueue our external JS
          wp_enqueue_script( 'main-js', plugins_url( 'assets/main.min.js', __FILE__ ), array( 'jquery' ) );
      
          // make json accesible within enqueued JS
          wp_localize_script( 'main-js', 'customQuery', $json );
      }
      add_action( 'wp_enqueue_scripts', 'kevinlearynet_scripts' );
      

      这将在main.min.js 中创建window.customQuery 对象。

      【讨论】:

      • 解码json_encode()的目的是什么?为什么不将编码后的数据直接传递给wp_localize_script()
      • json_decode( json_encode() ) 会将所有嵌套对象转换为数组,如果没有它,您将混合使用 stdObj 和关联数组
      【解决方案3】:

      进一步扩展 Femi 的方法,如果您只想返回一些循环数据 + 自定义字段,请尝试以下操作:

      <?php
      
      // return in JSON format
      header( 'Content-type: application/json' );
      
      // Needed if you want to manually browse to this location for testing
      define('WP_USE_THEMES', false);
      $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
      require_once( $parse_uri[0] . 'wp-load.php' );
      
      // WP_Query arguments
      $args = array (
      
      'post_type'              => 'location',
      'post_status'            => 'publish',
      'name'                   => $_GET["location"],
      
      );
      
      // The Query
      $loop = new WP_Query( $args );
      
      //the loop
      while ( $loop->have_posts() ) : $loop->the_post();
      
          // create an array if there is more than one result        
          $locations = array();
      
           // Add in your custom fields or WP fields that you want
           $locations[] = array(
             'name' => get_the_title(),
             'address' => get_field('street_address'),
             'suburb' => get_field('suburb'),
             'postcode' => get_field('postcode'),
             'phone_number' => get_field('phone_number'),
             'email' => get_field('email_address')
           );
      
      endwhile;
      
      wp_reset_query();
      
      // output
      echo json_encode( $locations );
      
      ?>
      

      【讨论】:

      • $locations = array();应该在while循环之外
      猜你喜欢
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多