duanlei2458 2018-08-01 21:57
浏览 66

Wordpress - 包括自定义分类法Slug到自定义Post-Type Slug

I am a little stuck with the following code I have done, the issue is as follows:

Custom Post-Type Page to use URL:

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

Custom Taxonomy I need to be accessible via URL:

security/ssl-certificates/[TAXONOMY]

I have been playing around but cannot seem to crack it for POST-TYPE, when I see the view links for the brands in the custom taxonomy they show as;

/security/ssl-certificates/geotrust (geotrust being the taxonomy brand)

so that seems to be fine but cannot get it working on the Post-Type URL, tried adding %ssl_brand% but shows as this;

security/ssl-certificates/%ssl_brand%/post-type-item-slug

See in the code below:

functions.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' );
  • 写回答

1条回答 默认 最新

  • douge3113 2018-08-01 23:05
    关注

    I worked it out myself with this function addition (for anyone that needs a similar solution):

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题