2011-11-07 87 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); 

這會給你的$職位對象在內容和什麼當前查詢。