2013-11-14 87 views
1

我在WordPress中创建了一个登陆页面(一个页面有很多节),并且我使用get_template_part()调用每个节。所以我有这样的事情;Wordpress在不同的页面上显示帖子

<?php get_template_part('content', 'reasons-to-purchase'); ?> 
    <?php get_template_part('content', 'testimonials'); ?> 
    <?php get_template_part('content', 'reasons-to-purchase'); ?> 

然后,每个内容文件具有一个循环返回匹配它的类别,该职位,posts_per_page设置为一个,如下所示(内容 - 原因对purchase.php);

  <?php 

     $args = array(
      'post_type' => 'section',  
      'category_name' => 'Reasons-To-Purchase', 
      'posts_per_page' => 1, 
      'orderby' => 'date', 
      'order' => 'DESC' 
     ); 

     $the_query = new WP_Query ($args); 

if (get_category_by_slug('Reasons-To-Purchase')->category_count > 0) {?> 


<!-- Featured content image or slider. --> 
     <div class="container panel"> 

      <?php if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> 

      <?php the_content(); ?> 


     </div> 

<?php } ?> 

      <?php wp_reset_query(); ?> <div class="clearfix"></div>  

我想是创造尽可能多的,因为这些部分在曾经为了我喜欢的,每个部分内环路拉分配给该类别中的下一个职位。因此,例如在第一个'购买原因'部分,第一个职位被拉,然后在第二个'购买原因'第二个职位被称为。目前我使用'wp_reset_query()'重置每个文件中的循环,所以它不会干扰下一个可能不同的部分。基本上循环必须继续为下一个类似的命名部分没有重复任何帖子..

任何想法如何做到这一点或建议将不胜感激。

+0

您的问题似乎并不清楚,请编辑和b它还包括相关的代码 –

+0

现在已经做好了,希望所有必要的信息都在那里。 – Naz

回答

1

你需要调用get_template_part()get_template_part()不支持再利用你的变量的前通过网页参数,你需要使用locate_template()include()

$page=1; 
include(locate_template('content-reasons-to-purchase.php')); 
include(locate_template('content-testimonials.php')); 
$page=2; 
include(locate_template('content-reasons-to-purchase.php')); 
$page=3; 
include(locate_template('content-reasons-to-purchase.php'));/* and so on page 4,5,6... */ 

在你模板使代码

略有变化
if(empty($page)){ 
$page=1; 
} 

$args = array(
      'post_type' => 'section',  
      'category_name' => 'Reasons-To-Purchase', 
      'posts_per_page' => 1, 
      'orderby' => 'date', 
      'paged'=>$page, /* <======= get page no from your file i.e locate_template(...) and use here*/ 
      'order' => 'DESC' 
     ); 

希望这是有道理的

相关问题