我对以下已完成的代码有些困惑,问题如下:
使用URL的自定义帖子类型页面:
安全性/ ssl证书/%ssl_brand%/ [POST-TYPE-ITEM]
自定义分类法,我需要通过URL进行访问:
安全性/ ssl证书/ [TAXONOMY]
我一直在玩,但似乎无法破解POST-TYPE,当我在自定义分类法中看到品牌显示为的视图链接时,就可以看到
/ security / ssl-certificates / geotrust(geotrust是分类法 品牌)
这似乎很好,但无法在Post-Type URL上运行,尝试添加%ssl_brand%,但显示如下;
security / ssl-certificates /%ssl_brand%/ post-type-item-slug
请参见下面的代码:
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' );
答案 0 :(得分:0)
我自己通过添加此功能(针对需要类似解决方案的任何人)解决了问题:
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);