gpt4 book ai didi

php - Get_post 不显示数据

转载 作者:搜寻专家 更新时间:2023-10-31 21:21:53 24 4
gpt4 key购买 nike

我有一个自定义的帖子类型推荐,我正在尝试显示标题、内容和特色图片。

这是我的代码:

    <?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"]=> 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." ["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" }

我做错了什么?

最佳答案

我知道您正在使用 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(); ?>

你也在关闭foreach里面<div>这会破坏你的 html代码。

关于php - Get_post 不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45808218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com