0

I am facing issue in debug mode with wordpress. In my featured slider getting error Notice: Object of class WP_Query could not be converted to int in on line

I am using below code for my slider

<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 are nothing but switching the class for style

--[ Resolved Code ]------------------------------------------------------------------- As I am new to php I didn't realized I am using two loop at the same time for and while. With Manny Fleurmond's comment my mind just clicked and I use below code and it just works fine with me. Also changed post_per_page option as m0r7if3r said. Please tell me if anything is wrong in this code.

$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
                ?>
3
  • 1
    Why do you have both a for loop and a while loop? You should need only the while loop. You can't iterate through a WP_Query object like that in a for loop. Commented Mar 5, 2012 at 23:31
  • 1
    You shouldn't be using showposts, it's WAY old. It has been replaced by posts_per_page. This is not your issue though.
    – mor7ifer
    Commented Mar 6, 2012 at 2:35
  • Thanks Manny and m0r7if3r. Your comment resolve my issue. It was really help to understand the issue very easy and simple language helps people like me :) Please check above edited code and let me know if anything still need to improve. Commented Mar 6, 2012 at 5:35

2 Answers 2

3

Your problem is here:

$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++) {

You're creating a new instance of WP_Query to run a custom query and storing it in the $featuredPosts variable. But then you're trying to use $featuredPosts as the upper limit of a for loop. This is what PHP is complaining about. Your $i<=$featuredPosts should be something more like $i<=count($featuredPosts->posts).

That said, you're using actual Loop functions below that ... which belong in a while($featuredPosts->have_posts()) loop instead of a regular for loop ...

3
  • 1
    Great minds (or, more accurately, warped minds that spend all their time thinking about WordPress) think alike! :) Commented Mar 5, 2012 at 20:37
  • Hmm as you said I should use for($i=1; $i<=get_option('mp_slides_no'); $i==) but this is repeating loop twice, however error has gone. and when I tried for($i=1; $i<=get_option('mp_slides_no'); $i==) giving me an error Parse error: syntax error, unexpected '(', expecting ';' in Commented Mar 5, 2012 at 21:37
  • I have solved the issue with Manny's comment. Thanks a lot for your help. I really appreciate Commented Mar 6, 2012 at 5:39
3

Try changing this:

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

...to this:

<?php
$featuredPosts_args = array(
    'showposts' => get_option( 'mp_slides_no' ),
    'category_name' => get_option('mp_featured_cat')
);
$featuredPosts = new WP_Query( $featuredPosts_args );
?>

Ultimately, I think this is your problem:

for($i=1; $i<=$featuredPosts; $i++)

Here, $featuredPosts is an object, not an integer. Either find another way to do what you're trying to do here, or get rid of this for loop entirely.

1
  • Nope it couldn't solve the issue. Is there any way to make it dynamic without for loop? I am not so experienced so need proper help form you guys.. Commented Mar 5, 2012 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.