Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从多维数组实现WP_Post对象_Php_Arrays_Wordpress_Multidimensional Array - Fatal编程技术网

Php 如何从多维数组实现WP_Post对象

Php 如何从多维数组实现WP_Post对象,php,arrays,wordpress,multidimensional-array,Php,Arrays,Wordpress,Multidimensional Array,我很难从数组中提取WP_post对象的post_标题,该数组位于数组的数组中 我已经创建了帖子类型和自定义字段,这样我就可以吃“饭”,里面有“配料”本身 根据本例,最终结果应为: 香肠和土豆泥 香肠 土豆 这是我的代码: <?php // Create our arguments for getting our post $args = array( 'post_type'=> 'meals', 'orderby' => 'title', 'order' =>

我很难从数组中提取WP_post对象的post_标题,该数组位于数组的数组中

我已经创建了帖子类型和自定义字段,这样我就可以吃“饭”,里面有“配料”本身

根据本例,最终结果应为:

香肠和土豆泥

香肠

土豆

这是我的代码:

<?php

// Create our arguments for getting our post
$args = array(
  'post_type'=> 'meals',
  'orderby' => 'title',
  'order' => 'ASC',
  'posts_per_page' => -1,
  'nopaging' => true,
);

$query = new WP_Query( $args) ;

if ( $query->have_posts() ) { ?>
        <ul>
          <?php 
    while ( $query->have_posts() ) {
        $query->the_post(); ?>
          
          <li><?php the_title(); ?></li>
          <ul>
          <li>
          
          <?php 
    
          $ingredients = get_field('ingredients', $post);
          echo "<pre>";
          print_r ($ingredients); //output array
          echo "</pre>";
          
          echo "<ul>";
          foreach ( $ingredients['post_title'] as $value => $ingredient ): 
             echo "<li>" . $ingredient . "</li>";
          endforeach;
          echo "</ul>";
          
          ?>

          </li>
          </ul>
          
          <?php } ?>
        </ul>
        <?php }
wp_reset_postdata();
?>

因此,如果信息在那里,为什么我的foreach$components作为$component不显示代码

我真的很感谢你们的帮助,因为我花了整个下午的时间在这里,参考了许多关于StackOverflow的文章,Wordpress网站和许多独立开发者的博客。我知道我遗漏了一些东西,这是因为它是数组或数组中的一个post对象,尽管在代码中使用了各种变体,但我仍然无法破解它

你能帮忙吗


多谢

多亏了霍华德的评论,我更仔细地研究了这个特殊的程序,并制定了它。我没有深入研究post对象的数组,修复方法很简单,只需将[0]添加到echo即可:

Array
(
    [0] => Array
        (
            [ingredient] => Array
                (
                    [0] => WP_Post Object
                        (
                            [ID] => 491
                            [post_author] => 1
                            [post_date] => 2020-10-28 17:46:10
                            [post_date_gmt] => 2020-10-28 17:46:10
                            [post_content] => 
                            [post_title] => Sausages
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => open
                            [ping_status] => open
                            [post_password] => 
                            [post_name] => sausages
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2020-10-28 17:46:10
                            [post_modified_gmt] => 2020-10-28 17:46:10
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => https://www.website.com/home/ingredients/sausages/
                            [menu_order] => 0
                            [post_type] => ingredients
                            [post_mime_type] => 
                            [comment_count] => 0
                            [filter] => raw
                        )

                )

            [quantity] => 1
        )

    [1] => Array
        (
            [ingredient] => Array
                (
                    [0] => WP_Post Object
                        (
                            [ID] => 448
                            [post_author] => 1
                            [post_date] => 2020-10-28 09:23:52
                            [post_date_gmt] => 2020-10-28 09:23:52
                            [post_content] => 
                            [post_title] => Potatoes
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => potatoes
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2020-10-28 10:57:21
                            [post_modified_gmt] => 2020-10-28 10:57:21
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => https://www.website.com/home/?post_type=ingredients&p=448
                            [menu_order] => 0
                            [post_type] => ingredients
                            [post_mime_type] => 
                            [comment_count] => 0
                            [filter] => raw
                        )

                )

            [quantity] => 1
        )

)
echo“
  • ”$成分['Component'][0]->文章标题。“
  • ”;
    Array
    (
        [0] => Array
            (
                [ingredient] => Array
                    (
                        [0] => WP_Post Object
                            (
                                [ID] => 491
                                [post_author] => 1
                                [post_date] => 2020-10-28 17:46:10
                                [post_date_gmt] => 2020-10-28 17:46:10
                                [post_content] => 
                                [post_title] => Sausages
                                [post_excerpt] => 
                                [post_status] => publish
                                [comment_status] => open
                                [ping_status] => open
                                [post_password] => 
                                [post_name] => sausages
                                [to_ping] => 
                                [pinged] => 
                                [post_modified] => 2020-10-28 17:46:10
                                [post_modified_gmt] => 2020-10-28 17:46:10
                                [post_content_filtered] => 
                                [post_parent] => 0
                                [guid] => https://www.website.com/home/ingredients/sausages/
                                [menu_order] => 0
                                [post_type] => ingredients
                                [post_mime_type] => 
                                [comment_count] => 0
                                [filter] => raw
                            )
    
                    )
    
                [quantity] => 1
            )
    
        [1] => Array
            (
                [ingredient] => Array
                    (
                        [0] => WP_Post Object
                            (
                                [ID] => 448
                                [post_author] => 1
                                [post_date] => 2020-10-28 09:23:52
                                [post_date_gmt] => 2020-10-28 09:23:52
                                [post_content] => 
                                [post_title] => Potatoes
                                [post_excerpt] => 
                                [post_status] => publish
                                [comment_status] => closed
                                [ping_status] => closed
                                [post_password] => 
                                [post_name] => potatoes
                                [to_ping] => 
                                [pinged] => 
                                [post_modified] => 2020-10-28 10:57:21
                                [post_modified_gmt] => 2020-10-28 10:57:21
                                [post_content_filtered] => 
                                [post_parent] => 0
                                [guid] => https://www.website.com/home/?post_type=ingredients&p=448
                                [menu_order] => 0
                                [post_type] => ingredients
                                [post_mime_type] => 
                                [comment_count] => 0
                                [filter] => raw
                            )
    
                    )
    
                [quantity] => 1
            )
    
    )
    
    就这样,它起作用了


    谢谢。

    可能需要
    get_字段('components',$post->ID)
    ,除非“components”是ACF字段中的post对象?您在foreach循环中运行的是什么?这一部分也有点混乱。@HowardE-谢谢你,你的评论帮助我找到了答案。