Tạo file hiển thị custom post type & custom taxonomy, register_post_type (ok)

Đọc thêm: https://wordpress-lionel.gitbook.io/wordpress/cach-de-xem-cac-quy-luat-rule-lay-gia-tri-nhu-the-nao-add_rewrite_rule-usdwp_rewrite-usdwp_query-gre để có thêm register_taxonomy

Đọc thêm: https://app.gitbook.com/@wordpress-lionel/s/wordpress-advand/display-page-of-custom-post-type-taxonomy-in-wordpress-ok

wp-content/themes/twentytwentyone/functions.php

/**
 * Registers the `zombie` post type.
 */
function zombie_init() {
  register_post_type( 'zombie', array(
    'labels'                => array(
      'name'                  => __( 'Zombies', 'YOUR-TEXTDOMAIN' ),
      'singular_name'         => __( 'Zombie', 'YOUR-TEXTDOMAIN' ),
      'all_items'             => __( 'All Zombies', 'YOUR-TEXTDOMAIN' ),
      'archives'              => __( 'Zombie Archives', 'YOUR-TEXTDOMAIN' ),
      'attributes'            => __( 'Zombie Attributes', 'YOUR-TEXTDOMAIN' ),
      'insert_into_item'      => __( 'Insert into zombie', 'YOUR-TEXTDOMAIN' ),
      'uploaded_to_this_item' => __( 'Uploaded to this zombie', 'YOUR-TEXTDOMAIN' ),
      'featured_image'        => _x( 'Featured Image', 'zombie', 'YOUR-TEXTDOMAIN' ),
      'set_featured_image'    => _x( 'Set featured image', 'zombie', 'YOUR-TEXTDOMAIN' ),
      'remove_featured_image' => _x( 'Remove featured image', 'zombie', 'YOUR-TEXTDOMAIN' ),
      'use_featured_image'    => _x( 'Use as featured image', 'zombie', 'YOUR-TEXTDOMAIN' ),
      'filter_items_list'     => __( 'Filter zombies list', 'YOUR-TEXTDOMAIN' ),
      'items_list_navigation' => __( 'Zombies list navigation', 'YOUR-TEXTDOMAIN' ),
      'items_list'            => __( 'Zombies list', 'YOUR-TEXTDOMAIN' ),
      'new_item'              => __( 'New Zombie', 'YOUR-TEXTDOMAIN' ),
      'add_new'               => __( 'Add New', 'YOUR-TEXTDOMAIN' ),
      'add_new_item'          => __( 'Add New Zombie', 'YOUR-TEXTDOMAIN' ),
      'edit_item'             => __( 'Edit Zombie', 'YOUR-TEXTDOMAIN' ),
      'view_item'             => __( 'View Zombie', 'YOUR-TEXTDOMAIN' ),
      'view_items'            => __( 'View Zombies', 'YOUR-TEXTDOMAIN' ),
      'search_items'          => __( 'Search zombies', 'YOUR-TEXTDOMAIN' ),
      'not_found'             => __( 'No zombies found', 'YOUR-TEXTDOMAIN' ),
      'not_found_in_trash'    => __( 'No zombies found in trash', 'YOUR-TEXTDOMAIN' ),
      'parent_item_colon'     => __( 'Parent Zombie:', 'YOUR-TEXTDOMAIN' ),
      'menu_name'             => __( 'Zombies', 'YOUR-TEXTDOMAIN' ),
    ),
    'public'                => true,
    'hierarchical'          => false,
    'show_ui'               => true,
    'show_in_nav_menus'     => true,
    'supports'              => array( 'title', 'editor' ),
    'has_archive'           => true,
    'rewrite'               => true,
    'query_var'             => true,
    'menu_position'         => null,
    'menu_icon'             => 'dashicons-admin-post',
    'show_in_rest'          => true,
    'rest_base'             => 'zombie',
    'rest_controller_class' => 'WP_REST_Posts_Controller',
  ) );

}
add_action( 'init', 'zombie_init' );

/**
 * Sets the post updated messages for the `zombie` post type.
 *
 * @param  array $messages Post updated messages.
 * @return array Messages for the `zombie` post type.
 */
function zombie_updated_messages( $messages ) {
  global $post;
  $permalink = get_permalink( $post );
  $messages['zombie'] = array(
    0  => '', // Unused. Messages start at index 1.
    /* translators: %s: post permalink */
    1  => sprintf( __( 'Zombie updated. <a target="_blank" href="%s">View zombie</a>', 'YOUR-TEXTDOMAIN' ), esc_url( $permalink ) ),
    2  => __( 'Custom field updated.', 'YOUR-TEXTDOMAIN' ),
    3  => __( 'Custom field deleted.', 'YOUR-TEXTDOMAIN' ),
    4  => __( 'Zombie updated.', 'YOUR-TEXTDOMAIN' ),
    /* translators: %s: date and time of the revision */
    5  => isset( $_GET['revision'] ) ? sprintf( __( 'Zombie restored to revision from %s', 'YOUR-TEXTDOMAIN' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    /* translators: %s: post permalink */
    6  => sprintf( __( 'Zombie published. <a href="%s">View zombie</a>', 'YOUR-TEXTDOMAIN' ), esc_url( $permalink ) ),
    7  => __( 'Zombie saved.', 'YOUR-TEXTDOMAIN' ),
    /* translators: %s: post permalink */
    8  => sprintf( __( 'Zombie submitted. <a target="_blank" href="%s">Preview zombie</a>', 'YOUR-TEXTDOMAIN' ), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ),
    /* translators: 1: Publish box date format, see https://secure.php.net/date 2: Post permalink */
    9  => sprintf( __( 'Zombie scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview zombie</a>', 'YOUR-TEXTDOMAIN' ),
      date_i18n( __( 'M j, Y @ G:i', 'YOUR-TEXTDOMAIN' ), strtotime( $post->post_date ) ), esc_url( $permalink ) ),
    /* translators: %s: post permalink */
    10 => sprintf( __( 'Zombie draft updated. <a target="_blank" href="%s">Preview zombie</a>', 'YOUR-TEXTDOMAIN' ), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ),
  );
  return $messages;
}
add_filter( 'post_updated_messages', 'zombie_updated_messages' );
/**
 * Registers the `zombie_speed` taxonomy,
 * for use with 'zombie'.
 */
function zombie_speed_init() {
  register_taxonomy( 'zombie-speed', array( 'zombie' ), array(
    'hierarchical'      => false,
    'public'            => true,
    'show_in_nav_menus' => true,
    'show_ui'           => true,
    'show_admin_column' => false,
    'query_var'         => true,
    'rewrite'           => true,
    'capabilities'      => array(
      'manage_terms'  => 'edit_posts',
      'edit_terms'    => 'edit_posts',
      'delete_terms'  => 'edit_posts',
      'assign_terms'  => 'edit_posts',
    ),
    'labels'            => array(
      'name'                       => __( 'Zombie speeds', 'YOUR-TEXTDOMAIN' ),
      'singular_name'              => _x( 'Zombie speed', 'taxonomy general name', 'YOUR-TEXTDOMAIN' ),
      'search_items'               => __( 'Search Zombie speeds', 'YOUR-TEXTDOMAIN' ),
      'popular_items'              => __( 'Popular Zombie speeds', 'YOUR-TEXTDOMAIN' ),
      'all_items'                  => __( 'All Zombie speeds', 'YOUR-TEXTDOMAIN' ),
      'parent_item'                => __( 'Parent Zombie speed', 'YOUR-TEXTDOMAIN' ),
      'parent_item_colon'          => __( 'Parent Zombie speed:', 'YOUR-TEXTDOMAIN' ),
      'edit_item'                  => __( 'Edit Zombie speed', 'YOUR-TEXTDOMAIN' ),
      'update_item'                => __( 'Update Zombie speed', 'YOUR-TEXTDOMAIN' ),
      'view_item'                  => __( 'View Zombie speed', 'YOUR-TEXTDOMAIN' ),
      'add_new_item'               => __( 'Add New Zombie speed', 'YOUR-TEXTDOMAIN' ),
      'new_item_name'              => __( 'New Zombie speed', 'YOUR-TEXTDOMAIN' ),
      'separate_items_with_commas' => __( 'Separate zombie speeds with commas', 'YOUR-TEXTDOMAIN' ),
      'add_or_remove_items'        => __( 'Add or remove zombie speeds', 'YOUR-TEXTDOMAIN' ),
      'choose_from_most_used'      => __( 'Choose from the most used zombie speeds', 'YOUR-TEXTDOMAIN' ),
      'not_found'                  => __( 'No zombie speeds found.', 'YOUR-TEXTDOMAIN' ),
      'no_terms'                   => __( 'No zombie speeds', 'YOUR-TEXTDOMAIN' ),
      'menu_name'                  => __( 'Zombie speeds', 'YOUR-TEXTDOMAIN' ),
      'items_list_navigation'      => __( 'Zombie speeds list navigation', 'YOUR-TEXTDOMAIN' ),
      'items_list'                 => __( 'Zombie speeds list', 'YOUR-TEXTDOMAIN' ),
      'most_used'                  => _x( 'Most Used', 'zombie-speed', 'YOUR-TEXTDOMAIN' ),
      'back_to_items'              => __( '&larr; Back to Zombie speeds', 'YOUR-TEXTDOMAIN' ),
    ),
    'show_in_rest'      => true,
    'rest_base'         => 'zombie-speed',
    'rest_controller_class' => 'WP_REST_Terms_Controller',
  ) );

}
add_action( 'init', 'zombie_speed_init' );

/**
 * Sets the post updated messages for the `zombie_speed` taxonomy.
 *
 * @param  array $messages Post updated messages.
 * @return array Messages for the `zombie_speed` taxonomy.
 */
function zombie_speed_updated_messages( $messages ) {

  $messages['zombie-speed'] = array(
    0 => '', // Unused. Messages start at index 1.
    1 => __( 'Zombie speed added.', 'YOUR-TEXTDOMAIN' ),
    2 => __( 'Zombie speed deleted.', 'YOUR-TEXTDOMAIN' ),
    3 => __( 'Zombie speed updated.', 'YOUR-TEXTDOMAIN' ),
    4 => __( 'Zombie speed not added.', 'YOUR-TEXTDOMAIN' ),
    5 => __( 'Zombie speed not updated.', 'YOUR-TEXTDOMAIN' ),
    6 => __( 'Zombie speeds deleted.', 'YOUR-TEXTDOMAIN' ),
  );

  return $messages;
}
add_filter( 'term_updated_messages', 'zombie_speed_updated_messages' );

wp-content/themes/twentytwentyone/taxonomy-zombie-speed.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();

$description = get_the_archive_description();
?>
<?php if ( have_posts() ) : ?>

    <header class="page-header alignwide">
      <?php the_archive_title( '<h1 class="page-title">', '</h1>' ); ?>
      <?php if ( $description ) : ?>
          <div class="archive-description"><?php echo wp_kses_post( wpautop( $description ) ); ?></div>
      <?php endif; ?>
    </header><!-- .page-header -->

  <?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(); ?>

wp-content/themes/twentytwentyone/single-zombie.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');

	if (is_attachment()) {
		// Parent post navigation.
		the_post_navigation(
			array(
				/* translators: %s: Parent post link. */
				'prev_text' => sprintf(__('<span class="meta-nav">Published in</span><span class="post-title">%s</span>', 'twentytwentyone'), '%title'),
			)
		);
	}

	// If comments are open or there is at least one comment, load up the comment template.
	if (comments_open() || get_comments_number()) {
		comments_template();
	}

	// Previous/next post navigation.
	$twentytwentyone_next = is_rtl() ? twenty_twenty_one_get_icon_svg('ui', 'arrow_left') : twenty_twenty_one_get_icon_svg('ui', 'arrow_right');
	$twentytwentyone_prev = is_rtl() ? twenty_twenty_one_get_icon_svg('ui', 'arrow_right') : twenty_twenty_one_get_icon_svg('ui', 'arrow_left');

	$twentytwentyone_next_label = esc_html__('Next post', 'twentytwentyone');
	$twentytwentyone_previous_label = esc_html__('Previous post', 'twentytwentyone');

	the_post_navigation(
		array(
			'next_text' => '<p class="meta-nav">' . $twentytwentyone_next_label . $twentytwentyone_next . '</p><p class="post-title">%title</p>',
			'prev_text' => '<p class="meta-nav">' . $twentytwentyone_prev . $twentytwentyone_previous_label . '</p><p class="post-title">%title</p>',
		)
	);
endwhile; // End of the loop.

get_footer();

Last updated