我正在使用Starker主题作为构建自定义WordPress主题的基础。更鲜明的主题带有一个循环。用于设置主循环的php文件。我决定使用循环。php文件在我的网站主页上显示内容。我想合并这个循环以及我网站其他页面的分页,但我只想查询特定类别。下面是我循环中的代码。php。在我的网站上设置其他页面或模板并拥有自己独特的查询而不与主循环冲突的最佳方式是什么?
<!-- 960 16 Column Grid -->
<div class="container_16">
<!-- Featured News -->
<section class="grid_10 featured-news">
<!-- 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\' => \'featured-news\',
\'posts_per_page\' => 2,
\'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();
?>
<!-- /Featured News Loop -->
<!-- Post -->
<article class="post" id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<!-- 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(\'m.d.Y\'); ?></p>
<!-- /Featured News Meta -->
<!-- Featured News Thumbnail -->
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(640,320)); ?></a>
<!-- /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;
/* PageNavi at Bottom */
if (function_exists(\'wp_pagenavi\')){wp_pagenavi();}
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
<!-- /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">←</span> Older News\', \'twentyten\' ) ); ?>
<div class="pagination newer-news">
<?php previous_posts_link( __( \'Newer News <span class="arrow">→</span>\', \'twentyten\' ) ); ?>
</div>
</div>
<?php endif; ?>
<!-- /Pagination -->
</section>
<!-- /Featured News -->
<!-- Other News -->
<aside class="grid_5 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__( \'%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 <span class="arrow">→</span></a>
<div>
<!-- /View More -->
</aside>
<!-- /Other News -->
</div>
<!-- /960 16 Column Grid -->
SO网友:HungryCoder
在主循环文件中,检查$args。它过滤查询。您可以使用自己的选项在其他页面上使用相同的查询。
假设在另一个页面上,您希望获取不同类别的帖子。您的查询参数可能如下所示:
$args = array(
\'post_type\' => \'post\',
\'cat\' => \'DIFFERENT_CATEGORY\',
\'posts_per_page\' => 2,
\'paged\' => $paged
);
使用特定类别ID更改不同的\\u类别。
然后可以将这些参数传递给循环中的WP\\u Query。php。
如果页面有多个此类查询,则应使用wp_reset_query
在运行进一步查询之前。更多信息:http://codex.wordpress.org/Function_Reference/wp_reset_query
有帮助吗?