【发布时间】:2018-01-30 04:25:20
【问题描述】:
我有一个自定义的帖子类型推荐,并正在尝试显示标题、内容和特色图片。
这是我的代码:
<?php
$args = array('posts_per_page' => -1, 'post_status' => 'publish', 'post_type' => 'testimonial');
$testimonialsposts = get_posts($args);
?>
<div class="row">
<?php
foreach ($testimonialsposts as $post)
{
setup_postdata($post);
?>
<div class="testimonial-box projectitem">
<img src="<?= get_post_meta($post->ID, '_wp_attached_file', true); ?>"/>
</div>
<div class="testimonial-contact">
<div class="testimonial-text"><?= get_post($post->ID, 'content', true); ?></div>
<div class="testimonial-name"><?= get_post($post->ID, 'post_title', true); ?></div>
<?php wp_reset_postdata();
} ?>
</div>
但我什么也得不到。当我尝试在 setup_postdata 之后回显详细信息时,它只回显“数组”。
当我对 $post 进行 var_dump 时,它也会显示所有数据。
这就是我var_dump$post时得到的结果
object(WP_Post)#614 (24) { ["ID"]=> int(69) ["post_author"]=> 字符串(1)“1”[“post_date”]=>字符串(19)“2017-08-22 02:26:43” ["post_date_gmt"]=> 字符串(19) "2017-08-22 02:26:43" ["post_content"]=> string(575) "Lorem Ipsum 只是 印刷和排版行业。 Lorem Ipsum 一直是 自 1500 年代以来的行业标准虚拟文本,当一个未知的 打印机拿了一个类型的厨房并将其打乱以制作一个类型样本 书。它不仅经历了五个世纪,而且跨越了 电子排版,基本保持不变。它是 1960 年代随着 Letraset 床单的发布而普及 包含 Lorem Ipsum 段落,最近还有桌面 发布软件,如 Aldus PageMaker,包括 Lorem 版本 Ipsum。" ["post_title"]=> 字符串(13) "testimonial_1" ["post_excerpt"]=> 字符串(0)“”[“post_status”]=>字符串(7)“发布” ["comment_status"]=> 字符串(6) "关闭" ["ping_status"]=> 字符串(6) “关闭” [“post_password”]=> 字符串(0)“” [“post_name”]=> 字符串(11) “推荐”[“to_ping”]=>字符串(0)“”[“pinged”]=>字符串(0)“” ["post_modified"]=> 字符串(19) "2017-08-22 02:27:07" [“post_modified_gmt”]=> 字符串(19)“2017-08-22 02:27:07” ["post_content_filtered"]=> 字符串(0) "" ["post_parent"]=> int(0) ["guid"]=> 字符串(63) "http://192.168.0.202/clinic202/?post_type=testimonial&p=69" ["menu_order"]=> int(0) ["post_type"]=> string(11) "testimonial" ["post_mime_type"]=> 字符串(0) "" ["comment_count"]=> 字符串(1) "0" [“过滤器”]=> 字符串(3)“原始” }
我做错了什么?
【问题讨论】:
-
您可以使用
print_r()代替echo()来显示数组的内容。如果这没有帮助,请更新您的帖子以说明数组是否为空或其中包含的内容 -
@FluffyKitten 我更新了我的 qn
-
您不需要发布全部内容,只需让我们知道它正在运行 :-) 好的,所以您的查询正在运行,但是您的循环全错了 - 您使用的是
get_post出于某种原因,而不仅仅是使用get_the_title或get_the_content,而且您没有在正确的位置关闭testimonial-contactdiv。