Problem with wordpress, displaying post, instead of sidebar widgets are appearing before if(have_posts()): out put

Posted on 2011-11-01
Last Modified: 2012-05-12
Hi Experts,

I am having trouble with a page template, my page is displaying posts instead of the wordpress page, i believe its a loop issue.  Cause when I disable a widget that displays news it displays fine.  It also works fine when I change the order of the page, and place sidebar() code below content.  right now the sidebar(0 appears above, as you can see in code attached.  My widget that is affecting things, right_news_widget.php has the following code:


function recentnewsposts() {
    global $wpdb;
    global $post;
    require(TEMPLATEPATH . '/var.php'); ?>
                             <div class="technology">Latest news and events</div>
                             <div class="thelanguage">
                                   <?php query_posts("cat=-$ts_portfolio_cat&showposts=3&caller_get_posts=1");
      while(have_posts()) : the_post();  ?>
            <li><a href="<?php the_permalink(); ?>"><strong><?php the_title(); ?></strong></a></li>
      <?php endwhile; ?>
        <li><a href="http://localhost/">View all</a></li>


function widget_recentnewsposts($args) {
      extract($args, EXTR_SKIP);
      echo $before_widget;
      echo $after_widget;

register_sidebar_widget('Recent News Posts', 'widget_recentnewsposts');

What must I do to keep the strcuture of the page the same, but have both loops working, one that display the news as defined in
<div id="news">
<?php get_sidebar('newsevents'); ?>

 and have the structure in the code attached remain the same.

Template Name: Homepage

get_header(); ?>
<div id="newpanels">
<?php get_sidebar(); ?>
<?php get_sidebar('insurancep'); ?>
<div id="news">
<?php get_sidebar('newsevents'); ?>


 <span  class='st_twitter_vcount' displayText='Tweet'></span><span  class='st_email_vcount' displayText='Email'></span><span  class='st_facebook_vcount' displayText='Facebook'></span><span  class='st_sharethis_vcount' displayText='ShareThis'></span>
<!-- Begin Content Area -->
<div id="content" class="clearfix">
<div id="features" class="clearfix">

if(have_posts()) : while(have_posts()) : the_post (); ?>

    <?php the_content(); ?>
   <?php  endwhile; endif; ?>
<!-- End of Content Area -->
<script type="text/javascript">
                        var countries=new ddtabcontent("picztabs")
                        countries.setselectedClassTarget("link") //"link" or "linkparent"
<!-- Start Footer Area including accordion code -->


get_footer(); ?>

<!-- End of Footer Area including accordion code -->

Open in new window

Question by:kasondeneddy
    LVL 31

    Accepted Solution

    Instead of using query_posts, create a new query object

    $my_query = new WP_Query( 'cat=-$ts_portfolio_cat&showposts=3&caller_get_posts=1' );
    while( $my_query->have_posts() ) : $my_query->the_post();  ?>

    Author Comment


    Thanks that solved my problem :-)  Can you explain what caused the issue?

    Author Closing Comment

    Worked perfectly
    LVL 31

    Expert Comment

    Thanks that solved my problem :-)  Can you explain what caused the issue?
    You can run into problems with WordPress when you try to run multiple loops on the same page using query_posts because WordPress only pulls one group of posts from the database by default.

    So when you try to use query_posts again you aren't actually pulling a new group of posts from the database.  You need to create a new query object for that.


    Author Comment

    Thanks, learn so much through EE :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    WordPress has made it easy to create scalable sites based on their powerful CMS, but as great as WordPress is, there are some SEO issues that haven’t been addressed out of the box.  Fortunately these problems can be solved with a few plugins.  The f…
    How to install BuddyPress on your self-hosted WordPress site It’s been called everything from “social networking in a box” to “Facebook without the terms of service,” but until Feb. 16, BuddyPress was a relatively unknown outside the WordPress MU…
    The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
    The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now