Custom post type plugin with category, add code in function.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | function custom_post_type() { // Set UI labels for Custom Post Type $labels = array( 'name' => _x( 'vips PC Slider', 'Post Type General Name', 'storefront' ), 'singular_name' => _x( 'vips-slider', 'Post Type Singular Name', 'storefront' ), 'menu_name' => __( 'vips PC Slider', 'storefront' ), 'parent_item_colon' => __( 'Parent Slider', 'storefront' ), 'all_items' => __( 'All Slider', 'storefront' ), 'view_item' => __( 'View Slider', 'storefront' ), 'add_new_item' => __( 'Add New Slide', 'storefront' ), 'add_new' => __( 'Add New', 'storefront' ), 'edit_item' => __( 'Edit Slide', 'storefront' ), 'update_item' => __( 'Update Slider', 'storefront' ), 'search_items' => __( 'Search Slider', 'storefront' ), 'not_found' => __( 'Not Found', 'storefront' ), 'not_found_in_trash' => __( 'Not found in Trash', 'storefront' ), ); // Set other options for Custom Post Type $args = array( 'label' => __( 'vips-slider', 'storefront' ), 'description' => __( 'vips Slider', 'storefront' ), 'labels' => $labels, // Features this CPT supports in Post Editor 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ), // You can associate this CPT with a taxonomy or custom taxonomy. 'taxonomies' => array( 'category' ), 'menu_icon' => 'dashicons-welcome-view-site', /* A hierarchical CPT is like Pages and can have * Parent and child items. A non-hierarchical CPT * is like Posts. */ 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'menu_position' => 2, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'page', ); // Registering your Custom Post Type register_post_type( 'vips-slider', $args ); } /* Hook into the 'init' action so that the function * Containing our post type registration is not * unnecessarily executed. */ add_action( 'init', 'custom_post_type', 0 ); |
Display in template (where you want to display or use)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php $args = array( 'post_type' => 'vips-slider', 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => 'home-vipsaudio-products', ), ), ); // Query the posts: $obituary_query = new WP_Query($args); // Loop through the obituaries: while ($obituary_query->have_posts()) : $obituary_query->the_post(); // Echo some markup echo '<p>'; // As with regular posts, you can use all normal display functions, such as the_title(); // Within the loop, you can access custom fields like so: echo get_post_meta($post->ID, 'slider_type', true); // Or like so: $slider_type = get_post_custom_values('slider_type'); echo $slider_type[0]; echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo get_post_meta($post->ID, 'home-vipsaudio-products-link', true); echo '</p>'; // Markup closing tags. endwhile; // Reset Post Data wp_reset_postdata(); ?> |