php WordPress 中的 foreach

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10734804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:54:38  来源:igfitidea点击:

foreach in wordpress

phpwordpress

提问by MariaZ

I am having some problems to make the following code to work in my side bar of wordpress:

我在使以下代码在我的 wordpress 侧栏中工作时遇到了一些问题:

            $the_query = get_posts( $args );


            $year_post = 0;
            foreach ($the_query as $post) : 
                setup_postdata($post);
                $current_year = get_the_time('Y'); 
                if( $current_year != $year_post ) {
                    $year_post = $current_year;
                    echo '<li><a href="'.get_year_link($current_year).'">'.$current_year.'</a></li>';
                }
                $month_post = 0;
                foreach ($post as $post_monthly) : 
                       setup_postdata($post_monthly);  
                       $current_month = get_the_time('m');
                        if ($current_month != $month_post){
                               $month_post =$current_month;
                               echo '<li><a href="'.get_year_link($current_month).'">'.$current_month.'</a></li>';
                        }

                        foreach ($post_monthly as $post_title) : 
                                setup_postdata($post_title);

                                echo '<li><a href="'.get_permalink().'">'.the_title().'</a></li>';
                        endforeach;

                endforeach;

            endforeach;

            wp_reset_postdata();
            $the_query = get_posts( $args );


            $year_post = 0;
            foreach ($the_query as $post) : 
                setup_postdata($post);
                $current_year = get_the_time('Y'); 
                if( $current_year != $year_post ) {
                    $year_post = $current_year;
                    echo '<li><a href="'.get_year_link($current_year).'">'.$current_year.'</a></li>';
                }
                $month_post = 0;
                foreach ($post as $post_monthly) : 
                       setup_postdata($post_monthly);  
                       $current_month = get_the_time('m');
                        if ($current_month != $month_post){
                               $month_post =$current_month;
                               echo '<li><a href="'.get_year_link($current_month).'">'.$current_month.'</a></li>';
                        }

                        foreach ($post_monthly as $post_title) : 
                                setup_postdata($post_title);

                                echo '<li><a href="'.get_permalink().'">'.the_title().'</a></li>';
                        endforeach;

                endforeach;

            endforeach;

            wp_reset_postdata();

With this code I am trying to display the following:

使用此代码,我尝试显示以下内容:

  • 2012
    -May
    • Lorem ipsum
    • Lorem ipsum
    • Lorem ipsum
  • 2012年
    -5月
    • 逻辑推理
    • 逻辑推理
    • 逻辑推理

It display 2012, May, but when it comes to display the titles of the post I am getting the following error: "Warning: Invalid argument supplied for foreach() "

它显示 2012 年 5 月,但在显示帖子标题时,我收到以下错误:“警告:为 foreach() 提供的参数无效”

I have checked so many times this code, I just don't seem to see the problem.

我已经检查了很多次这段代码,我似乎没有看到问题。

Can anyone see something that I am missing, It will be highly appreciated. Thanks!

谁能看到我遗漏的东西,将不胜感激。谢谢!

回答by tere?ko

You should check if $post_monthlyand $post_titleare actually arrays. You could do that by using var_dump().

您应该检查$post_monthly$post_title实际上是否为数组。你可以通过使用来做到这一点var_dump()

It is quite possible that in some month, there are no posts, then the value of variable might be null(i am not an expert in wordpress, but that's what would explain the error message). You could mitigate it by having a condition with !empty()or is_array()before the loop.

很可能在某个月没有帖子,那么变量的值可能是null(我不是 wordpress 专家,但这可以解释错误消息)。您可以通过在循环之前!empty()is_array()之前设置条件来缓解它。

回答by Okonomiyaki3000

The return value of get_posts()is an array of objects. So then, your $postcontains an object, not an array. http://codex.wordpress.org/Template_Tags/get_posts

的返回值get_posts()是一个对象数组。那么,你$post包含一个对象,而不是一个数组。 http://codex.wordpress.org/Template_Tags/get_posts

回答by Okonomiyaki3000

add the (array) before $arrays:

在 $arrays 之前添加(数组):

foreach ((array)$post...

to ignore empty ones and warnings like this

忽略像这样的空的和警告

回答by sankar

for Example:

例如:

<?php   

    $args = array(

    'post_type' => 'art',

    'post_status' => 'publish',

    'posts_per_page' => -1,

    ); ?>
    <?php $my_posts = get_posts( $args );

    foreach($my_posts as $my_post){ ?>
    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id($my_post->ID),array(500,500)); ?>
        <div class="view_list">
        <div class="flt1 ">  <img src="<?php print $image[0]; ?> "></div>   

        <ul>
            <li> <? echo  $my_post->post_title; ?> </li>
            <li> <? echo the_time('l, F j, Y'); ?> </li>
            <?php $terms = wp_get_post_terms( $my_post->ID, array( 'act', 'subject' ) ); ?>
            <? // print_r ( $terms); ?>

            <img src="<?php echo $url[0]; ?>"  alt="" />
            <li><?php foreach ( $terms as $term ) : ?>
            <? $term_link = get_term_link( $term ); ?>
            <?php //echo $term->taxonomy; ?><a href="<? echo $term_link  ?>"><?php echo $term->name; ?></a> 
            <?php endforeach; ?></li>   
            <?php echo get_field('Height', $my_post->ID) .'*' .get_field('width', $my_post->ID) ;  ?></li>
        </ul>

        </div>  <? }?>