我将Yoast Breadcrumbs用于自定义帖子类型和分类法以及WPML作为翻译系统。
WordPress 5.1.1 酵母10 WPML 4.2.4.1
这是我的自定义帖子插件代码:
<?php
/*Plugin Name: Akal CPT for PSJ / cpt-akal Theme
Description: This plugin registers custom post type.
Version: 1.0
License: GPLv2
*/
//CUSTOM TAXONOMY => GENERATION DES CATEGORIES "MH & ACC Types"
function mh_acc_type() {
$labels = array(
'name' => _x( 'MH & ACC Types', 'Taxonomy General Name', 'cpt-akal' ),
'singular_name' => _x( 'MH & ACC Type', 'Taxonomy Singular Name', 'cpt-akal' ),
'menu_name' => __( 'MH & ACC Types', 'cpt-akal' ),
'all_items' => __( 'All Items', 'cpt-akal' ),
'parent_item' => __( 'Parent Item', 'cpt-akal' ),
'parent_item_colon' => __( 'Parent Item:', 'cpt-akal' ),
'new_item_name' => __( 'New Item Name', 'cpt-akal' ),
'add_new_item' => __( 'Add New Item', 'cpt-akal' ),
'edit_item' => __( 'Edit Item', 'cpt-akal' ),
'update_item' => __( 'Update Item', 'cpt-akal' ),
'view_item' => __( 'View Item', 'cpt-akal' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'cpt-akal' ),
'add_or_remove_items' => __( 'Add or remove items', 'cpt-akal' ),
'choose_from_most_used' => __( 'Choose from the most used', 'cpt-akal' ),
'popular_items' => __( 'Popular Items', 'cpt-akal' ),
'search_items' => __( 'Search Items', 'cpt-akal' ),
'not_found' => __( 'Not Found', 'cpt-akal' ),
'no_terms' => __( 'No items', 'cpt-akal' ),
'items_list' => __( 'Items list', 'cpt-akal' ),
'items_list_navigation' => __( 'Items list navigation', 'cpt-akal' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'residentiel',
'hierarchical' => true,
),
);
register_taxonomy( 'mh_acc_type', array( 'mh_accs_post_type' ), $args );
}
add_action( 'init', 'mh_acc_type', 0 );
//CUSTOM POST Type = MH & ACC => GENERATION DES SINGLE POST "MH_ACCS_POST_TYPE"
function akal_cpt_mh_acc() {
$labels = array(
'name' => _x('MH & ACC', 'cpt-akal'),
'singular_name' => _x('MH & ACC', 'cpt-akal'),
'add_new' => _x('Add MH & ACC', 'cpt-akal'),
'add_new_item' => __('Add MH & ACC'),
'edit_item' => __('Edit MH & ACC'),
'new_item' => __('New MH & ACC'),
'view_item' => __('View MH & ACC'),
'search_items' => __('Search MH & ACC'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'taxonomies' => array('mh_acc_type', 'post_tag'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_rest' => true,
'rest_base' => '',
'query_var' => true,
'rewrite' => true,
'capability_type' => 'page',
'hierarchical' => true,
'menu_position' => null,
'has_archive' => 'residentiel/.',
'supports' => array('title', 'editor', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', 'excerpt'),
'rewrite' => array('slug' => _x( 'residentiel/achat-vente-mobil-home-accessoires/%mh_acc_type%', 'cpt-akal' )), //FOR WPML
'_builtin' => false,
);
register_post_type( 'mh_accs_post_type' , $args );
}
add_action('init', 'akal_cpt_mh_acc');
//REDIRECTION %mh_acc_type%
function custom_post_link($post_link, $id = 0)
{
$post = get_post($id);
if($terms = wp_get_object_terms($post->ID, 'mh_acc_type'))
{
$mh_acc_type = $terms[0]->slug;
//Replace the query var surrounded by % with the slug of
//the first taxonomy it belongs to.
return str_replace('%mh_acc_type%', $mh_acc_type, $post_link);
}
//If all else fails, just return the $post_link.
return $post_link;
}
add_filter('post_type_link', 'custom_post_link', 1, 3);
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
$newRules = array();
$newRules['residentiel/(.+)/(.+)/(.+)/?$'] = 'index.php?mh_accs_post_type=$matches[3]';
$newRules['residentiel/(.+)/(.+)/?$'] = 'index.php?mh_acc_type=$matches[2]';
return array_merge($newRules, $rules);
}
//Register template single-mh_accs_post_type.php for all CPT
function load_mh_accs_template($template) {
global
$post;
if ($post->post_type == "mh_accs_post_type" && $template !== locate_template(array("single-mh_accs_post_type.php")))
{
return plugin_dir_path( __FILE__ ) . "single-mh_accs_post_type.php";
}
return $template;
}
add_filter('single_template', 'load_mh_accs_template');
//Register template archives-mh_acc_type.php for MH & ACC Taxonomy Archives
add_filter('taxonomy_template', 'load_mh_acc_type_template');
function load_mh_acc_type_template($template) {
if (is_tax ('mh_acc_type'))
{
$template = plugin_dir_path( __FILE__ ) . "archive-mh_acc_type.php";
}
return $template;
}
这是生成的页面的示例:
如您所见,经过长时间的努力和多次头痛之后,一切工作都很好。当我通过WPML使用翻译时,效果也很好,这里是英文(原文为法文):
除了...在此英语版本中,当我检查Yoast提供的面包屑时,我发现第二级/ residentiel /出了点问题。 / residentiel /是法语版本,因此应该是/ residential /英文,但不是。 我已经使用字符串翻译和自定义分类法完成了所有WPML设置。我以为我的代码在重定向部分有问题。因为/ residentiel /是硬编码的,所以也许我应该添加条件“如果language = eng,请执行此操作”,但是我不够好……或者我正在与WPML或Yoast bug对抗。 WPML将无济于事,Yoast支持也无法解决...
有什么想法/帮助吗? 谢谢 阿卡(Akal)