Custom shortcode is causing a WSOD

admin2025-06-04  53

I have written a custom shortcode (see below) and placed it in the functions.php file of my child theme.

The aim of the shortcode is to retrieve the title of a previously created custom post. In this case the custom post type I created is titled 'publications_test' from the ID of the post.
For example, [testshorctode id=427] would output the title of the post whose ID is 427. Unfortunately, the code is producing a WSOD and I'm not sure where I am going wrong.

Any help would be greatly appreciated. I am by no means a web developer, so I apologize for any obvious error.

function register_shortcodes() {
add_shortcode( 'testshortcode', 'testshortcode_function' );
}

add_action( 'init', 'register_shortcodes' );

function testshortcode_function($atts) {
    extract( shortcode_atts(
        'post_type' => 'publications_test',
        'post_status' => 'publish'
        'id' => '',
    );

            global $post;

    $string = '';
    $query = new WP_Query( $atts );
    if( $query->have_posts() ){
        $string .= '<ul>';
        while( $query->have_posts() ){
            $query->the_post();
            $string .= '<li>' . get_the_title() . '</li>';
        }
        $string .= '</ul>';
    }
    wp_reset_postdata();
    return $string;
}

I have written a custom shortcode (see below) and placed it in the functions.php file of my child theme.

The aim of the shortcode is to retrieve the title of a previously created custom post. In this case the custom post type I created is titled 'publications_test' from the ID of the post.
For example, [testshorctode id=427] would output the title of the post whose ID is 427. Unfortunately, the code is producing a WSOD and I'm not sure where I am going wrong.

Any help would be greatly appreciated. I am by no means a web developer, so I apologize for any obvious error.

function register_shortcodes() {
add_shortcode( 'testshortcode', 'testshortcode_function' );
}

add_action( 'init', 'register_shortcodes' );

function testshortcode_function($atts) {
    extract( shortcode_atts(
        'post_type' => 'publications_test',
        'post_status' => 'publish'
        'id' => '',
    );

            global $post;

    $string = '';
    $query = new WP_Query( $atts );
    if( $query->have_posts() ){
        $string .= '<ul>';
        while( $query->have_posts() ){
            $query->the_post();
            $string .= '<li>' . get_the_title() . '</li>';
        }
        $string .= '</ul>';
    }
    wp_reset_postdata();
    return $string;
}
Share Improve this question edited Jun 8, 2017 at 19:44 avpaderno 1116 bronze badges asked Jun 8, 2017 at 16:09 Chris GChris G 1 1
  • in wp_config.php set define( 'WP_DEBUG', true ); to help figure out white screen problems. that's a good place to start with debugging wordpress php problems – mrben522 Commented Jun 8, 2017 at 17:36
Add a comment  | 

1 Answer 1

Reset to default 2
 extract( shortcode_atts(
        'post_type' => 'publications_test',
        'post_status' => 'publish'
        'id' => '',
    );

The above is wrong. You have an array inside that function but it's not in an actual array. Try this instead:

 extract( shortcode_atts(array(
        'post_type' => 'publications_test',
        'post_status' => 'publish',
        'id' => '',
    ), $atts, 'testshortcode');

shortcode atts

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749027796a315745.html

最新回复(0)