0

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;
}
  • 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 Jun 8 '17 at 17:36
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

  • 1
    Your code is also wrong as it contains syntax errors, you should take your own advise and turn WP_DEBUG on – ngearing Jun 9 '17 at 0:20
  • Added that missing comma – mrben522 Jan 9 at 20:09

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.