Is it possible to use 2 shortcodes with the same function and change only the post type in the function depending on what shortcode it is?
For instance, when I use all_faq
shortcode the post type should be Faq. When I use wordpress_faq
then the post type should be wp-Faq
. It works now but is it possible to make the code shorter in one function? Like if all_faq
is used change post type to faq
and else if wordpress faq
is used change post type to wp-faq
.
//shortcode NORMAL FAQ
add_shortcode( 'all_faq', 'display_custom_post_type' );
function display_custom_post_type(){
$args = array(
'post_type' => 'Faq',
'post_status' => 'publish',
'posts_per_page' => '-1'
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ){
$string .= '<div class="container-fluid">';
$string .= '<div class="grid-container">';
while( $query->have_posts() ){
$query->the_post();
$string .= '<div class="sub-grid">';
$string .= '<div class="faq-shortcode-title">' . get_the_title() . '</div>';
$string .= '<div class="faq-shortcode-content">' . get_the_content() . '</div>';
$string .= '</div>';
}
$string .= '</div>';
$string .= '</div>';
}
wp_reset_postdata();
return $string;
}
//shortcode WORDPRESS FAQ
add_shortcode( 'wordpress_faq', 'display_wordpress_faq' );
function display_wordpress_faq(){
$args = array(
'post_type' => 'wp-Faq',
'post_status' => 'publish',
'posts_per_page' => '-1'
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ){
$string .= '<div class="container-fluid">';
$string .= '<div class="grid-container">';
while( $query->have_posts() ){
$query->the_post();
$string .= '<div class="sub-grid">';
$string .= '<div class="faq-shortcode-title">' . get_the_title() . '</div>';
$string .= '<div class="faq-shortcode-content">' . get_the_content() . '</div>';
$string .= '</div>';
}
$string .= '</div>';
$string .= '</div>';
}
wp_reset_postdata();
return $string;
}