2013-02-21 101 views
0

我有一个WordPress页面是一个博客页面模板。现在我的问题是,它显示所有的帖子。我只希望它显示分配给某个类别的帖子如何使博客页面只显示某些职位

我该怎么做?

这是我的blog.php页面:我想你必须在这里编辑它吗?

<?php 
/* 
Template Name: Blog page 
*/ 
?> 
<?php get_header(); ?> 

<div class="blog"> 

<?php 
$gogo_select_blog_sidebar_position = get_post_meta($post->ID, 'gogo_select_blog_sidebar_position', true); 
$gogo_blog_link_text = get_post_meta($post->ID, 'gogo_blog_link_text', true); 
$gogo_blog_link_url = get_post_meta($post->ID, 'gogo_blog_link_url', true); 
$gogo_blog_text_no_posts = get_post_meta($post->ID, 'gogo_blog_text_no_posts', true); 
?> 

<header class="box-headline"> 
    <h4 class="main-headline"><?php the_title(); ?></h4> 
</header> 

<?php if($gogo_select_blog_sidebar_position=='right-sidebar-blog-template') { ?> 
<!--Left content area--> 
<div class="blog-holder"> 
<?php } else { ?> 
<div class="blog-holder right"> 
<?php } ?> 

<?php 
$temp = $wp_query; 
$wp_query= null; 
$wp_query = new WP_Query(); 
$wp_query->query('posts_per_page='.$gogo_blog_text_no_posts.'&paged='.$paged); 
$postcount = 0; 
while ($wp_query->have_posts()) : $wp_query->the_post(); $postcount++; 
?> 

<!--Begin post content--> 
<?php 
    // The following determines what the post format is and shows the correct file accordingly 
    $format = get_post_format(); 
    get_template_part('/lib/includes/post-formats/'.$format); 

    if($format == '') 
    get_template_part('/lib/includes/post-formats/standard'); 
?> 
<?php if($postcount % 2 == 0) echo "<div class='clearfix'></div>"; ?> 
<!--End post content--> 
<?php endwhile; ?> 
<!--Pagination --> 
<?php if (function_exists("pagination")) {pagination();} ?> 
<!--End pagination --> 
<?php $wp_query = null; $wp_query = $temp;?> 
</div> 
<!--End left content area--> 

<?php if($gogo_select_blog_sidebar_position=='right-sidebar-blog-template') { ?> 
<!-- Right content --> 
<aside class="sidebar right"> 
<?php } else { ?> 
<aside class="sidebar left"> 
<?php } ?> 
<?php get_sidebar('blog'); ?> 
</aside> 
<!-- End Right content --> 
</div><!-- End .block --> 
<?php get_footer();?> 

回答

1

您的类别添加到WP_Query字符串:

$wp_query->query('posts_per_page='.$gogo_blog_text_no_posts.'&paged='.$paged . '&category_name=***yourcategory***'); 
+0

酷,谢谢。欣赏。还有一个问题:我想用博客页面模板创建另一个页面,但对于此页面,我只想显示另一个类别,但由于它将使用同一个blog.php页面,因此如何为新的页面指定另一个类别页? – DextrousDave 2013-02-21 07:36:18

+0

查看[模板层次结构](http://codex.wordpress.org/Template_Hierarchy),您可能更喜欢使用'category.php'来执行此任务。当然,这一切都取决于你的背景。 – RRikesh 2013-02-21 08:07:28

+0

谢谢。真的很赞赏你的回答。得到它的工作。我复制blog.php文件并将其命名为blog2.php。我改变了顶部的模板名称,并使用了代码给你,让我只显示某个类别的帖子。所以当我创建了一个包含帖子的新页面时,我只是选择了正确的模板(这就是在blog.php文件顶部命名模板名称的地方) – DextrousDave 2013-02-21 20:00:09

相关问题