首页 文章

添加分页是WordPress上的自定义归档页面(论文主题)

提问于
浏览
2

我在页面上使用以下PHP代码来显示单个类别中每页10个帖子 .

function my_page_of_posts5() {
    if (is_page('10')) {
        $custom_loop = new WP_Query('posts_per_page=10&cat=9');
        echo '<div class="my-archives"><ul class="archive-list">';
        if ( $custom_loop->have_posts() ) : 
            while ( $custom_loop->have_posts() ) : $custom_loop->the_post();
                echo '<li><a class="archive-link" href="' 
                    . get_permalink() . '">' 
                    . get_the_title() 
                    . '</a> <span class="my-comment-count">( ';
                comments_number('0', '1', '%');
                echo ' )</span></li>';
                the_excerpt();
            endwhile;
            wp_reset_query();
        endif;
        echo '</ul></div>';
    }
}
add_action('thesis_hook_after_content','my_page_of_posts5');

我正在使用Thesis WordPress主题框架 .

我希望在页面上的这些帖子后显示分页 .

我应该用什么代码来表示分页?

我尝试过使用WP-Paginate插件并放入wp-paginate();页面上的功能,但它不能正常工作 .

需要另一种方法来解决它 .

4 回答

  • 1

    There is a Two possible Solutions :

    (一世) . Solution #1 :

    我希望你会认为wp-pagenavi是最好的解决方案 . 将它集成到论文主题中太容易了 . 作为论文主题用户,您已经使用了论文openhook插件 .

    现在只需按照步骤在论文主题中添加 pagenavi 即可 .

    • 打开 Apperance > Thesis Openhook .

    • 查找 After Content .

    • 只需在框中粘贴代码即可 . <?php cr_pagenavi(); ?>

    • 选中 Execute PHP on this hook 并单击“保存” .

    (ⅱ) . Solution #2 :

    1. 首先,我安装了一个Simple Pagination插件(简单易用) . 如果您正在使用论文,请将此代码复制并粘贴到thesis_hook_after_content中并点击“保存” .

    <div class="pagination">
    <?php wp_simple_pagination(); ?>
    </div>
    

    2. 转到custom_functions.php并粘贴此代码以删除上一篇和下一篇文章

    function no_home_post_nav() {
    if (is_home())
      remove_action('thesis_hook_after_content', 'thesis_post_navigation');
    }
    add_action('thesis_hook_before_content','no_home_post_nav');
    

    3. 在您的主页上查看您的新分页,并将其自定义为您想要的任何样式 .

  • 1

    在您尝试使用 wp_paginate 时,如果不了解更多有关查询的确切行为的信息,我建议的最佳方法是将 'paged=' . get_query_var( 'paged' ) 添加到您的 WP_Query args,commeça:

    $custom_loop = new WP_Query('posts_per_page=10&cat=9&paged=' . get_query_var( 'paged' ));
    

    添加后,wp_paginate可能适合您 .

    试一试!

    UPDATE

    好的,从一开始:

    • 声明并抓取 paged 查询变量

    • 在查询中将其用作参数

    • 调用 posts_nav_link 以获得必要的导航

    你的整个片段应该像这样工作:

    function my_page_of_posts5() {
        if (is_page('10')) {
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // what page? if none, one
            $custom_loop = new WP_Query('posts_per_page=10&cat=9&paged=' . $paged);
            echo '<div class="my-archives"><ul class="archive-list">';
            if ( $custom_loop->have_posts() ) : while ( $custom_loop->have_posts() ) : $custom_loop->the_post();
                echo '<li><a class="archive-link" href="' . get_permalink() . '">' . get_the_title() . '</a> <span class="my-comment-count">( ';
                comments_number('0', '1', '%');
                echo ' )</span></li>';
                the_excerpt();
                endwhile;
                wp_reset_query();
            endif;
        echo '</ul></div>';
        posts_nav_link(); // your next and previous links
        }
    }
    add_action('thesis_hook_after_content','my_page_of_posts5');
    
  • 0

    请尝试使用此代码,将其替换为现在的代码 .
    它增加了我一直使用的分页功能,不需要额外的插件 . 易于定制 .

    <?php
    function my_page_of_posts5() {
        if (is_page('10')) {
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // what page? if none, one
            $custom_loop = new WP_Query('posts_per_page=10&cat=9&paged=' . $paged);
            echo '<div class="my-archives"><ul class="archive-list">';
            if ( $custom_loop->have_posts() ) : while ( $custom_loop->have_posts() ) : $custom_loop->the_post();
                echo '<li><a class="archive-link" href="' . get_permalink() . '">' . get_the_title() . '</a> <span class="my-comment-count">( ';
                comments_number('0', '1', '%');
                echo ' )</span></li>';
                the_excerpt();
                endwhile;
                wp_reset_query();
            endif;
        echo '</ul></div>';
        theme_pagination();
        }
    }
    add_action('thesis_hook_after_content','my_page_of_posts5');
    
    // based on http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin
    function theme_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 > 1 && $showitems < $pages)
                echo '<a href="'.get_pagenum_link($paged - 1).'" class="previous" >';
                echo _x('previous', 'paginate', THEME_L10n);
                echo '<span>previouw-arrow</span></a>';
    
            for ($i=1; $i <= $pages; $i++) {
                if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
                    if ($paged == $i){
                        echo '<span class="current number">'.$i.'</span>';
                    } else {
                        echo '<a href="'.get_pagenum_link($i).'" class="inactive number" >'.$i.'</a>';
                    }
                }
            }
    
            //if ($paged < $pages && $showitems < $pages)
            echo '<a href="'.get_pagenum_link($paged + 1).'" class="next" ><span>next-arrow</span>';
            echo _x('next', 'paginate', THEME_L10n);
            echo '</a>';
            echo "</div>\n";
        }
    }
    
  • 1

    您可以尝试在示例中的代码末尾添加此分页代码:

    //Set Blog Reading Settings to 10!
    global $wp_query;
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array( 'base'    => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
                                'format'  => '?paged=%#%',
                                'current' => max( 1, get_query_var( 'paged' ) ),
                                'total'   => $wp_query->max_num_pages,
                                'end_size'=> 1,
                                'mid_size'=> 5 ) );
    

    该代码适用于普通免费主题的归档和类别页面,但您使用的代码不是免费的,也不是开源的,因此很难预测结果 .

    我认为应删除帖子数量:

    WP_Query('posts_per_page=10&cat=9')
    // Just leave it like this:
    WP_Query('cat=9')
    

    原因是WP中每页的帖子数量在阅读设置中配置 . 在查询中放置数量可能会阻止分页 . 毋庸置疑,必须有超过10个职位才能进行分页 .

    祝好运!

相关问题