手动编号分页在wordpress模板文件中不起作用

克里斯蒂娜·贝雷斯勒(Kristina Bressler)

我有此代码有效,显示“较旧的条目”和“较新的条目”。但是,我想编辑此代码以显示“ 1”,“ 2”,“ 3”等,而没有左/右箭头或Prev / Nex文本。因此,我做了研究并且感到困惑。我可以理解诸如Wordpress分页(数字分页位于底部),如何向您的WordPress主题中添加分页分页链接之类的教程,但它们并未说明将代码放置在何处。仅第二篇教程完全解释了我需要做的事情,但是即使如此,这种方式还是行不通的...

我的代码有效:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

// the query
$the_query = new WP_Query( 'cat=9&posts_per_page=9&paged=' . $paged ); 
?>

<?php if ( $the_query->have_posts() ) : ?>

<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>

<div class="proyectpost">
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

            <div class="innerpost">

             <div class="postthumbnail">
            <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
     echo '<img src="' . $image_src[0]  . '" width="100%"  />';
    } ?>
            </div>
            <div class="posttitle">
        <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            </div><!-- .entry-header -->

        <div class="postsubtitle">
        <div class="datepanel">

        </div>
        </div>

</div>
</article><!-- #post-## --> 

</div>


<?php endwhile; ?>

<?php

// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>

<?php 
// clean up after the query and pagination
wp_reset_postdata(); 
?>

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

第二篇教程说过将此代码添加到functions.php中:

// Numbered Pagination
if ( !function_exists( 'wpex_pagination' ) ) {

    function wpex_pagination() {

        $prev_arrow = is_rtl() ? '&rarr;' : '&larr;';
        $next_arrow = is_rtl() ? '&larr;' : '&rarr;';

        global $wp_query;
        $total = $wp_query->max_num_pages;
        $big = 999999999; // need an unlikely integer
        if( $total > 1 )  {
             if( !$current_page = get_query_var('paged') )
                 $current_page = 1;
             if( get_option('permalink_structure') ) {
                 $format = 'page/%#%/';
             } else {
                 $format = '&paged=%#%';
             }
            echo paginate_links(array(
                'base'          => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format'        => $format,
                'current'       => max( 1, get_query_var('paged') ),
                'total'         => $total,
                'mid_size'      => 3,
                'type'          => 'list',
                'prev_text'     => $prev_arrow,
                'next_text'     => $next_arrow,
             ) );
        }
    }

}

并用此代码替换默认分页

<?php wpex_pagination(); ?>

如何在wordpress模板php文件中显示编号的分页?

克里斯蒂娜·贝雷斯勒(Kristina Bressler)

经过进一步研究后,我发现了一些线索,这些线索启发了我找出在哪里放置正确的代码,以便获得编号的分页。

您使用以下代码:

<?php
$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' => $the_query->max_num_pages
) );
?>

取代:

<?php

// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>

我认为让我感到困惑的是我在研究时,有人说要在functions.php中添加一些代码,而有人说要在某些地方使用其他代码。因此,当我阅读分页链接时,该页面不清楚上面的新代码是否应添加到functions.php或替换“ prev / next”链接。

无论如何,现在就可以使用!:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当我在mongodb中手动设置_id时,@ CreatedDate不起作用

即使手动执行,golang命令也不起作用

在ggplot2中使用`scale_fill_manual`手动着色图不起作用

手动创建的global.asax.cs文件不起作用

手动屏幕跟踪在Firebase(iOS)中不起作用

手册中的手动编号

Bootstrap Datepicker设置日期手动不起作用

如果手动创建vert.x实例,则群集Vert.x在docker中不起作用

Popover手动单击外部关闭:在Safari中不起作用

来自手动标记的Span的ENT_TYPE模式不起作用

HTML JS手动表单验证不起作用

分页在Wordpress中的静态博客页面中不起作用

计数器不起作用,但通过手动输入起作用

手动键入时,Linux Shell命令有效。放入脚本文件时不起作用

脚本中的子流程不起作用,手动启动时可以

手动将flexslider添加到Rails 4应用中-类型错误flexslider不起作用

手动创建的scilab启动器不起作用

在Eclipse中手动导入项目不起作用?

HTML Textarea-手动输入的新行(/ n)不起作用

通过crontab任务移动文件不起作用,但是如果手动执行则可以工作

手动最小化/恢复后,在样式中设置“WindowsState”=“最大化”不起作用

Htaccess 手动插入的链接不起作用

手动触发作业不起作用

手动导航在 AngularJS 1.7 的 ui-router 中不起作用

在 Cron 中调用命令不起作用,但如果手动运行则有效

模板化手动析构函数调用不起作用

Rest POST 在 CRON 任务中不起作用(它适用于手动 rake 命令)

手动创建的公式似乎不起作用……但为什么呢?

分页编号序列不起作用(jquery)