I have created a custom post type called “faq”. The custom post type uses categories. I have created a shortcode [faq] that displays all the faq posts.
What I would like to do is add an attribute to the shortcode that specifies an faq category. So instead of using the shortcode [faq] it would be something like [faq category=”travel”] and it would only display the posts from the “travel” category.
I am using the following code for the shortcode at the moment. I just need to add the category attribute somehow:
add_shortcode('faq', function() {
$posts = get_posts(array( //Get the FAQ Custom Post Type
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => 'faq',
));
$faq = '<div id="faq-accordion">'; //Open the container
foreach ( $posts as $post ) { // Generate the markup for each Question
$faq .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
$post->post_title,
wpautop($post->post_content)
);
}
$faq .= '</div>'; //Close the container
return $faq; //Return the HTML.
});
Note: Even though the WP_Query parameter is called
category_name
, it expects to be given a slug.I have created a custom post type called “
whiz_qt_quote
“. The custom post type uses categories. I have created a shortcode for all the post[quote]
.What I would like to display one category post on one page. So instead of using the shortcode
[quote]
, it would be something like[quote category="motivation"]
and it would only display the posts from the “motivation” category.I am using the following code for the shortcode at the moment. I just need to add the category attribute somehow: