簡體   English   中英

Wordpress - 將自定義分類 Slug 包含到自定義 Post-Type Slug 中

[英]Wordpress - Including custom Taxonomy Slug into Custom Post-Type Slug

我正在處理以下代碼。 問題如下:

使用 URL 的自定義帖子類型頁面:

security/ssl-certificates/%ssl_brand%/[POST-TYPE-ITEM]

自定義分類我需要通過 URL 訪問:

安全/ssl-證書/[分類]

我一直在玩,但似乎無法為 POST-TYPE 破解它,當我在自定義分類中看到品牌的視圖鏈接時;

/security/ssl-certificates/geotrust(geotrust 是分類法品牌)

所以這似乎很好,但不能讓它在 Post-Type URL 上工作,嘗試添加 %ssl_brand% 但顯示如下;

安全/ssl-證書/%ssl_brand%/post-type-item-slug

請參閱下面的代碼:

函數.php

function cptui_register_my_cpts_ssl_certificates() {

    /**
     * Post Type: SSL Certificates.
     */

    $labels = array(
        "name" => __( "SSL Certificates", "foxhost-child" ),
        "singular_name" => __( "SSL Certificate", "foxhost-child" ),
        "all_items" => __( "SSL Certificates", "foxhost-child" ),
        "add_new" => __( "Add New SSL", "foxhost-child" ),
        "add_new_item" => __( "Add New SSL", "foxhost-child" ),
        "edit_item" => __( "Edit SSL", "foxhost-child" ),
        "new_item" => __( "New SSL", "foxhost-child" ),
        "view_item" => __( "View SSL", "foxhost-child" ),
        "view_items" => __( "View All SSL's", "foxhost-child" ),
        "search_items" => __( "Search SSL", "foxhost-child" ),
        "not_found" => __( "No SSL's Found", "foxhost-child" ),
        "not_found_in_trash" => __( "No SSL's found in Trash", "foxhost-child" ),
        "parent_item_colon" => __( "security,products", "foxhost-child" ),
        "parent_item_colon" => __( "security,products", "foxhost-child" ),
    );

    $args = array(
        "label" => __( "SSL Certificates", "foxhost-child" ),
        "labels" => $labels,
        "description" => "SSL Certificate Products",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => false,
        "show_in_menu" => "edit.php?post_type=products",
        "show_in_nav_menus" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => array( "slug" => "security/ssl-certificates/%ssl_brand%", "with_front" => false ),
        "query_var" => "ssl_brand",
        "menu_icon" => "dashicons-lock",
        "supports" => array( "title", "editor", "revisions" ),
        "taxonomies" => array( "ssl_brand" ),
    );

    register_post_type( "ssl_certificates", $args );
}

add_action( 'init', 'cptui_register_my_cpts_ssl_certificates' );




function cptui_register_my_taxes_ssl_brand() {

    /**
     * Taxonomy: SSL Brands.
     */

    $labels = array(
        "name" => __( "SSL Brands", "foxhost-child" ),
        "singular_name" => __( "Brand", "foxhost-child" ),
    );

    $args = array(
        "label" => __( "SSL Brands", "foxhost-child" ),
        "labels" => $labels,
        "public" => true,
        "hierarchical" => false,
        "label" => "SSL Brands",
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => array( 'slug' => 'security/ssl-certificates', 'with_front' => false, ),
        "show_admin_column" => true,
        "show_in_rest" => false,
        "rest_base" => "ssl_brand",
        "show_in_quick_edit" => false,
    );
    register_taxonomy( "ssl_brand", array( "ssl_certificates" ), $args );
}

add_action( 'init', 'cptui_register_my_taxes_ssl_brand' );

我自己解決了這個功能添加(對於需要類似解決方案的任何人):

function build_ssl_url_with_brand( $post_link, $id = 0, $leavename = FALSE ) {
  if ( strpos('%ssl_brand%', $post_link) === 'FALSE' ) {
    return $post_link;
  }
  $post = get_post($id);
  if ( !is_object($post) || $post->post_type != 'ssl_certificates' ) {
    return $post_link;
  }
  $terms = wp_get_object_terms($post->ID, 'ssl_brand');
  if ( !$terms ) {
    return str_replace('security/ssl-certificates/%ssl_brand%/', '', $post_link);
  }
  return str_replace('%ssl_brand%', $terms[0]->slug, $post_link);
}
add_filter('post_type_link', 'build_ssl_url_with_brand', 10, 2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM