【问题标题】:how to add pagination in wordpress如何在wordpress中添加分页
【发布时间】:2017-02-02 05:11:47
【问题描述】:

我有不同自定义分类法的帖子。我需要在页面中显示一个分类的帖子。现在,我在一个页面中获得了所有分类法中的所有帖子。我有一个用于分类的 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 中的帖子。在这种情况下如何添加分页?

【问题讨论】:

    标签: wordpress pagination


    【解决方案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“分页示例循环”

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

    【讨论】:

    • 此代码不起作用。如果我已将 post_per_page 添加为 1。那么它将在单个页面中为每个术语显示 1 个帖子。意味着在单个页面中有 1 个“Loyd”帖子,并且 1 个帖子显示在“Nycofee”下。
    • 尝试在 $args 上方添加这个 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 尝试将每页的帖子也更改为 5
    • 再次无法正常工作。实际上我需要为帖子添加分页。第一个 foreach 循环显示所有术语。每个学期都有任意数量的职位。我需要在一个页面中显示一个术语及其所有帖子(不知道帖子的数量),然后在下一页中显示下一个术语及其所有帖子,依此类推。第一个循环的分页
    猜你喜欢
    • 2015-09-21
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    相关资源
    最近更新 更多