簡體   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