0

我在使以下代码在我的 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();

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

  • 2012年
    -5月
    • Lorem ipsum
    • Lorem ipsum
    • Lorem ipsum

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

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

任何人都可以看到我缺少的东西,将不胜感激。谢谢!

4

4 回答 4

2

例如:

<?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>  <? }?>
于 2014-12-24T14:20:33.717 回答
2

在 $arrays 之前添加 (array):

foreach ((array)$post...

忽略空的和这样的警告

于 2012-05-24T10:07:16.923 回答
2

您应该检查是否$post_monthly$post_title实际上是数组。您可以使用var_dump().

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

于 2012-05-24T09:41:34.400 回答
1

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

于 2012-05-24T09:44:49.067 回答