我有标准的职位和我的网站上使用自定义的职位。基本标准和定制的文章中,我们希望下面的内容。
父类职位例如:博客 再分类别,此父类别。因此,例如:
博客(父类)
新闻(子类别)
LatestNewsPostOne
事件(子类)
LatestEvnntssPostOne
所以帖子可以住在任何一类或全部。如果您访问博客类别中,您将有以下permastructure:
domain.com/blog
如果您访问新闻类别中,您将有以下permastructure:
domain.com/blog/news
不过,当您访问LatestNewsPostOne,您将有以下permastructure:
domain.com/blog/LatestNewsPostOne
因此,在最后一个例子,你会发现,一旦你点击一个单独的职位将其打开。子类别消失的URL。目前,我的网站树叶中,我不希望子类别。
有谁知道任何解决方案来实现这个就一个字preSS的网站?
解决方案我已经想通了。下面是我的code摆脱从博客文章永久链接URL中的子类别段塞,我增加了以下在主题文件夹我的functions.php文件:
的add_filter(
post_link,
custom_post_type_link,
10,
3);功能custom_post_type_link($永久,$后,$ leavename){
如果(!的GetType($后)=='后'){
返回$永久链接;
}
开关($后> post_type){
案后:
// $永久= get_home_url()。 '/'。 $后> post_name。 '/';
$猫= get_the_category($后> ID);
$ subcats =阵列();
的foreach($猫为$猫){
$猫= get_category($ CAT-> term_id);
如果($ CAT->母公司){$ subcats [] = sanitize_title($ CAT->的名字); }
}
如果($ subcats){
的foreach($ subcats为$ subcat){
。$ subcat = $ subcat'/';
$永久= str_replace函数($ subcat,,$永久链接);
}
}
打破;
}
返回$永久;}
I have standard posts and custom posts utilised on my site. Basically for standard and custom posts we want the following.
parent post category e.g.: "blog" then sub categories to this parent category. So e.g.:
Blog (parent Category)
News (Sub Category)
LatestNewsPostOne
events (sub category)
LatestEvnntssPostOne
So posts can live in any or all of these categories. If you visit the "blog" category you will have the following permastructure:
domain.com/blog
If you visit "news" category you will have the following permastructure:
domain.com/blog/news
However when you visit "LatestNewsPostOne" you would have the following permastructure:
domain.com/blog/LatestNewsPostOne
So in the last example you will notice that once you click on an individual post to open it. the subcategory disappears from the url. Currently my site leaves in the subcategory which I don’t want.
Does anyone know of any solution to implement this on a WordPress site?
解决方案I've figured it out. Below is my code to get rid of the subcategory slug from blog post permalink URL, I added the following to my functions.php file in the theme folder:
add_filter(
'post_link',
'custom_post_type_link',
10,
3); function custom_post_type_link($permalink, $post, $leavename) {
if (!gettype($post) == 'post') {
return $permalink;
}
switch ($post->post_type) {
case 'post':
//$permalink = get_home_url() . '/' . $post->post_name . '/';
$cats = get_the_category($post->ID);
$subcats = array();
foreach( $cats as $cat ) {
$cat = get_category($cat->term_id);
if($cat->parent) { $subcats[] = sanitize_title($cat->name); }
}
if($subcats) {
foreach($subcats as $subcat) {
$subcat = $subcat.'/';
$permalink = str_replace($subcat, "", $permalink);
}
}
break;
}
return $permalink;}
相关推荐
最新文章