• Hello,

    I have created a custom post type that can be added to a page/post using a shortcode.

    I am having difficulty in adding an attribute where the user can enter which category the posts should be displayed from.

    My current shortcode code:

    function my_sc($atts) {
    
    	ob_start();
    
    	$sc_atts = shortcode_atts( array(
    		'posts_per_page' => -1,
    		'order' => 'ASC',
    		'orderby' => 'date',
    		'post_type' => 'rye_cpt'
        ), $atts ); 
    
        $args = array(
          'posts_per_page' => $sc_atts['posts_per_page'],
          'order' => $sc_atts['order'],
          'orderby' => $sc_atts['orderby'],
          'post_type' => 'rye_cpt'
        );
    
     return ob_get_clean();
    
     }
    add_shortcode('shortcode','my_sc');

    All the other attributes such as posts_per_page etc I’m just having difficulty for a category to be entered within the shortcode.

    Any help much appreciated.

Viewing 1 replies (of 1 total)
  • You are looking to use these attributes as part of your wp_query:

    Category Parameters
    Show posts associated with certain categories.
    
    cat (int) - use category id.
    category_name (string) - use category slug.
    category__and (array) - use category id.
    category__in (array) - use category id.
    category__not_in (array) - use category id.

    Most likely you will use the name or the id like this..

    $args = array(
          'posts_per_page' => $sc_atts['posts_per_page'],
          'order' => $sc_atts['order'],
          'orderby' => $sc_atts['orderby'],
          'category_name' => 'staff' ,
          'post_type' => 'rye_cpt'
        );

    You may want to review the wp_query object:

    https://codex.www.ads-software.com/Class_Reference/WP_Query

Viewing 1 replies (of 1 total)
  • The topic ‘Add custom post type taxonomy to shortcode atts’ is closed to new replies.