切换到主页上的静态内容中断滑块

时间:2011-11-07 11:31:28

标签: wordpress

我正在开发一个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)并建议修复?

提前致谢!

1 个答案:

答案 0 :(得分:0)

乳清使用query_posts你改变查询而不是与它交互。当页面被设置为最新帖子时它工作的原因是因为$ post对象已经基于运行的主要WordPress查询循环进行了全球化。如果你调用global $ 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 );

这将为您提供$ post对象的内容以及当前查询中的内容。

相关问题