WordPress分页不起作用 [英] Wordpress Pagination not working

查看:237
本文介绍了WordPress分页不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了多种解决方案,但没有用.

I have tried multiple solutions but it's not working.

当我转到/page/2时,它不起作用.

When I go to /page/2, it doesn't work.

我正在主题主题的index.php中执行自定义查询.

I'm executing a custom query in index.php of my theme.

if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
} else {
    $paged = 1;
}

$args = array(
    'post_type' => array('post', 'music', 'videos'),
    'post_status' => 'publish',
    //'meta_key' => 'featured',
    //'meta_value' => '1',
    'posts_per_page' => 10,
    'orderby'=>'date',
    'order'=>'DESC',
    'paged' => $paged
);

query_posts($args);

这是我网站的链接:我的网站首页

此页面无效(抛出404)-无效的页面(格式为-mywebsite/page/2/)

This page is not working (throwing 404) - Page which is not working(of the format - mywebsite/page/2/)

刚刚意识到此页面2可以正常工作-正在运行的页面(格式为-mywebsite.com/?page = 2)

Just realized this page 2 works - Page which is working (of the format - mywebsite.com/?page=2)

推荐答案

我遇到了同样的问题,这为我解决了所有问题.这使我可以使用漂亮的永久链接对index.php和page.php进行分页.

I was having the same problem and this fixed everything for me. This allows me to paginate on index.php as well as my page.php with pretty permalinks.

HTML/PHP:

<?php
    //Fix homepage pagination
    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }

    $temp = $wp_query;  // re-sets query
    $wp_query = null;   // re-sets query
    $args = array( 'post_type' => array('post', 'music', 'videos'), 'orderby'=>'date', 'order'=>'DESC', 'posts_per_page' => 10, 'paged' => $paged);
    $wp_query = new WP_Query();
    $wp_query->query( $args );
    while ($wp_query->have_posts()) : $wp_query->the_post(); 
?>

<!--your loop stuff here -->

<?php endwhile; ?>
<nav>
   <?php paginate(); ?>
   $wp_query = null;
   $wp_query = $temp; // Reset
</nav>

这允许一些事情.它检查您的主页,页面或单个页面,并告诉$ paged变量如何依次作出反应.它还允许您使用自定义帖子类型查询分页.另外,通过不使用query_post,您可以避免使用时有时会得到的一些非常时髦的东西. paginate();是我们现在引入的自定义函数:

This allows several things. One it checks if your on home, page, or single and tells the $paged variable how to react in turn. It also allows you to query your pagination with custom post types. Also by not using query_post you get to avoid some really funky stuff that you sometimes get when using it. The paginate(); is a custom function which we bring in now:

在您的functions.php内部

function paginate() {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

$pagination = array(
    'base' => @add_query_arg('page','%#%'),
    'format' => '',
    'total' => $wp_query->max_num_pages,
    'current' => $current,
    'show_all' => true,
    'type' => 'list',
    'next_text' => '&raquo;',
    'prev_text' => '&laquo;'
    );

if( $wp_rewrite->using_permalinks() )
    $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'page', get_pagenum_link( 1 ) ) ) . '?page=%#%/', 'paged' );

if( !empty($wp_query->query_vars['s']) )
    $pagination['add_args'] = array( 's' => get_query_var( 's' ) );

echo paginate_links( $pagination );
}

这最初来自 http://bavotasan.com/2011/simple-pagination-for-wordpress/和我一起稍微修改一下,以使分页可以在主页上工作.

This originally came from http://bavotasan.com/2011/simple-pagination-for-wordpress/ with me slightly modding it to get the pagination to work on the homepage.

这又做了几件事.它将页面与每个页面分页,并获得其自己的链接(我觉得很好),并且还重新编写了URL,以实现漂亮的永久链接.如果您检查链接,则部分使用了变量"s"代替"paged".我将's'替换为'paged',一切正常.

This, again, does several things. It paginates your page with each page getting it's own link (which I find nice) and it also re-writes the URL to allow for pretty permalinks. If you check the link, the variable 's' was being used in place of 'paged' in part of this. I replaced the 's' with 'paged' and everything worked perfectly on my end.

可选的分页样式

ul.page-numbers {
    margin: 20px 0 10px;
    width: 100%;
    padding: 0;
    font-size: 12px;
    line-height: normal;
    clear: both;
    float: left;
}

ul.page-numbers li {
       float: left;
    }

ul.page-numbers a,
ul.page-numbers span {
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    background: -webkit-gradient(linear, left top, left bottom, from(#E4E3E3), to(#FFFFFF));
    background: -moz-linear-gradient(top,  #E4E3E3,  #FFFFFF);
    filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#E4E3E3', endColorstr='#FFFFFF');
    padding: 3px 4px 2px 4px; 
    margin: 2px;
    text-decoration: none;
    border: 1px solid #ccc;
    color: #666;
}

ul.page-numbers a:hover,
ul.page-numbers span.current {  
    border: 1px solid #666;
    color: #444;
}

修改 之后,我意识到单击页面选项卡之一后,首页分页将中断.我已通过替换条件语句并将其放在位置来解决此问题.我也在上面更新了我的代码.

Edit I realized afterwards that home page pagination would break after clicking one of the page tabs. I've fixed this by replacing the conditional statement and putting this in its place. I've updated my code above as well.

$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'page', get_pagenum_link( 1 ) ) ) . '?page=%#%/', 'paged' );

这篇关于WordPress分页不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆