0

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;
}
1

the callback function has 3 parameters and the 3rd is the tag name :

add_shortcode( 'all_faq', 'commonFunction' );
add_shortcode( 'wordpress_faq', 'commonFunction' );


function commonFunction($attr, $content, $tag) {

    $cores = [
        "all_faq" => "Faq",
        "wordpress_faq" => "wp-Faq",
    ];


    $args = array(
        'post_type' => $cores[$tag],
        'post_status' => 'publish',
        'posts_per_page' => '-1'
    );


    // ...

}
  • MUCH better solution than mine. Thanks for sharing. – Mat_ Mar 17 '16 at 11:26
0

Maybe you could reverse the problem. What you need is simply add an argument to your shortcode and this way, you can have only one function and one shortcode. You could have something like that at the beginning of you function :

$atts = shortcode_atts( array(
    'posttype' => 'Faq'
), $atts, 'all_faq' );

$args = array(

    'post_type' => $atts['posttype'],
    'post_status' => 'publish',
    'posts_per_page' => '-1'
);

Then, you can use your shortcode with a new parameter : [all_faq posttype="Faq"]

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.