0

I'm trying to make a shortcode with two parameters $number_posts and $category_name.

The shortcode is calling another function named bg_make_post_grid that uses those parameters as variables.

I added wp_die() to the function to see if it was being called, and it does not kill WordPress so I don't think the function is being called.

What am I doing wrong?

// Add Shortcode
add_shortcode( 'bg_recent_post_grid', 'bg_recent_post_grid_shortcode' );
function bg_recent_post_grid_shortcode( $atts ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            'category_name' => 'uncategorized',
            'number_posts' => '1',
        ), $atts );

    return bg_make_post_grid ( $atts['number_posts'], $atts['category_name'] );

}


function bg_make_post_grid ( $number_posts, $category_name ) {

    $args = array(
        'numberposts' => $number_posts,
        'category' => $category_name,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'post',
    );

    $recent_posts = wp_get_recent_posts( $args );

    wp_die();

}
  • Does bg_make_post_grid() return a value or echo something? – Jacob Peattie Jul 25 '18 at 11:13
  • In the future it will return do more and return something, but for now I just wanted to see if it was being called by adding wp_die() and it turned out it does not. – Ben Turner Jul 25 '18 at 11:18
  • Did you try echo do_shortcode('[bg_recent_post_grid]'); or echo bg_recent_post_grid_shortcode([]); ? And where did you add the code? Is it in functions.php or a plugin you wrote? – Sally CJ Jul 25 '18 at 12:50
  • Code was in a plugin. The problem I had was related to the plugin somehow, I just had to deactivate and reactivate under a different name and it started working. Should I delete the question now? Not sure what is best practice in that situation, since the fix was not code related. – Ben Turner Jul 25 '18 at 13:22
  • You can write an answer (and accept it), but I think it's up to you.. – Sally CJ Jul 25 '18 at 13:30
-1

Please replace the old code with below code it will show you that it's working

add_shortcode( 'bg_recent_post_grid', 'bg_recent_post_grid_shortcode' );
function bg_recent_post_grid_shortcode( $atts ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            'category_name' => 'uncategorized',
            'number_posts' => '1',
        ), $atts );

    return bg_make_post_grid ( $atts['number_posts'], $atts['category_name'] );

}


function bg_make_post_grid ( $number_posts, $category_name ) {

    $args = array(
        'numberposts' => $number_posts,
        'category' => $category_name,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'post',
    );

    $recent_posts = wp_get_recent_posts( $args );
    print_r( $recent_posts); die;

} 
 echo do_shortcode('[bg_recent_post_grid]');
|improve this answer|||||
  • This has the same result. It doesn't do anything. Also calling bg_make_post_grid outside of the shortcode does not prove the shortcode can call the function. P.S. that was not me who down voted you. – Ben Turner Jul 25 '18 at 11:39
  • > Above code is working for me so i told you. – Trilok Jul 25 '18 at 11:44

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.