Thay đổi Custom Post Type && Custom Taxonomies khi thay đổi ngôn ngữ Phần 1 (ok)
Phần 1 ta mới chỉ giải quyết được Custom Post Type thay đổi khi ngôn ngữ thay đổi Phần 2 hiện tại có 1 số ví dụ để giải quyết tiếp Taxonomies
C:\xampp\htdocs\wordpress1\wp-content\themes\twentytwentyone\functions.php
// ===========
add_action('init', 'my_realisaties_cpt');
function my_realisaties_cpt() {
$labels = array(
'name' => _x('Realisaties', 'post type general name', 'your-plugin-textdomain'),
'singular_name' => _x('Realisatie', 'post type singular name', 'your-plugin-textdomain'),
'menu_name' => _x('Realisaties', 'admin menu', 'your-plugin-textdomain'),
'name_admin_bar' => _x('Realisatie', 'add new on admin bar', 'your-plugin-textdomain'),
'add_new' => _x('Add New', 'Realisatie', 'your-plugin-textdomain'),
'add_new_item' => __('Add New Realisatie', 'your-plugin-textdomain'),
'new_item' => __('New Realisatie', 'your-plugin-textdomain'),
'edit_item' => __('Edit Realisatie', 'your-plugin-textdomain'),
'view_item' => __('View Realisatie', 'your-plugin-textdomain'),
'all_items' => __('All Realisaties', 'your-plugin-textdomain'),
'search_items' => __('Search Realisaties', 'your-plugin-textdomain'),
'parent_item_colon' => __('Parent Realisaties:', 'your-plugin-textdomain'),
'not_found' => __('No realisaties found.', 'your-plugin-textdomain'),
'not_found_in_trash' => __('No realisaties found in Trash.', 'your-plugin-textdomain'),
);
$args = array(
'labels' => $labels,
'description' => __('Description.', 'your-plugin-textdomain'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'realisaties'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'show_in_rest' => true,
'rest_base' => 'realisaties',
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments','custom-fields'),
);
register_post_type('realisaties', $args);
flush_rewrite_rules(true);
}
// ===========
add_action('init', 'my_realisaties_taxonomy', 30);
function my_realisaties_taxonomy() {
$labels = array(
'name' => _x('Realisaties', 'taxonomy general name'),
'singular_name' => _x('Realisatie', 'taxonomy singular name'),
'search_items' => __('Search Realisaties'),
'all_items' => __('All Realisaties'),
'parent_item' => __('Parent Realisatie'),
'parent_item_colon' => __('Parent Realisatie:'),
'edit_item' => __('Edit Realisatie'),
'update_item' => __('Update Realisatie'),
'add_new_item' => __('Add New Realisatie'),
'new_item_name' => __('New Genre Realisatie'),
'menu_name' => __('Realisatie Taxonomy'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'realisaties'),
'show_in_rest' => true,
'rest_base' => 'realisatiesabc',
);
register_taxonomy('realisatiesabc', array('realisaties'), $args);
flush_rewrite_rules(true);
}
C:\xampp\htdocs\wordpress1\wp-content\themes\twentytwentyone\taxonomy-realisatiesabc.php
<?php
/**
* The template for displaying archive pages
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package WordPress
* @subpackage Twenty_Twenty_One
* @since Twenty Twenty-One 1.0
*/
get_header();
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php get_template_part( 'template-parts/content/content', get_theme_mod( 'display_excerpt_or_full_post', 'excerpt' ) ); ?>
<?php endwhile; ?>
<?php twenty_twenty_one_the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content/content-none' ); ?>
<?php endif; ?>
<?php get_footer(); ?>
C:\xampp\htdocs\wordpress1\wp-content\themes\twentytwentyone\single-realisaties.php
<?php
/**
* The template for displaying all single posts
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* @package WordPress
* @subpackage Twenty_Twenty_One
* @since Twenty Twenty-One 1.0
*/
get_header();
/* Start the Loop */
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content/content-single' );
endwhile; // End of the loop.
get_footer();
Chưa hoàn thành taxonomy hiện tại chỉ mới đổi được nhưng chưa hoạt động :(
PreviousMột ví dụ kinh điển sử dụng Custom Post Type && Custom Taxonomies dùng chung đường dẫn (ok)NextThay đổi Custom Post Type && Custom Taxonomies khi thay đổi ngôn ngữ Phần 2 (ok)
Last updated
Was this helpful?