WP 自定义帖子类型分页/404 错误
我在 WordPress 中使用自定义帖子类型(称为“ns_news_article”,由 Magic Fields 生成),该类型已注册并正在运行。这会重写 URL 以在域后面添加“category/news”(例如:www.mydomain.com/category/news/custom-post-1)。我必须在那里有“假目录”。
我在functions.php中有这个:
add_rewrite_rule("[ns_news_article]" . '$', "index.php?pagename=[ns_news_article]", "top");
add_rewrite_rule("[ns_news_article]" . '/page/([0-9])*/?', "index.php?pagename=[ns_news_article]" . '&paged=$matches[1]', "top");
global $wp_rewrite;
$wp_rewrite->flush_rules();
第二行解决了查看多页列表时的分页404问题,但在尝试查看单个帖子时出现404错误。如果我去掉这个,你可以查看帖子,但分页不起作用。
我可以解决这个问题还是应该以其他方式完成?
I'm using a custom post type in WordPress (called "ns_news_article" generated from Magic Fields) which are registered and working. This rewrites the URL to add "category/news" after the domain (so: www.mydomain.com/category/news/custom-post-1). I have to have the "fake directories" in there.
I have this in the functions.php:
add_rewrite_rule("[ns_news_article]" . '
The 2nd line takes care of pagination 404 problem when viewing as multiple pages of lists, but then gives 404 errors when trying to view the single post. If I get rid of this, you can view the posts, but pagination doesn't work.
Can I fix this or should it be done another way?
, "index.php?pagename=[ns_news_article]", "top");
add_rewrite_rule("[ns_news_article]" . '/page/([0-9])*/?', "index.php?pagename=[ns_news_article]" . '&paged=$matches[1]', "top");
global $wp_rewrite;
$wp_rewrite->flush_rules();
The 2nd line takes care of pagination 404 problem when viewing as multiple pages of lists, but then gives 404 errors when trying to view the single post. If I get rid of this, you can view the posts, but pagination doesn't work.
Can I fix this or should it be done another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最近完成了自定义帖子类型分页,所以这就是我所做的。假设您的自定义帖子类型称为“customp”。
在您的主题中创建一个名为
page-customp.php
的文件。然后发布一个标题为“Customp”的空页面。现在,当您访问 www.yourdomain.com/customp 时,您将看到一个使用page-customp.php
作为模板的页面。现在我们将使用此页面来显示自定义帖子并进行分页。将此代码放入
您的 page-customp.php
文件中:<前><代码>$分页 = 1;
$每页帖子数 = 5;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
query_posts( '&post_type=customp&paged=' . $paged . '&posts_per_page='.$postsPerPage );
while (have_posts()) : the_post();
the_title();
回声 get_the_excerpt();
最后;
自定义p_paginate();
在您的
functions.php
文件中放置此代码:我希望它有帮助:)
I've recently done custom post types pagination, so this is how I've done. Let's say your custom post type it's called "customp".
Create a file in your theme called
page-customp.php
. Then publish an empty page with "Customp" title. Now, when you're visiting www.yourdomain.com/customp you will see a page that is usingpage-customp.php
as a template. Now we will be using this page to show the custom posts and with a pagination.Place this code in
your page-customp.php
file:In your
functions.php
file place this:I hope it helps :)