简体   繁体   English

Get_post不显示数据

[英]Get_post not displaying data

I have a custom post type testimonial and am trying to display the title, content and the featured image. 我有一个自定义的帖子类型推荐,我试图显示标题,内容和特色图像。

This is my code: 这是我的代码:

    <?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>

But I am getting nothing. 但我一无所获。 When I tried to echo the details after setup_postdata its only echoing "array". 当我试图在setup_postdata之后回显细节时,它只回显“数组”。

When I var_dump the $post it shows all the data also. 当我var_dump $ post时,它也会显示所有数据。

This is what i get when I var_dump $post 这是我在var_dump $post时得到$post

object(WP_Post)#614 (24) { ["ID"]=> int(69) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2017-08-22 02:26:43" ["post_date_gmt"]=> string(19) "2017-08-22 02:26:43" ["post_content"]=> string(575) "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." object(WP_Post)#614(24){[“ID”] => int(69)[“post_author”] => string(1)“1”[“post_date”] => string(19)“2017-08 -22 02:26:43“[”post_date_gmt“] => string(19)”2017-08-22 02:26:43“[”post_content“] => string(575)”Lorem Ipsum只是虚拟文本印刷和排版行业。自16世纪以来,Lorem Ipsum一直是业界标准的虚拟文本,当时一个未知的打印机拿了一个类型的厨房,并拼写它制作一个类型的标本书。它不仅存活了五个世纪,而且还存在20世纪60年代推出了包含Lorem Ipsum通道的Letraset板材,最近推出了包括Lorem Ipsum版本在内的Aldus PageMaker等桌面出版软件,推动了电子排版的发展。 ["post_title"]=> string(13) "testimonial_1" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(11) "testimonial" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2017-08-22 02:27:07" ["post_modified_gmt"]=> string(19) "2017-08-22 02:27:07" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(63) " http://192.168.0.202/clinic202/?post_type=testimonial&p=69 " ["menu_order"]=> int(0) ["post_type"]=> string(11) "testimonial" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [“post_title”] => string(13)“testimonial_1”[“post_excerpt”] => string(0)“”[“post_status”] => string(7)“publish”[“comment_status”] => string( 6)“closed”[“ping_status”] => string(6)“closed”[“post_password”] => string(0)“”[“post_name”] => string(11)“testimonial”[“to_ping” ] => string(0)“”[“pinged”] => string(0)“”[“post_modified”] => string(19)“2017-08-22 02:27:07”[“post_modified_gmt”] => string(19)“2017-08-22 02:27:07”[“post_content_filtered”] => string(0)“”[“post_parent”] => int(0)[“guid”] => string (63)“ http://192.168.0.202/clinic202/?post_type=testimonial&p=69 ”[“menu_order”] => int(0)[“post_type”] => string(11)“testimonial”[“post_mime_type” ] => string(0)“”[“comment_count”] => string(1)“0”[“filter”] => string(3)“raw”}

What am I doing wrong? 我究竟做错了什么?

I understand you are using get_posts but this is what i would do, query the posts and loop by using wordpress loop instead using foreach . 我知道你正在使用get_posts但这就是我要做的,通过使用wordpress循环而不是使用foreach查询帖子和循环。

try this 尝试这个

<?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(); ?>

also you are closing foreach inside <div> which will break your html code. 你也在关闭<div>里面的foreach ,这会打破你的html代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM