帖子永久链接与类别

时间:2015-01-08 22:08:58

标签: wordpress permalinks

我在Wordpress帖子上使用永久链接。我的固定链接代码为/%category%/%postname%/ - 因此链接包含类别名称。我的问题是,我使用了一些类别来分组我的帖子。例如:特色,主持人推荐等......我希望这些类别出现在链接中。这是可能的,如果是的话怎么办呢?

3 个答案:

答案 0 :(得分:1)

add_filter( 'post_link', 'filter_permalink_categories', 10, 3 );

function filter_permalink_categories( $permalink, $post, $catname) {

    if( $post->filter == $catname ) {

        $permalink = str_replace( $catname, '', $permalink );

    }

    return $permalink;

}

这可能不是您所需要的,但应该让您知道如何过滤永久链接。

此外,您可能想要查看category_link过滤器。抱歉,Wordpress CODEX是无声的heres the link though

答案 1 :(得分:1)

我更改了其他类别的帖子。

$disallow = array("category-slug-1", "category-slug-2");
function selectCategory($postID){
    global $disallow;
    $cats=wp_get_post_categories($postID);
    foreach ($cats as $key => $value) {
        $current = get_category($value);
        if(!in_array($current->slug, $disallow)){
            return $current->slug;
        }
    }
}

用这个永久链接代替:

add_filter( 'post_link', 'filter_permalink_categories', 10, 3 );
function filter_permalink_categories( $permalink, $post, $catname) {
    global $disallow;
    foreach ($disallow as $key => $value) {
        $permalink = str_replace( $disallow, selectCategory($post->ID), $permalink );
    }
    return $permalink;

}

请记住仪表板中的保存永久链接设置

答案 2 :(得分:1)

  

但是,WordPress团队一直在对此进行改进,并且   现在,它不仅可以包含一个,而且可以包含多个类别   在URL内。在本教程中,我们将向您展示如何制作它   发生。

WordPress Permalinks with Multiple Categories

function multiple_category_post_link($url = '')
{  
  // check permalink structure for the required construct; /%category%/%postname%/ 
  if (strrpos(get_option('permalink_structure'), '%category%/%postname%') !== false)
  {
    // get the current post
    global $post, $wp_query;

    // prepare variables for use below
    $post_id = $cat_id = 0;
    $new_url = '';

    // for categories
    if (is_category()) 
    {
      // remember current category and post
      $cat_id = get_query_var('cat'); 
      $post_id = $post->ID;

      // add the post slug to the current url
      $new_url = $_SERVER['REQUEST_URI'] . $post->post_name;   
    }

    // for single posts 
    else if (is_single()) 
    {
      // last part in the 'category_name' should be the slug for the current category
      $cat_slug = array_pop(explode('/', get_query_var('category_name')));
      $cat = get_category_by_slug($cat_slug);

      // remember current category and post
      $post_id = $wp_query->post->ID;     
      if ($cat) $cat_id = $cat->cat_ID;

      // replace the slug of the post being viewed by the slug of $post
      $new_url = str_replace('/' . get_query_var('name'), '', $_SERVER['REQUEST_URI']) . $post->post_name;
    } 

    if ($post_id > 0 && $cat_id > 0 && !empty($new_url))
    {
      // make sure categories match!
      foreach(get_the_category($post_id) as $cat)
      {
        if ($cat->cat_ID == $cat_id)
        {
          $url = $new_url;
          break;
        } 
      }  
    }    
  }   

  // always return an url!
  return $url;
} 
add_filter('post_link', 'multiple_category_post_link');