get all category custom post type wordpress dev
- In your WordPress theme, open the functions.php file.
- Add the following code to register the custom post type:
function create_custom_post_type() {
register_post_type( 'custom_post_type_name',
array(
'labels' => array(
'name' => __( 'Custom Post Types' ),
'singular_name' => __( 'Custom Post Type' )
),
'public' => true,
'has_archive' => true,
)
);
}
add_action( 'init', 'create_custom_post_type' );
After adding the code, save the file.
Create a new file in your theme directory and name it custom-post-type-template.php.
- Add the following code to display the custom post type:
<?php
$args = array( 'post_type' => 'custom_post_type_name', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>
Save the file.
In your WordPress admin panel, navigate to Posts > Categories and create the desired categories for the custom post type.
When creating or editing a custom post, assign the appropriate category to it.
After completing these steps, you will have successfully set up a custom post type with categories in WordPress.