【问题标题】:How to add Custom pagination in wordpress如何在wordpress中添加自定义分页
【发布时间】:2015-09-21 22:29:21
【问题描述】:

我在 wordpress 中创建了自定义主题。 我想将自定义分页添加到我的自定义帖子模板中,即 INDEX.PHP

你能检查一下分页脚本有什么问题吗?实际上我设置了 4 个帖子限制,我的博客中大约有 8 个帖子。当点击 2 个分页时,它不会移动到下一页...

<?php get_header(); ?>

<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

// WP_Query arguments
$args = array (
    'post_type'              => the_post(),
    'posts_per_page'         => '3',
    'paged' => $paged
);
?>
<?php

// The Query
$cquery = new WP_Query( $args );
while ( $cquery->have_posts() ) : $cquery->the_post();
?>
<div class="row">
                    	<div class="img"><a href="<?php the_permalink() ?>" class="imgPos"><?php the_post_thumbnail('full'); ?></a></div>
                        <div class="text">
                        	<h2><a href="<?php the_permalink() ?>" style="color:#545454;"><?php the_title(); ?></a></h2>
                            <h3 style="line-height: 1px;"><span class="floatL">By &nbsp;</span> <span class="floatL"> <?php the_author_posts_link(); ?> &nbsp;</span> <span class="floatL">&nbsp; - &nbsp;</span> <span class="floatL"><?php the_time('F jS, Y'); ?></span><div class="clr"></div></h3>
                            <p><?php the_content('Read More') ?></p>
                           
                        </div> 
                        </div> 
<?php
$post->ID;
endwhile;

$big = 999999999; // need an unlikely integer
?>
<div class="row">
<div class="pagination">
<?php
echo paginate_links( array(
    'base' => str_replace( $big, '', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' =>  $cquery->max_num_pages
) );
?>
</div>
</div>
<?php get_footer(); ?>

请帮忙:) 谢谢, 哈沙德·帕蒂尔

【问题讨论】:

    标签: wordpress post pagination


    【解决方案1】:

    在“functions.php”文件中添加以下代码:

    function custom_pagination($pages = '', $range = 2)
    {  
     $showitems = ($range * 2)+1;  
    
     global $paged;
     if(empty($paged)) $paged = 1;
    
     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   
    
     if(1 != $pages)
     {
         echo "<div class='pagination'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";
    
         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
             }
         }
    
         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>\n";
     }
     }
    

    并在您的“index.php”文件中添加“custom_pagination();”在while循环之后。

    【讨论】:

    • 我如何在帖子中调用分页实际上我对 wordpress 很陌生..请在这里查看链接 equigreen.in/blog 我想添加分页
    【解决方案2】:

    试试这个

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    
    // WP_Query arguments
    $args = array (
        'post_type'              => 'YOUR_CUSTOM_POST_TYPE',
        'posts_per_page'         => '3',
        'paged' => $paged
    );
    
    // The Query
    $cquery = new WP_Query( $args );
    while ( $cquery->have_posts() ) : $cquery->the_post();
    echo $post->ID;
    endwhile;
    
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' =>  $cquery->max_num_pages
    ) );
    

    【讨论】:

    • 我如何在帖子中调用分页实际上我对 wordpress 很陌生。请检查链接equigreen.in/blog 这里我想添加分页
    • 请从您的模板中删除名为 custom_pagination() 的函数
    • 据我所知......我必须将您的代码复制到 fucntion.php 以及我必须调用的任何地方?
    • 您是否为自定义帖子列表创建了模板?
    • 请检查下面的 这里是动态内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    相关资源
    最近更新 更多