Php 正在处理JSON解码以在get_posts()中使用
几天来,我一直在努力使用自定义字段中的数据返回帖子。这是我在functions.php中的函数。我可以返回我的帖子,只是它们不限于$json变量中定义的帖子。我可以解码json并返回数组…但我似乎无法将其转换为$disks_参数中的posts___正确填充我的数组 谁能帮我找出我做错了什么Php 正在处理JSON解码以在get_posts()中使用,php,json,wordpress,woocommerce,Php,Json,Wordpress,Woocommerce,几天来,我一直在努力使用自定义字段中的数据返回帖子。这是我在functions.php中的函数。我可以返回我的帖子,只是它们不限于$json变量中定义的帖子。我可以解码json并返回数组…但我似乎无法将其转换为$disks_参数中的posts___正确填充我的数组 谁能帮我找出我做错了什么 add_action ('woo_loop_before', 'hgf_home_menu'); function hgf_home_menu () { if (is_home() || is_front
add_action ('woo_loop_before', 'hgf_home_menu');
function hgf_home_menu () {
if (is_home() || is_front_page()) {
wp_reset_query();
global $posts;
$menu_args = array(
'post_type'=>'single_menu',
'posts_per_page' => 1,
'meta_key' => 'orderby_date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'orderby_date', // Order-By Date field is upcoming
'value' => date("Y-m-d"),
'compare' => '>='
),
array(
'key' => 'orderby_date', // Order-By Date isn't more than two weeks out
'value' => date("Y-m-d", strtotime( "+2 weeks")),
'compare' => '<='
)
),
);
// Get menu that meets criteria
$menus = new WP_Query( $menu_args );
// Show menu that meets criteria
if ( $menus->have_posts() ) {
while ( $menus->have_posts() ) {
$menus->the_post();
}
wp_reset_postdata();
// Get the menu's product/post listing
$json = '[{"id":"435"},{"id":"527"},{"id":"563"},{"id":"568"}]';
$array = json_decode($json);
$include = array();
foreach($array as $a) {
$include[] = $a->ID;
}
$args = array(
'posts_per_page' => -1,
'include' => $include);
$posts = get_posts($args);
$dish_args = array(
'post_type' => 'product',
'post__in' => $posts,
);
// Get dishes in menu listing
$dishes = get_posts( $dish_args );
if ($dishes) {
foreach ($dishes as $dish) {
}
}
} else { // no posts found }
/* Restore original Post Data */
wp_reset_postdata();
}
}
您的jSon属性是id而不是id
$include[] = $a->ID;
应该是
$include[] = $a->ID;
这里哪里/为什么有jSon?这是其他原因造成的吗。
否则,这可能只是一个简单的分隔列表,甚至是一个普通的PHP数组。我找到了它…结果我只是删掉了一堆阻碍它的代码:$include=array$args=数组$dish_args=数组$洗碗碟=发帖子。以下是更正部分:
$json = '[{"id":"435"},{"id":"527"},{"id":"563"},{"id":"568"}]';
$dishes = json_decode($json);
if ($dishes) {
foreach ($dishes as $dish) {
// Echo titles and permalinks
}
}
我试过$include[]=$a->id;结果也没有改变。是的,该字段收集与自定义帖子类型关联的帖子,并用以下内容填充:“[{id:435},{id:527},{id:563},{id:568}]”。我想知道如何分解它,以便调用这些帖子。谢谢你花时间回答。事实上,我已经想出了如何让它发挥作用……我想我已经发布了答案。它可以将$include=array的所有内容削减到$dishs=get_posts$dish_args,然后将$array更改为$dishs。我会发帖的。