在帖子URL中包含分类插件

时间:2017-01-29 作者:Kenan

我的postname:127 Hours

我的分类名称:Actors

我为帖子添加了分类名称:James Franco

我的帖子链接:http://website。com/127-hours.html

我想:http://website。com/127-hours-james-franco.html

我想在url中添加自定义分类法值。

我的functions.php 代码(用于参与者分类)

function add_custom_taxonomies()
{
    register_taxonomy(\'actor\', \'post\', array(
        \'hierarchical\' => false,
        \'labels\' => array(
            \'name\' => _x(\'Actor\', \'taxonomy general name\') ,
            \'singular_name\' => _x(\'Actor\', \'taxonomy singular name\') ,
            \'search_items\' => __(\'Actor Search\') ,
            \'all_items\' => __(\'All Actors\') ,
            \'edit_item\' => __(\'Edit Actor\') ,
            \'update_item\' => __(\'Update Actor\') ,
            \'add_new_item\' => __(\'Add New Actor\') ,
            \'new_item_name\' => __(\'New Actor Name\') ,
            \'menu_name\' => __(\'Actors\') ,
        ) ,
        \'rewrite\' => array(
            \'slug\' => \'actor\', // This controls the base slug that will display before each term
            \'with_front\' => false, // Don\'t display the category base before "/locations/"
            \'hierarchical\' => false

        ) ,
    ));
}

add_action(\'init\', \'add_custom_taxonomies\', 0);

1 个回复
SO网友:Phaze Phusion

在WordPress Permalinks中添加自定义分类标记,您希望做的是在Permalink结构中使用您的分类,例如:。/%actor%/%postname%

首先,您要像之前一样编写分类法,但需要额外的字段:

add_action(\'init\', \'add_actor_taxonomy\');
 
function add_actor_taxonomy() {
  if (!is_taxonomy(\'actor\')) {
    register_taxonomy( \'actor\', \'post\', 
      array(
        \'hierarchical\' => FALSE,
        \'label\' => __(\'actor\'),
        \'public\' => TRUE,
        \'show_ui\' => TRUE,
        \'query_var\' => \'actor\',
        \'rewrite\' => true, // this adds the tag %actor% to WordPress
      )
    );
  }
}
即使已将%actor%标记添加到WordPress中,在您告诉WordPress如何翻译该标记之前,它也不会起作用。

// The post_link hook allows us to translate tags for regular post objects
add_filter(\'post_link\', \'actor_permalink\', 10, 3);
// The post_type_link hook allows us to translate tags for custom post type objects
add_filter(\'post_type_link\', \'actor_permalink\', 10, 3);
 
function actor_permalink($permalink, $post_id, $leavename) {
  // If the permalink does not contain the %actor% tag, then we don’t need to translate anything.
  if (strpos($permalink, \'%actor%\') === FALSE) return $permalink;
   
    // Get post
    $post = get_post($post_id);
    if (!$post) return $permalink;
 
    // Get the actor terms related to the current post object.
    $terms = wp_get_object_terms($post->ID, \'actor\');
    
    // Retrieve the slug value of the first actor custom taxonomy object linked to the current post.
    if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) )
      $taxonomy_slug = $terms[0]->slug;
    // If no actor terms are retrieved, then replace our actor tag with the value "actor"
    else
      $taxonomy_slug = \'actor\';
 
  // Replace the %actor% tag with our custom taxonomy slug
  return str_replace(\'%actor%\', $taxonomy_slug, $permalink);
}
使用如下自定义permalink结构

/%actor%/%postname%
您的新帖子链接如下所示

http://website.com/james-franco/127-hours
参考号:http://shibashake.com/wordpress-theme/add-custom-taxonomy-tags-to-your-wordpress-permalinks

相关推荐

Force pretty permalinks?

我正在构建一个插件,该插件将用于单个站点,并依赖于add_rewrite_rule 要工作,需要打开永久链接。打开它们并不困难,因为它只是一个站点,但我担心其中一个管理员可能会在不知道自己在做什么的情况下关闭它,并破坏该站点。如何以编程方式强制保持漂亮的永久链接?