繁体   English   中英

打印出单个 div 中数组的 php 值

[英]print out php values of array in individual divs

我得到某个 wordpress post_type 的页面 id 并将它们存储在一个数组中,如下所示:

<?php $post_ids = get_posts(array(
    'fields'          => 'ids', // Only get post IDs
    'post_type' => 'projekt',
    'posts_per_page'  => -1
));
print_r($post_ids);
?>

数组如下所示: 数组 ( [0] => 9460 [1] => 8092 [2] => 7910 [3] => 7338 [4] => 6643 [5] => 4946 )

现在我正在使用这些数组值来获取每个页面的 id_value “projekt_name”,并且理想情况下希望在自己的 div 中回显它们,如下所示:

    echo '<ul class="display-posts-desktop-slider">';

foreach($post_ids as $id => $id_value) {
$other_page = $id_value;
$ergebnis = the_field('projekt_name', $other_page) ;

    echo '<div class="test"><li><?php' . $ergebnis . '?></li></div>';

}

    echo '</ul>';

我面临的问题是,为每一个都正确创建了一个列表项,但是变量 $ergebnis 中的值打印在列表之外,作为没有任何 class 等的纯文本。

问题:如何将每个值作为其自己的列表项回显,例如。 将它们放在 li 标签内...? 我希望我能描述发生了什么,并感谢您的时间和帮助。

干杯

看起来你有一个小错误,具体来说

echo '<div class="test"><li><?php'. $ergebnis. '?></li></div>';

您应该将此行更改为

echo "<div class='test'><li>$ergebnis</li></div>';"

我已经使用我自己的临时数组尝试了这条线并且它有效。

希望能帮助到你:)

我解决了它,我犯的错误是

the_field('projekt_name', $other_page) ;

自动打印结果。 所以

foreach($post_ids as $id => $id_value) {
$other_page = $id_value;


    echo '<div class="testtesttest">'; 

$ergebnis = the_field('projekt_name', $other_page) ;

echo '</div>';

现在将每个 $ergebnis 打印到自己的 div 中。

暂无
暂无

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

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