【问题标题】:Posts permalink with category带有类别的帖子永久链接
【发布时间】:2015-03-07 04:12:25
【问题描述】:

我在我的 Wordpress 帖子中使用永久链接。我的固定链接代码是/%category%/%postname%/ - 所以链接包括类别名称。我的问题是,我正在使用一些类别来对我的帖子进行分组。例如:精选、版主推荐等...我希望这些类别出现在链接中。这可能吗?如果可以,怎么做?

【问题讨论】:

  • 如果您使用的是类别 slug,它应该已经显示...但听起来您正在使用某种类型的模板文件来“对您的帖子进行分组”。如果您可以提供该代码,或者从那里开始...
  • @TimHallman 我的一些帖子有 2 或 3 个类别。我想从永久链接中排除一些类别。
  • 好的,明白了...然后可能需要过滤永久链接。下面我用一个例子来回答。

标签: wordpress permalinks


【解决方案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

【讨论】:

  • 此方法从我的网址中清除类别。但我想更改其他类别的帖子。有可能吗?
  • 是的,您基本上需要使用要删除的类别的数组或字符串加载$catname 变量,然后相应地修复str_replace 函数。
  • 您也可以使用str_replace 更改类别名称。考虑如果您要删除一个名称为“sample”的类别,则使 $catname = 'sample'。如果您有多个类别,则需要将 $catname 设为一个数组,然后使用 str_replace 循环
  • 感谢我创建了用于在帖子中选择其他类别的功能。
【解决方案2】:

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

$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;

}

请记住在这些之后从仪表板保存永久链接设置

【讨论】:

    【解决方案3】:

    但是,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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2018-05-26
      相关资源
      最近更新 更多