我在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
?>
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
完全循环。