我正在运行一个post循环,其中有四个我需要关联的属性: title、date、link、location:
while (have_posts()) : the_post();
if( !in_array( $post->ID, $already_displayed_ids )) {
array_push( $thisPostid, $post->ID );
$title = get_the_title();
$location = usp_get_meta(false, 'usp-custom-8');
$link = get_permalink();
$contentYear = usp_get_meta(false, 'usp-custom-14');
if ($contentYear >= 0 && $contentYear <= 2019) {
array_push($yearsArray, $contentYear);
if (($wp_query->current_post +1) == ($wp_query->post_count)) {
$yearsArray = array_unique($yearsArray);
sort($yearsArray);
}
}
array_push( $already_displayed_ids, $post->ID );
}
endwhile;
基本上我正在运行一个循环,我在$already_displayed_ids
中检查非重复,我需要关联并推送我在这里推送的任何帖子的标题、位置、日期和链接array_push($yearsArray, $contentYear);
目前我只能推送$contentYear
,但没有link
、title
或location
与推送的帖子相关联,我想创建一个关联数组来推送每个帖子所需的所有规范。$contentYear
是一个带有日期的自定义字段,所以我使用这些值生成一个导航,它们是日期。但我需要关联到每个日期的标题,位置和链接,因为我目前不知道如何推动这些规格关联到我正在推动的时间。
发布于 2018-06-04 02:59:58
我是这样解决这个问题的:
while (have_posts()) : the_post();
if( !in_array( $post->ID, $already_displayed_ids )) {
array_push( $thisPostid, $post->ID );
$contentYear = usp_get_meta(false, 'usp-custom-14');
if ($contentYear >= 0 && $contentYear <= 2019) {
array_push($yearsArray,
array(
'year'=> $contentYear,
'link'=> get_permalink(),
'location'=> usp_get_meta(false, 'usp-custom-8'),
'title'=> get_the_title()
)
);
}
array_push( $already_displayed_ids, $post->ID );
}
endwhile;
然后我可以做foreach ($yearsArray as $year) {
和echo $year['title']
等
发布于 2018-06-04 01:19:26
据我所知,目前还没有从post中获取关联数组的直接功能。以下是您所要求的内容的一小段:
$posts = array();
if (have_post()) :
while (have_posts()) :
array_push($posts,
Array(
'id'=>the_ID(),
'quantity'=>1,
'size'=>$size,
'colour'=>$colour
)
);
endwhile;
$_SESSION['cart'] = $posts[0]; // change 0 index
endif;
请注意,上面的代码并不理想,您可能应该这样做。此外,您可能希望使用cookie,而不是会话。
https://stackoverflow.com/questions/50668929
复制相似问题