2017-02-02 76 views
-1

我有不同的自定义分类法中的帖子。我需要在页面中显示一个分类的帖子。现在我可以在单个页面中获取所有分类法中的所有帖子。我有一个分类的foreach循环,在循环内部,与该分类相对应的所有帖子都通过post方法显示。我之前从未使用过分页。我如何在这种情况下添加分页? 我下面封闭如何在wordpress中添加分页

<div class="main"> 

<section class="brands-sec product-sec"> 
<div class="container"> 
<?php 
$siteurl = home_url('/'); 
$tax = 'product'; // slug of taxonomy 
$terms = get_terms($tax); 
foreach ($terms as $term) { 
$id = $term->term_id; 
$slug = $term->slug; 
$description = $term->description; 
$image_url = z_taxonomy_image_url($id, NULL, TRUE); // category image display 
$link = "<a href='$siteurl?$tax=$slug' ><h1> $term->name </h1></a>"; 

echo '<img src="' . $image_url . '">'; ?> 
    <div class="col-md-8 pull-right col-sm-10 pull-right col-xs-12 brnd prdct"> 

     <img src="<?php echo $image_url ; ?>" class="img-responsive pdt-logo" alt="loyd"/> 

     <div class="brand-logos pdt"> 

     <p><?php echo $description ; ?></p> 

       <h4>Product</h4> 
      <?php $args = array("posts_per_page" => "-1", "product"=>$slug ,"post_type" => "products"); 
$posts = get_posts($args); 
foreach($posts as $data){ 
$thumb = wp_get_attachment_url(get_post_thumbnail_id($data ->ID)); 
$custom_fonts = get_post_custom($data->ID);$custom_fonts_key=  $custom_fonts['category'];$custom_fonts_array = $custom_fonts_key[0]; ?>      
      <div class="row mb40"> 

       <div class="col-md-4 col-sm-4 col-xs-12 brand-single product-single"> 
        <img src="<?php echo $thumb; ?>" class="img-responsive" alt="allegro products"/> 
        <h5><?php echo $data->post_title; ?></h5> 
        <p><?php echo $data->post_content; ?></p> 
       </div> 
       <div class="col-md-8 col-sm-8 col-xs-12 brand-single product-detail"> 
        <ul class="products"> 
        <?php 
    $attachments = new Attachments('my_attachments',$data->ID); /* pass the instance name */ ?> 
<?php if($attachments->exist()) : ?> 
<?php while($attachments->get()) : ?> 
        <li><img src="<?php echo $attachments->url(); ?>" class="img-responsive" alt="allegro products"/></li> 
<?php endwhile; ?> 
<?php endif; ?> 
        </ul> 


       </div> 


      </div> 
<?php } ?>    

     </div><!--end brand-logos--> 

    </div><!--end brnd--> 
    <?php } ?> 

</div> 

我的代码以下产品自定义分类。在那里有不止一个术语说loyd,Nycofee,....在这两个术语中都有不止一个帖子。我需要在一页中显示loyd中的帖子,在下一页中显示ncofee。如何在这种情况下添加分页?

回答

0

试试这个:

你有post_per_page为-1

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$args = array("posts_per_page" => "-1", "product"=>$slug ,"post_type" => "products"); 

将其更改为你想的数量。并添加到末尾

'paged' => $paged 

结束这里的PHP之前

  </div> 
<?php } ?>    

     </div><!--end brand-logos--> 

添加

<?php if (function_exists('wp_pagenavi')) wp_pagenavi(array('query' => $args)); ?> 
<?php wp_reset_postdata(); ?> 

这里是WordPress的

<?php if (have_posts()) : ?> 

<!-- Add the pagination functions here. --> 

<!-- Start of the main loop. --> 
<?php while (have_posts()) : the_post(); ?> 

<!-- the rest of your theme's main loop --> 

<?php endwhile; ?> 
<!-- End of the main loop --> 

<!-- Add the pagination functions here. --> 

<div class="nav-previous alignleft"><?php next_posts_link('Older posts'); ?></div> 
<div class="nav-next alignright"><?php previous_posts_link('Newer posts'); ?></div> 

<?php else : ?> 
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
<?php endif; ?> 
基本分页

Soruce:https://codex.wordpress.org/Pagination“示例循环与分页”

如果它仍显示完整列表。 试试这个:http://callmenick.com/post/custom-wordpress-loop-with-pagination

+0

它显示完整列表。我怎么用这个? – Ranju

+0

你可以检查这个网址http://callmenick.com/post/custom-wordpress-loop-with-pagination –

+0

我已经添加了我的代码。你能帮我吗? – Ranju

相关问题