【问题标题】:How to display postmeta key and value in same post id wordpress (JSON data)如何在相同的帖子 ID wordpress 中显示 postmeta 键和值(JSON 数据)
【发布时间】:2017-03-04 09:55:16
【问题描述】:

在那里我有我想显示的数据后元

$command = $_GET['command'];
switch ($command) {
    case 'list_product':

        $loop = new WP_Query( 
                array(
                        'post_type'    => 'product'
                        // 'showposts'    => 4,
                        // 'meta_key'     => '_sale_price', 
                        // 'meta_value'   => '0', 
                        // 'meta_compare' => '>=',
                    )
                ); 
if($loop->have_posts()) :

    $data = array( "api_status" => 1, "api_message" => "success");
    while ( $loop->have_posts() ) : $loop->the_post();

           $data[] = array("id" => get_the_ID(),
          "post_name" => get_the_title(),
          "post_meta" => get_post_meta(get_the_ID());

    endwhile;


    echo  json_encode($data);
break;
}

在那里我想显示数据:

元数据具有相同的post id数据,

我想在内部细节中循环数据,请有人帮助我或告诉我需要改进哪些代码以便我的代码正常工作?

【问题讨论】:

  • 等等,所以你只是想把它作为 JSON 格式?
  • 确定..你能帮我吗?

标签: javascript php json wordpress


【解决方案1】:

元数据存储在 wp_postmeta 表中。使用 json_encode。

您可以通过$meta = get_post_meta( get_the_ID() );获取元数据

请检查以下代码。

if($loop->have_posts()) :

    $data = array( "api_status" => 1, "api_message" => "success");
    while ( $loop->have_posts() ) : $loop->the_post();

           $data[] = array("id" => get_the_ID(),
          "post_name" => get_the_title(),
          "post_meta" => get_post_meta(get_the_ID());

    endwhile;


    echo  json_encode($data);

【讨论】:

  • 当然,我知道,但我如何才能将数据作为 JSON 数据获取?我的意思是怎么做?详细介绍?
  • 我得到空白回复,你能检查一下我的代码有错吗?我更新我的问题.. 谢谢
【解决方案2】:

如果你只希望你的表数据为json,那么你只需要使用json_encode()

http://php.net/manual/en/function.json-encode.php

【讨论】:

    【解决方案3】:

    你可以试试这个

    if( $loop->have_posts() ) :
    
        $data = array( "api_status" => 1, "api_message" => "success");
        $meta = array();
        while ( $loop->have_posts() ) : $loop->the_post();
    
            $meta[] = array(
                "id"         => get_the_ID(),
                "post_name"  => get_the_title(),
                "_edit_lock" => get_post_meta( get_the_ID(), '_edit_lock', true ),
                "_edit_last" => get_post_meta( get_the_ID(), '_edit_last', true ),
                "username"   => get_post_meta( get_the_ID(), 'username', true ),
                "password"   => get_post_meta( get_the_ID(), 'password', true ),
                "email"      => get_post_meta( get_the_ID(), 'email', true ),
                "phone"      => get_post_meta( get_the_ID(), 'phone', true ),
            );
    
        endwhile;
    endif;
    
    echo  json_encode($meta);
    

    【讨论】:

    • 谢谢,可以问一下吗?我如何在其中给出一个参数(用于访问此数据)抱歉超出主题,参数the id post
    • 对不起,如果有误传,我的意思是当我想访问这些数据时,我需要先输入 id,
    • 我没听懂。您是否在询问如何访问 JSON 数据?或显示您尝试的示例代码也会有所帮助。
    • 它已经帮助了我,但是(显示有关 pos_type = 产品的数据)然后我想在我可以访问我需要输入 ID 的数据之前给出更多的特殊信息,例如:我输入 id:49 所以数据仅显示 id 为 49 的数据
    猜你喜欢
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多