2012-06-13 91 views
0

我已搜索,here is the closes result.让WordPress自动显示类别固定链接只为特定类别

我正在建立一个新的WordPress的网站。我希望大多数帖子在网址中没有任何类别,只需登录www.site.com/title即可。不过,我确实希望博客帖子是分开的,所以我想要访问www.site.com/blog/title。我还希望将来可以添加更多类似选项,仅限特定类别,而不是整个网站。

在stackoverflow上这里有很多相似的问题,但大多数有0回复。任何建议都会很棒。我甚至没有任何运气试过Advanced Permalinks

回答

7

你可以简单地通过设置>固定链接并添加到通用设置>自定义结构的值/blog/%postname%/。在那里你可以从www.site.com/blog/title获得博客文章。

我不明白第一个问题。通过:

我最想要的职位已经没有类别的URL

你的意思是没有www.site.com/category/category-name?或没有www.site.com/category/post?

编辑#1

要回答这个问题:

www.site.com/category/post是什么,我只是想对博客文章以“博客”>如果该类别的类别“鞋子”我不希望URL中显示类别。 -

第一:您可以将固定链接设置为/%postname%/所以所有的职位将有现场/标题因此从该链接访问

二:你要过滤永久链接行为不同下的职位“博客“类别。

试试这个

add_filter('post_link', 'custom_permalink', 10, 2); 
function custom_permalink($permalink, $post) { 
    // Get the categories for the post 
    $categories = wp_get_post_categories($post->ID); 
    foreach ($categories as $cat) { 
     $post_cat = get_category($cat); 
     $post_cats[] = $post_cat->slug; 
    } 

    // Check if the post have 'blog' category 
    // Assuming that your 'Blog' category slug is 'blog' 
    // Change 'blog' to match yours 
    if (in_array('blog',$post_cats)) { 
     $permalink = trailingslashit(home_url('blog/' . $post->post_name)); 
    } 

    return $permalink; 
} 

第三:你要过滤rewrite_rules

add_filter('rewrite_rules_array', 'custom_rewrite_rule'); 
function custom_rewrite_rule($rules) { 
    $new_rules = array(
     'blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1', 
     'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]', 
     'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]', 
     'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]', 
     'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]' 
    ); 

    $rules = $new_rules + $rules; 

    return $rules; 
} 

转到永久链接设置,并保存设置刷新重写规则,并积极

上述变化注意:将这些功能添加到您的活动主题functions.php模板

注意:我还没有测试过,但这是您更改固定链接的方式。我做了类似的方式来改变我的档案和搜索结果的永久链接。

+0

www.site.com/category/post是我只希望博客文章类别为“博客”,如果类别是“鞋子”我不希望在URL中显示类别。 – Branndon

+0

我已经编辑了我的答案,请检查... – AriePutranto

+0

我没有忘记,我会尽快回复并回复您。我正在移动,所以现在有点忙碌。感谢您抽出宝贵的时间回复,我会尽快回复,并尝试一下您的代码。 – Branndon

相关问题