【问题标题】:Get_post not displaying dataGet_post 不显示数据
【发布时间】: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_titleget_the_content,而且您没有在正确的位置关闭testimonial-contact div。

标签: php wordpress


【解决方案1】:

我知道您使用的是get_posts,但这是我要做的,查询帖子并使用wordpress 循环而不是使用foreach 循环。

试试这个

<?php
    query_posts(array(
        'post_type' => 'testimonial',
        'posts_per_page' => -1,
    ));
    if (have_posts()) : while (have_posts()) : the_post(); ?>
     <div class="testimonial-box projectitem">
         <?php echo wp_get_attachment_image( get_post_thumbnail_id( $post->ID ), '', 'false', array( "class" => "img-responsive" ) ); ?>
     </div>
    <div class="testimonial-contact">
        <div class="testimonial-text"><?php the_content();?></div>
        <div class="testimonial-name"><?php the_title(); ?></div>

    </div>
<?php endwhile; endif;
      wp_reset_postdata();
      wp_reset_query(); ?>

你也在 &lt;div&gt; 内关闭 foreach,这会破坏你的 html 代码。

【讨论】:

  • 我遇到了一个问题,但是当我再添加一个 xtra 推荐时,网站会中断并且推荐会显示出来。此代码适用于要显示的单个推荐.. 为什么会这样
  • 尝试将posts_per_page 设置为某个值并检查它是否有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 2021-02-01
  • 1970-01-01
相关资源
最近更新 更多