繁体   English   中英

Wordpress 永久链接重写,用于自定义分类法和在 URL 中使用父项和子项层次结构的帖子类型

[英]Wordpress permalink rewrite for custom taxonomy and post type that uses parent and child terms hierarchy in URLs

我已经注册了一个自定义分类法和帖子类型来处理我网站上照片库的显示。 我需要将永久链接设置为如下所示:domain.com/photos/region/country/location 或 domain.com/photos/country/location,具体取决于为每种帖子类型选择的条款。 地区和国家术语将取决于父术语是CanadaUnited States还是World

一个例子:

  • 加拿大(父项)
    • 阿尔伯塔省(儿童学期)
  • 美国(母语)
    • 亚利桑那州(儿童学期)
  • 世界(父项)
    • 法国(儿童学期)

注册分类代码

register_taxonomy( 'photo_galleries', array( 'photos' ),
  array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array(
      'slug' => 'galleries',
      'with_front' => false
    ),
));

自定义帖子类型 arguments

$args = array(
  'supports' => $supports,
  'labels' => $labels,
  'hierarchical' => true,
  'public' => true,
  'rewrite' => array(
    'slug' => 'photos',
    'with_front' => false
  ),
  'menu_position' => 5,
  'menu_icon' => 'dashicons-format-gallery',
  'has_archive' => true,
);
register_post_type( 'photos', $args );

画廊 post_type_link function

function galleries_post_type_link( $url, $post ) {
  if ( $post->post_type == 'photos' ) {
    global $post;
    $terms = get_the_terms( $post->id, 'photo_galleries' );
    $term = $terms[0]->slug;
    $url = str_replace('photos/', 'photos/' . $term . '/', $url);
  }
  return $url;
}
add_filter('post_type_link', 'galleries_post_type_link', 10, 4);

现在,此代码在管理面板自定义帖子编辑器中显示以下永久链接结构: domain.com/photos/france/bordeaux/ 这篇文章的父项是World ,子项是France France已设置为主要术语,但将World设为主要术语对永久链接没有影响。 如您所见,法国仅在永久链接中使用。

我尝试了以下自定义永久链接结构但没有成功: /%category%/%postname%//%postname%/

任何帮助是极大的赞赏。

干杯,

Wordpress 永久链接非常敏感,请在推送之前进行尽职调查。

您无需更改设置中的永久链接,将其保留为/%postname%/

在您的function.php中,让我们添加以下内容,以便在我们弄乱它们实际更新的永久链接时做到这一点。

/**
* Remove rewrite rules and then recreate rewrite rules.
* @link https://developer.wordpress.org/reference/functions/flush_rewrite_rules/
* Should be removed before pushing to live.
*/
flush_rewrite_rules();
add_action( 'after_switch_theme', 'flush_rewrite_rules' );

让我们创建一个名为Photos的自定义帖子类型和一个名为Locations的自定义分类。

/**
* Register question custom post type.
* @link https://developer.wordpress.org/reference/functions/register_post_type/
*/
add_action( 'init', 'wp_so66575072_custom_post_type' );
function wp_so66575072_custom_post_type() {
    $args = [
        'label' => 'Photos',
        'labels' => [ 
            'singular_name' => 'Photo', 
        ],
        'public' => true,
        'show_in_rest' => true, //Enable Gutenberg
        'menu_position' => -1,
        'menu_icon' => 'dashicons-editor-quote',
        'capability_type' => 'post',
        'taxonomies' => [ 'locations', ],
        'has_archive' => true,
        'delete_with_user' => false,
        'rewrite' => [
            'slug' => 'photos/%locations%',
        ],
    ];
    register_post_type( 'photos', $args );
};

/**
* Register locations custom taxonomy.
* @link https://developer.wordpress.org/reference/functions/register_taxonomy/
*/
add_action( 'init', 'wp_so66575072_custom_taxonomy' );
function wp_so66575072_custom_taxonomy() {
    $args = [
        'labels' => [
            'name' => 'Locations',
            'singular_name' => 'Location' 
        ],
        'hierarchical' => true,
        'show_in_rest' => true, //Enable Gutenberg
        'rewrite' => [
            'slug' => 'locations',
            'hierarchical' => true,
        ],
    ];
    register_taxonomy( 'locations', [ 'photos' ], $args );
};

我们需要用我们的条款替换我们的自定义分类句柄 %location%。

/**
* Replace custom taxonomy permalink handle.
* @link https://developer.wordpress.org/reference/hooks/post_type_link/
*/
add_filter( 'post_type_link', 'wp_so66575072_post_type_link' );
function wp_so66575072_post_type_link( $post_link ) {
    $taxonomy = 'locations';
    //@link https://developer.wordpress.org/reference/functions/get_the_terms/
    $terms = get_the_terms( get_the_ID(), $taxonomy );
    $slug = [];
    foreach ( $terms as $term ) {
        //@link https://developer.wordpress.org/reference/functions/sanitize_title_with_dashes/
        if ( $term->parent == 0 ) {
            array_unshift( $slug, sanitize_title_with_dashes( $term->name ) );
        } else {
            array_push( $slug, sanitize_title_with_dashes( $term->name ) );
        };
    };
    if ( ! empty( $slug ) ) {
        return str_replace( '%' . $taxonomy . '%' , join( '/', $slug ) , $post_link );
    }
    return $post_link;  
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM