如何更改自定义帖子类型及其分类的固定链接结构?
-
-
如果正确实施,我在上一个问题中给出的解决方案应该可以采用这种方式.唯一的事情是您无法获取html部分The solution I gave in the last question should have worked that way, if you implemented it correctly. The only thing is you can't get the html part
- 1
- 2011-09-20
- Manny Fleurmond
-
是的,但是它被锁定到特定路径,因此只有类别/子类别/页面,而可能没有子类别,甚至更多.这更加动态.Yes but it was locked to a specific path, so only category/subcategory/page while there might not be subcategories or even more. This is more dynamic.
- 0
- 2011-09-20
- Mark
-
或我没有正确实施它;)Or I did not implement it properly ;)
- 0
- 2011-09-20
- Mark
-
3 个回答
- 投票数
-
- 2011-09-20
好的,我想我可能有解决办法.我不知道这是否是实现此目标的正确方法,但是到目前为止,这似乎是唯一可行的方法.
add_filter('rewrite_rules_array', 'mmp_rewrite_rules'); function mmp_rewrite_rules($rules) { $newRules = array(); $newRules['portfolio/(.+)/(.+?).html$'] = 'index.php?project=$matches[2]'; $newRules['portfolio/(.+)/?$'] = 'index.php?project_category=$matches[1]'; return array_merge($newRules, $rules); } add_filter('request', 'mmp_rewrite_request'); function mmp_rewrite_request($vars) { if (isset($vars['project_category'])) { if (strpos($vars['project_category'], '/') !== false) { $categories = explode('/', $vars['project_category']); $vars['project_category'] = $categories[count($categories) - 1]; } } return $vars; }
Ok I think I might have a solution. I have no idea if this is the right way to accomplish this, but as for now it's the only thing that seems to work.
add_filter('rewrite_rules_array', 'mmp_rewrite_rules'); function mmp_rewrite_rules($rules) { $newRules = array(); $newRules['portfolio/(.+)/(.+?).html$'] = 'index.php?project=$matches[2]'; $newRules['portfolio/(.+)/?$'] = 'index.php?project_category=$matches[1]'; return array_merge($newRules, $rules); } add_filter('request', 'mmp_rewrite_request'); function mmp_rewrite_request($vars) { if (isset($vars['project_category'])) { if (strpos($vars['project_category'], '/') !== false) { $categories = explode('/', $vars['project_category']); $vars['project_category'] = $categories[count($categories) - 1]; } } return $vars; }
-
如果我们要询问类别(实际上是术语)和帖子,则请求过滤器是一种很好的过滤方法.它允许仅添加一个重写规则并获得帖子页面,无论帖子是属于某个类别,子类别还是根本不属于该类别.The request filter is a good way to filter if we're asking a category (term in fact) and a post. It allows adding only one rewrite rule and get post page whether if post is in a category, a subcategory or not at all.
- 0
- 2018-10-30
- ZalemCitizen
-
- 2014-11-10
2种不同方法:
*请参阅本文底部的注释.
例如,您想要具有这样的永久链接结构:
/MAIN_CATEGORY/SUB_CAT_2/Another_SUBCAT/我的帖子首先,您可能需要将永久链接设置为/%category%/%postname%.然后...
方法1:
创建标准类别(
MAIN_CATEGORY
,SUB_CAT_1
,等.
),并注册CUSTOM POST,包括以下参数:>'taxonomies'=> array('category'..)
并使用此代码更改永久链接: https://wordpress.stackexchange.com/a/195643/33667一个>
然后,发布自定义帖子(如果附加在类别下)后,URL将为:
example.com/MAIN_CATEGORY/SUB_CAT_1/my-post
方法2:
(请注意,此方法 不建议 您计划发布数百或数千个帖子)
注册自定义POST(名为
MAIN_CATEGORY
),包括以下参数:" supports"=> array('page-attributes'...... "分层"=>真正,
然后,像这样发布自定义帖子:
(例如,发布多个名为SUB_CAT_2,SUB_CAT_1 ..
的自定义帖子.之后,当您发布另一篇文章时,请选择SUB_CAT_2
作为父母.
ps
1)如果您是新手,请查看:"> 注册自定义帖子 和 使用TAXONOMY注册CUSTOM POST
2)如果您需要子级别的搜索功能,请使用2 Different Methods:
*See notes in the bottom of this post.
for example, you want to have such permalink structure:
/MAIN_CATEGORY/SUB_CAT_2/Another_SUBCAT/my-postAt first, you may need to set permalinks to /%category%/%postname%. Then...
METHOD 1:
create STANDARD categories (
MAIN_CATEGORY
,SUB_CAT_1
,and etc..
) , and register the CUSTOM POST, including this parameter:'taxonomies' => array('category'..)
and use this codes to change permalinks: https://wordpress.stackexchange.com/a/195643/33667
Then, after publishing a CUSTOM POST (if attached under a category), URL will be:example.com/MAIN_CATEGORY/SUB_CAT_1/my-post
METHOD 2:
(p.s. this method is not advised if you plan to publish hundreds or thousands of posts)
register the CUSTOM POST (named
MAIN_CATEGORY
), including these parameters:"supports" => array('page-attributes'...... "hierarchical" => true,
then, publish custom posts like this:
(i.e. publish several custom posts, namedSUB_CAT_2, SUB_CAT_1..
. After then, when you publish another post, but chooseSUB_CAT_2
as parent.
p.s.
1) If you are newbie, review: Register CUSTOM POST and Registering CUSTOM POST with TAXONOMY
2) if you will need search functionality for sub-levels, then use custom search query -
- 2016-04-07
After banging my head for hours, I found this plugin very helpful.
https://wordpress.org/plugins/custom-post-type-permalinks/
Below is the screen shot of options it gives.
这是我先前提出的问题的转贴,但我现在想进入更多细节以完全解决此问题.
再说一次,用(普通)帖子更改永久链接就像转到"设置">"永久链接",然后将其更改为您喜欢的任何东西(例如广泛使用的
%category%/%postname%.html
.这一切正常.如果这对于自定义帖子类型和分类法一样简单的话.这就是我想要完成的.
到目前为止,我得到的是以下内容:
custom_post_type
和一个taxonomy
project_category .post_type = project
帖子.如何将.html添加到页面的永久链接中?现在,它链接到 http://mywordpress.com/portfolio ,而我想要 http://mywordpress.com/portfolio.html .这应该可以解决第一个问题.portfolio/(CATEGORY PATH FROM TOP TO CHILD)/post.html
.我仍然停留在这一点上.我认为我应该以某种方式将最后一个类别添加到路径中,并将其沿project_category 传递,这将导致我可以正确处理它的taxonomy.php文件.%postname%.html
有关.我希望我的问题很清楚,并且有勇敢的开发人员可以帮助我解决这个使我忙了整整四天的问题!