WordPress 通过 $_GET 参数自定义帖子类型分页
我有一个页面,我想为名为“article”的特定自定义帖子类型进行分页,并且我还使用名为“week”的 $_GET 参数。
基本上当我访问 http://www.mydomain.com/single-page/? week=7 我想查看“文章”帖子类型中帖子的分页,分类法称为“周”等于 7。
一切都很好,但 paginate_links 函数没有按照我想要的方式运行。例如,2 分页链接具有此 url http://www.mydomain .com/single-page/?week=7/page/2。但是这个url不起作用,它给出了404错误。
但是这个网址 http://www.mydomain.com/single-page /page/2/?week=7 它正在工作。那么解决办法是什么呢?
当我访问 www.mydomain.com/single-page/?week=7/page 时,有没有办法告诉 WordPress 使用 www.mydomain.com/single-page/page/2/?week=7 /2 ?
您有一些关于自定义帖子类型分页的好教程吗?
这是我用来分页的代码:
//The paginate function
function paginate_articles_week()
{
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' => '?page=%#%',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => true,
'type' => 'list',
'prev_text' => '«',
'next_text' => '»',
);
if ($wp_rewrite->using_permalinks())
$pagination['base'] = user_trailingslashit(trailingslashit(remove_query_arg('s', get_pagenum_link(1))) . 'page/%#%/', 'paged');
if (!empty($wp_query) && $pagination['total'] != 1) {
$pagination['add_args'] = array('s' => get_query_var('s'));
echo "<div class='paginate'><strong>Pagini:</strong>" . paginate_links($pagination) . "</div>";
}
}
//The page template
global $post;
$week = $_GET['week'];
$slug = 'week-' . $week . '-month-2';
$paged = 1;
$postsPerPage = 1;
if (get_query_var('paged')) { $paged = get_query_var('paged'); }
if (get_query_var('page')) { $paged = get_query_var('page'); }
$args = array(
'post_type'=> 'article',
'paged' => $paged,
'posts_per_page' => $postsPerPage,
'timeline' => $slug
);
query_posts($args);
{the loop}
paginate_articles_week();
提前感谢您的回答:)
I have a single page where I want to make a pagination for a certain custom post type called 'article' and I'm also using a $_GET param called 'week'.
Basically when I go to http://www.mydomain.com/single-page/?week=7 I want to see the pagination of my posts from 'article' post type with taxonomy called week equal to 7.
All it's fine, but paginate_links function doesn't act how I want. For example the 2 pagination link has this url http://www.mydomain.com/single-page/?week=7/page/2. But this url doesn't work, it gives an 404 Error.
But this url http://www.mydomain.com/single-page/page/2/?week=7 it's working. So what is the solution?
Is there a way to tell Wordpress to use www.mydomain.com/single-page/page/2/?week=7 when I go to www.mydomain.com/single-page/?week=7/page/2 ?
Do you have some good tutorial for custom posts type pagination?
This is the code I'm using to paginate:
//The paginate function
function paginate_articles_week()
{
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' => '?page=%#%',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => true,
'type' => 'list',
'prev_text' => '«',
'next_text' => '»',
);
if ($wp_rewrite->using_permalinks())
$pagination['base'] = user_trailingslashit(trailingslashit(remove_query_arg('s', get_pagenum_link(1))) . 'page/%#%/', 'paged');
if (!empty($wp_query) && $pagination['total'] != 1) {
$pagination['add_args'] = array('s' => get_query_var('s'));
echo "<div class='paginate'><strong>Pagini:</strong>" . paginate_links($pagination) . "</div>";
}
}
//The page template
global $post;
$week = $_GET['week'];
$slug = 'week-' . $week . '-month-2';
$paged = 1;
$postsPerPage = 1;
if (get_query_var('paged')) { $paged = get_query_var('paged'); }
if (get_query_var('page')) { $paged = get_query_var('page'); }
$args = array(
'post_type'=> 'article',
'paged' => $paged,
'posts_per_page' => $postsPerPage,
'timeline' => $slug
);
query_posts($args);
{the loop}
paginate_articles_week();
Thanks in advance for the answers :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论