在多个页面上使用此帖子类别查询有问题吗?

时间:2012-06-23 作者:Matt

我的网站上有三个页面(Listen、Read、Watch),每个页面分别查询不同的帖子类别:“Listen”、“Read”和“Watch”。我创建了三个单独的php文件,它们有自己独特的模板名称,我在wp admin中创建了一个页面,并为每个文件选择了适当的模板。我多次使用下面的查询时遇到问题。例如,Listen页面将通过分页成功查询“Listen”类别;但是,我无法在其他两页上使用相同的代码。

            <?php
            /*
            Template Name: Listen
            */
            ?>

            <!-- header.php -->
            <?php get_header(); ?>
            <!-- /header.php -->

            <!-- 960 16 Column Grid -->
            <div class="container_16"> 

                <!-- Featured News -->
                <section class="grid_10 read">

                    <!-- Featured News Heading -->
                    <h1></h1>
                    <!-- /Featured News Heading -->

                        <!-- Featured News Loop -->
                        <?php
                            $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
                                $args=array(
                                    \'post_type\'=>\'post\',
                                    \'cat\' => \'listen\',
                                    \'posts_per_page\' => 5,
                                    \'paged\'=>$paged
                                );
                                $temp = $wp_query;
                                $wp_query= null;
                                $wp_query = new WP_Query($args);

                            if (function_exists(\'wp_pagenavi\')) { wp_pagenavi(); }
                            if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
                        ?> 

                        <!-- Post --> 
                        <article class="post">

                            <!-- Featured News Title -->
                            <span><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'%s\', \'twentyten\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                            </span>
                            <!-- /Featured News Title -->

                            <!-- Featured News Meta -->
                            <p class="featured-news-post-meta">By <span class="featured-news-author"><?php echo get_the_author(); ?></span> / <?php echo get_the_date(\'d.m.Y\'); ?>
                            </p>
                            <!-- /Featured News Meta -->

                            <!-- Featured News Thumbnail -->
                            <?php the_post_thumbnail(array(640,320)); ?>
                            <!-- /Featured News Thumbnail -->

                            <!-- Featured News Excerpt -->
                            <p class="featured-news-excerpt"><?php the_excerpt(); ?></p>
                            <!-- /Featured News Excerpt -->

                            <!-- Featured News Social Links -->
                            <?php include(\'includes/social.php\'); ?>
                            <!-- /Featured News Social Links -->

                        </article>
                        <!-- /Post -->

                        <?php endwhile; endif; ?>
                        <!-- /Featured News Loop -->

                        <!-- Pagination -->
                        <?php if ($wp_query->max_num_pages > 1) : ?>
                            <div class="grid_10 pagination older-news">
                                <?php next_posts_link( __( \'<span class="arrow">&larr;</span> Older News\', \'twentyten\' ) ); ?>
                                <div class="pagination newer-news">
                                <?php previous_posts_link( __( \'Newer News <span class="arrow">&rarr;</span>\', \'twentyten\' ) ); ?>
                                </div>
                            </div>
                        <?php endif; ?>
                        <!-- /Pagination -->

                        <?php
                        /* PageNavi at Bottom */
                        if (function_exists(\'wp_pagenavi\')){wp_pagenavi();}
                        $wp_query = null;
                        $wp_query = $temp;
                        wp_reset_query();
                        ?>
                        <!-- /Featured News Loop -->

                </section>
                <!-- /Featured News -->

                <!-- Other News -->
                <aside class="grid_5 other-news read-other-news">

                    <!-- Other News Heading -->
                    <h3></h3>
                    <!-- /Other News Heading -->

                        <!-- Other News Loop -->
                        <?php query_posts(\'category_name=other-news&showposts=6\'); ?>
                        <?php while (have_posts()) : the_post(); ?>

                        <!-- Other News List -->
                        <ul>
                            <li><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'twentyten\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                            </li>
                            <li class="other-news-post-date"><?php echo get_the_date(\'d.m.Y\'); ?>
                            </li>
                        </ul>
                        <!-- /Other News List -->

                        <?php endwhile; ?>
                        <!-- /Other News Loop -->

                        <!-- View More -->
                        <div class="view-more">
                            <a href="/other-news">View More &rarr;</a>
                        <div>
                        <!-- /View More -->

                </aside>
                <!-- /Other News -->

            </div>
            <!-- /960 16 Column Grid -->

            <!-- sidebar.php -->
            <?php get_sidebar(); ?>
            <!-- /sidebar.php -->

            <!-- footer.php -->
            <?php get_footer(); ?>
            <!-- /footer.php -->

1 个回复
最合适的回答,由SO网友:Milo 整理而成

cat 需要ID,如果使用slug,请设置category_name 相反看见WP_Query Category Parameters.

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post