admin管理员组文章数量:1124548
I want all my posts to have /ARTICLES/%post-name% structure.
But as soon as I modify the custom structure to that, for a reason that beats me, wordpress adds the articles
prefix across the board.
So I end up with urls like this for my category pages with something like the following
mysite/articles/category/videos
here the videos is a category and since I did not change the category to anything else, I was expecting the urls to be well, mysite/category/videos ), not mysite/articles/category/whatever Obviously, such a URL does not make sense.
On the permalinks
screen, under the Common Settings
's Post name
option, I get a grayed out ( uneditable ) sample link that shows something like mysite/sample-post
.
I wished I could have edit that grayed out info
to mysite/articles/sample-post
while keeping the other options as is.
But this does not seem to be possible.
Am I at a dead-end here?
Is there a way to only change the post urls without effecting the other urls?
I want all my posts to have /ARTICLES/%post-name% structure.
But as soon as I modify the custom structure to that, for a reason that beats me, wordpress adds the articles
prefix across the board.
So I end up with urls like this for my category pages with something like the following
mysite.com/articles/category/videos
here the videos is a category and since I did not change the category to anything else, I was expecting the urls to be well, mysite.com/category/videos ), not mysite.com/articles/category/whatever Obviously, such a URL does not make sense.
On the permalinks
screen, under the Common Settings
's Post name
option, I get a grayed out ( uneditable ) sample link that shows something like mysite.com/sample-post
.
I wished I could have edit that grayed out info
to mysite.com/articles/sample-post
while keeping the other options as is.
But this does not seem to be possible.
Am I at a dead-end here?
Is there a way to only change the post urls without effecting the other urls?
Share Improve this question edited May 17, 2012 at 20:12 Average Joe asked May 17, 2012 at 17:47 Average JoeAverage Joe 1,8894 gold badges24 silver badges41 bronze badges 3- 1 I think I had the same question a while ago: wordpress.stackexchange.com/questions/13411/… – Ryan Commented May 17, 2012 at 17:53
- That only takes care of the CPT urls. My problem is on category pages's URL. – Average Joe Commented May 17, 2012 at 20:08
- Custom permalink structure with a prefix just for posts – nmr Commented Apr 5, 2019 at 11:40
5 Answers
Reset to default 91) Add this rewrite at the end of you function.php
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array(
'YOUR_PREFIX/(.+?)/?$' => 'index.php?post_type=post&name='. $wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
function change_blog_links($post_link, $id=0){
$post = get_post($id);
if( is_object($post) && $post->post_type == 'post'){
return home_url('/YOUR_PREFIX/'. $post->post_name.'/');
}
return $post_link;
}
add_filter('post_link', 'change_blog_links', 1, 3);
2) Go to Settings > Permalinks and click Save Changes
.
Wordpress puts "/category/" by default as the category base in the permalinks even for a category page.
You have to change your custom permalink structure to:
/%category%/%postname%/
Put "articles" in the category base field and your permalinks for example at category page "Videos" will display in this form:
mysite.com/articles/videos
It won't work with post's permalinks thought as "articles" parts will be omitted from the permalink like this:
mysite.com/videos/postname
The WordPress SEO Plugin by Yoast has a section called permalinks where you can strip the category base (usually /category/) from the category URL.
<?php
/**
* Add new rewrite rule
*/
function create_new_url_querystring() {
add_rewrite_rule(
'blog/([^/]*)$',
'index.php?name=$matches[1]',
'top'
);
add_rewrite_tag('%blog%','([^/]*)');
}
add_action('init', 'create_new_url_querystring', 999 );
/**
* Modify post link
* This will print /blog/post-name instead of /post-name
*/
function append_query_string( $url, $post, $leavename ) {
if ( $post->post_type != 'post' )
return $url;
if ( false !== strpos( $url, '%postname%' ) ) {
$slug = '%postname%';
}
elseif ( $post->post_name ) {
$slug = $post->post_name;
}
else {
$slug = sanitize_title( $post->post_title );
}
$url = home_url( user_trailingslashit( 'blog/'. $slug ) );
return $url;
}
add_filter( 'post_link', 'append_query_string', 10, 3 );
/**
* Redirect all posts to new url
* If you get error 'Too many redirects' or 'Redirect loop', then delete everything below
*/
function redirect_old_urls() {
if ( is_singular('post') ) {
global $post;
if ( strpos( $_SERVER['REQUEST_URI'], '/blog/') === false) {
wp_redirect( home_url( user_trailingslashit( "blog/$post->post_name" ) ), 301 );
exit();
}
}
}
add_filter( 'template_redirect', 'redirect_old_urls' );
If you ended up here, because you have set a custom permalink prefix (for example: /articles/%postname%/
) for your blog posts (which results in: yoursite.com/articles/lorem-ipsum-dolor
), but it also gets applied to your custom post types too (resulting in: yoursite.com/articles/customposttype/dolor-sit-amet
), the easiest and most conventional solution is to set the with_front
argument to false
on the CPTs.
If you are using ACF, you can find this in:
Edit Post Type -> Advanced Settings -> URLs -> Front URL Prefix (set this to false
)
本文标签: adminPermalinks Question Adding a prefix ONLY in front of the posts
版权声明:本文标题:admin - Permalinks Question: Adding a prefix ONLY in front of the posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736639201a1945955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论