2011-11-07 84 views
0

我正在使用一个在每个页面上都有一个滑块的wordpress网站(但只有当我没有将网站设置为使用静态页面而不是我的帖子列表时在主页上) - 仍然和我在一起?切换到主页分页滑块上的静态内容

当主页(例如www.mysite.com)在主页上显示我的数据库中的帖子列表时,滑块工作正常,但是当我将主页更改为使用页面中的静态内容时,已创建,滑块消失。

下面的代码:

<?php get_header(); ?> 

<!-- begin featured-posts --> 
<div class="break"></div> 

<!-- begin featured --> 
<div id="featured"> 

    <?php 
    $tmp_query = $wp_query; 
    query_posts('showposts=3&cat=' . get_cat_ID(dp_settings('featured'))); 

    if (have_posts()) : 

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

    <!-- begin post --> 
    <div class="content"> 
     <a href="<?php the_permalink(); ?>"><?php dp_attachment_image($post->ID, 'alt="' . $post->post_title . '"'); ?></a>  
     <div class="title"> 
      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>    
     </div>   
     <p><?php echo dp_clean($post->post_content, 250); ?> <br /><br /> [<a class="readmore" href="#">Read More</a>]</p>    
    <div class="break"></div> 
    </div> 
    <!-- end post --> 

    <?php endwhile; endif; ?> 

</div> 
<!-- end featured --> 


    <!-- BEGIN content --> 
<div id="content"> 
    <?php 
    $wp_query = $tmp_query; 
    if (have_posts()) : 
    while (have_posts()) : the_post(); 
    ?> 

    <!-- begin post --> 
    <div class="post"> 
     <a href="<?php the_permalink(); ?>"><?php dp_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '"'); ?></a> 
     <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> 
     <p><?php echo dp_clean($post->post_content, 350); ?><br /><br />[<a class="readmore" href="#">Read More</a>]</p>    

        </div> 
    <!-- end post --> 

    <div class="break"></div> 

    <?php endwhile; ?> 
    <div class="postnav"> 

     <?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?> 

    </div> 
    <?php else : ?> 
    <div class="notfound"> 
    <h2>Not Found</h2> 
    <p>Sorry, but you are looking for something that is not here.</p> 
    </div> 
    <?php endif; ?> 

</div> 
<!-- END content --> 

<?php get_sidebar(); get_footer(); ?> 

所以,现在我看到,在<div id="featured">(这是滑块),$wp_query被保存在$tmp_query,然后在该网页的主要内容后重新使用。 因为我在主页上设置了静态内容,所以滑块不会显示在我网站的任何页面上。

我想要做的是在我的网站的每个页面上显示滑块,而不管该页面的内容是后置类型还是页面类型。 任何人都可以解释为什么页面类型的内容隐藏滑块(我能想到的是,if (have_posts()) :返回false),并建议修复?

在此先感谢!

回答

0

乳清使用query_posts您更改查询,而不是与它进行交互。当页面被设置为最新帖子时它工作的原因,因为$ post对象已经基于主要的WordPress查询循环运行而全球化。如果调用全局$ post,您的方法可能会工作,但更好的解决方案是对不改变主查询的精选滑块运行新查询。

<!-- begin featured --> 
<div id="featured"> 

    <?php 
    $cat_id = get_cat_ID(dp_settings('featured')); 
    $args = array(
     'cat' => $cat_id, 
     'posts_per_page' => 3 

    ); 

    $feature_query = new WP_Query($args); 


    while ($feature_query->have_posts()) : $feature_query->the_post(); ?> 


    <!-- begin post --> 
    <div class="content"> 
     <a href="<?php the_permalink(); ?>"><?php dp_attachment_image($post->ID, 'alt="' . $post->post_title . '"'); ?></a>  
     <div class="title"> 
      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>    
     </div>   
     <p><?php echo dp_clean($post->post_content, 250); ?> <br /><br /> [<a class="readmore" href="#">Read More</a>]</p>    
    <div class="break"></div> 
    </div> 
    <!-- end post --> 

    <?php endwhile; wp_reset_postdata(); ?> 

</div> 
<!-- end featured --> 

测试为什么任何查询不工作的一个好方法是使用var_dump();

例子:

$wp_query = $tmp_query; 
    var_dump($tmp_query); 
if (have_posts()) : 
while (have_posts()) : the_post(); 
global $post; 
var_dump($post); 

这会给你的$职位对象在内容和什么当前查询。