如何在wordpress中添加分页

时间:2017-02-02 05:11:47

标签: wordpress pagination

我在不同的自定义分类中有帖子。我需要在页面中显示一个分类的帖子。现在,我在一个页面中获取所有分类中的所有帖子。我有一个用于分类的foreach循环,在循环内所有与该分类相对应的帖子都是通过post方法显示的。我以前从未使用过分页。在这种情况下如何添加分页? 我随函附上我的代码

<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。在这种情况下如何添加分页?

1 个答案:

答案 0 :(得分:1)

试试这个:

你有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&#34;带分页的示例循环&#34;

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