// Register the custom post type "Sell To US" function create_sell_to_us_post_type() { $labels = array( 'name' => _x('Sell To US', 'Post Type General Name'), 'singular_name' => _x('Sell To US', 'Post Type Singular Name'), 'menu_name' => _x('Sell To US', 'Admin Menu text'), 'name_admin_bar' => _x('Sell To US', 'Add New on Toolbar'), 'archives' => __('Sell To US Archives'), 'attributes' => __('Sell To US Attributes'), 'parent_item_colon' => __('Parent Sell To US:'), 'all_items' => __('All Sell To US'), 'add_new_item' => __('Add New Sell To US'), 'add_new' => __('Add New'), 'new_item' => __('New Sell To US'), 'edit_item' => __('Edit Sell To US'), 'update_item' => __('Update Sell To US'), 'view_item' => __('View Sell To US'), 'view_items' => __('View Sell To US'), 'search_items' => __('Search Sell To US'), 'not_found' => __('Not found'), 'not_found_in_trash' => __('Not found in Trash'), 'featured_image' => __('Featured Image'), 'set_featured_image' => __('Set featured image'), 'remove_featured_image' => __('Remove featured image'), 'use_featured_image' => __('Use as featured image'), 'insert_into_item' => __('Insert into Sell To US'), 'uploaded_to_this_item' => __('Uploaded to this Sell To US'), 'items_list' => __('Sell To US list'), 'items_list_navigation' => __('Sell To US list navigation'), 'filter_items_list' => __('Filter Sell To US list'), ); $args = array( 'labels' => $labels, 'label' => __('Sell To US'), 'description' => __(''), 'menu_icon' => 'dashicons-admin-home', 'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'page-attributes', 'post-formats'), 'taxonomies' => array(), // Add the custom taxonomy to the post type 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => true, 'hierarchical' => true, 'exclude_from_search' => true, 'show_in_rest' => true, 'publicly_queryable' => true, 'capability_type' => 'post', 'rewrite' => array('slug' => 'sell_cat', 'with_front' => false), ); register_post_type('sell-to-us', $args); $taxonomy_args = array( 'labels' => array( 'name' => 'Category', 'singular_name' => 'Category', 'menu_name' => 'Category', ), 'hierarchical' => true, 'rewrite' => array('slug' => 'sell_cat', 'with_front' => false), ); register_taxonomy('sell_cat', array('sell-to-us'), $taxonomy_args); } add_action('init', 'create_sell_to_us_post_type'); function sell_to_us_loop($atts) { ob_start(); $categories = get_terms( array( 'taxonomy' => 'sell_cat', 'order' => 'asc' ) ); ?> <div class="row sellus"> <?php foreach ($categories as $category) : ?> <div class="col-md-4"> <div class="category"> <a href="<?php echo get_category_link($category->term_id); ?>"> <?php echo $category->name; ?> </a> </div> <?php $arg = array( 'post_type' => 'sell-to-us', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'sell_cat', 'field' => 'slug', 'terms' => $category->slug, ), ), ); $data = new WP_Query($arg); if ($data->have_posts()) { echo '<ul>'; while ($data->have_posts()) { $data->the_post(); $category = get_the_category(); $sell_cats = get_the_terms(get_the_ID(), 'sell_cat'); $category_url = get_category_link($category[0]->term_id); $sell_cat_url = get_term_link($sell_cats[0]->term_id, 'sell_cat'); echo '<li><a href="' .$sell_cat_url . basename(get_permalink()) . '">'; the_title(); echo '</a></li>'; } echo '</ul>'; } wp_reset_postdata(); ?> </div> <?php endforeach; ?> </div> <?php return ob_get_clean(); } add_shortcode('sell_to_us', 'sell_to_us_loop');
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter