WordPress:注意:无法在On Line中将WP_QUERY类的对象转换为int

时间:2012-03-05 作者:pixelngrain

我在wordpress的调试模式下面临问题。在我的特色滑块中获取错误Notice: Object of class WP_Query could not be converted to int in on line

我正在为我的滑块使用以下代码

<div id="<?php slider_class() //Theme function ?>" class="clearfix">

    <!--[BEGIN: Slider wrapper]-->
    <div id="slides-wrapper" <?php slider_wrapper_class() //Theme function ?>>
        <!--[BEGIN: #Slides]-->
        <div id="slides" class="<?php slides_class() //Theme function ?> clearfix">
            <!--[BEGIN: Slides_container]-->
            <div class="slides_container <?php slides_class() //Theme function ?>">

                <?php
                $featuredPosts = new WP_Query();
                $featuredPosts->query(\'showposts=\'.get_option(\'mp_slides_no\').\'&category_name=\'.get_option(\'mp_featured_cat\').\'\');

                for($i=1; $i<=$featuredPosts; $i++) { // second for() loop for post slides
                while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); // loop for posts
                ?>

                <?php if (get_option(\'mp_slides_content\') === "content_s") { ?>
                <!--[BEGIN: ALL SLIDES ++]-->
                <div class="slide clearfix">
                    <!-- thumbnail start -->
                    <?php mp_thumb(\'featured-thumbnail\',\'featured-slider-thumbnail\',\'480\'); //Theme function  ?>
                    <!-- end of thumbnail -->

                    <div class="featured-excerpt">
                        <h2 class="title"><a href="<?php the_permalink(); ?>"><?php echo substr($post->post_title,0,50); // short title ?>...</a></h2>

                        <?php the_excerpt(); ?>
                        <br />
                        <a href="<?php the_permalink(); ?>" class="featured-readmore clearfix"><span>Read more</span><img src="<?php bloginfo(\'template_directory\'); ?>/images/button-arrow.png" alt="readmore" width="24" height="24" /></a>
                    </div>

                </div>
                <!--[END: ALL SLIDES ++]-->

                <?php } elseif (get_option(\'mp_slides_content\') === "image_s") { ?>

                <!--[image slider start]-->

                <!--[BEGIN: ALL SLIDES ++]-->
                <div class="slide clearfix">
                    <!-- thumbnail start -->
                    <?php if (get_option(\'mp_slides_style\') === "fixed") { ?>

                        <?php mp_thumb(\'featured-thumbnail\',\'featured-slider-image920\',\'920\'); //Theme function  ?>

                    <?php } else { ?>

                        <?php mp_thumb(\'featured-thumbnail\',\'featured-slider-image960\',\'960\'); //Theme function  ?>

                    <?php } ?>
                    <!-- end of thumbnail -->

                </div>
                <!--[END: ALL SLIDES ++]-->             

                <!--[end of image slider]-->

                <?php } ?>

                <?php endwhile;
                } // end for() loop number 2
                ?>

            </div>
            <!--[END: Slides_container]-->

            <a href="#" class="prev"></a>
            <a href="#" class="next"></a>

        </div>
        <!--[END: #Slides]-->

    </div>
    <!--[END: Slider wrapper]-->

</div>
//theme function 只不过是换了一种风格

--[解析代码]----------------------------------------------------------------------------由于我是php新手,我没有意识到我在和同时使用了两个循环。有了Manny Fleurmond的评论,我的大脑刚刚点击,我使用了下面的代码,它对我来说很好。正如m0r7if3r所说,还更改了post\\u per\\u page选项。请告诉我此代码是否有任何错误。

$temp = $wp_query;
                $wp_query = null;
                $wp_query = new WP_Query();
                $wp_query->query(\'category_name=\'.get_option(\'mp_featured_cat\').\'&posts_per_page=\'.get_option(\'mp_slides_no\').\'\');          

                while ( $wp_query->have_posts() ) : $wp_query->the_post(); // loop for posts
                ?>

2 个回复
SO网友:EAMann

您的问题在于:

$featuredPosts = new WP_Query();
$featuredPosts->query(\'showposts=\'.get_option(\'mp_slides_no\').\'&category_name=\'.get_option(\'mp_featured_cat\').\'\');

for($i=1; $i<=$featuredPosts; $i++) {
您正在创建的新实例WP_Query 运行自定义查询并将其存储在$featuredPosts 变量但是你想用$featuredPosts 作为for循环的上限。这就是PHP所抱怨的。你的$i<=$featuredPosts 应该更像$i<=count($featuredPosts->posts).

也就是说,您正在使用下面的实际循环函数。。。属于while($featuredPosts->have_posts()) 循环而不是常规的for循环。。。

SO网友:Chip Bennett

尝试更改此选项:

<?php
$featuredPosts = new WP_Query();
$featuredPosts->query(\'showposts=\'.get_option(\'mp_slides_no\').\'&category_name=\'.get_option(\'mp_featured_cat\').\'\');
。。。为此:

<?php
$featuredPosts_args = array(
    \'showposts\' => get_option( \'mp_slides_no\' ),
    \'category_name\' => get_option(\'mp_featured_cat\')
);
$featuredPosts = new WP_Query( $featuredPosts_args );
?>
归根结底,我认为这是你的问题:

for($i=1; $i<=$featuredPosts; $i++)
在这里,$featuredPosts 是一个object, 不是一个integer. 要么找到另一种方法来做你想做的事,要么摆脱这个for 完全循环。

结束

相关推荐

How do you debug plugins?

我对插件创作还很陌生,调试也很困难。我用了很多echo,它又脏又丑。我确信有更好的方法可以做到这一点,也许是一个带有调试器的IDE,我可以在其中运行整个站点,包括插件?