?
Solved

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

Posted on 2011-11-01
5
Medium Priority
?
271 Views
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:

<?php

function recentnewsposts() {
    global $wpdb;
    global $post;
    require(TEMPLATEPATH . '/var.php'); ?>
   
                             <div class="technology">Latest news and events</div>
                             <div class="thelanguage">
                                   <ul>
                           
                                   <?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/www.picz.co.zm/news/">View all</a></li>
                                              </ul>
   
   
      </div>
 

   
   
   
    <?php
}


function widget_recentnewsposts($args) {
      extract($args, EXTR_SKIP);
      echo $before_widget;
      recentnewsposts();
      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'); ?>

</div>
 and have the structure in the code attached remain the same.



<?php
/*
Template Name: Homepage
*/

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

</div>


 <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>
</div>
<!-- Begin Content Area -->
<div id="content" class="clearfix">
<div id="features" class="clearfix">

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

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

<?php

get_footer(); ?>

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

Open in new window

0
Comment
Question by:kasondeneddy
  • 3
  • 2
5 Comments
 
LVL 31

Accepted Solution

by:
gwkg earned 2000 total points
ID: 37063354
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();  ?>
0
 

Author Comment

by:kasondeneddy
ID: 37064101
Hi,

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

Author Closing Comment

by:kasondeneddy
ID: 37064113
Worked perfectly
0
 
LVL 31

Expert Comment

by:gwkg
ID: 37071884
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.



0
 

Author Comment

by:kasondeneddy
ID: 37072920
Thanks, learn so much through EE :-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What's this? As a volunteer Page Editor for Experts-Exchange.com, I have noticed that many authors also have blogs, and pull articles from their blogs to post at Experts-Exchange. I appreciate each author sharing their blog content with our site …
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

829 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