I'm experiencing a strange issue with shortcodes in WordPress. When I save the page while the shortcode is added, it redirects me to wp-admin/post.php and shows me a white page with the results of the shortcode in html format (see screenshot 1). Luckily, all the editing is saved.
The funny thing is that I've done this a dozen times with the exact same method. But for a short while now it doesn't work anymore.
I do hope you see the issue and know what we can do to fix it.
The PHP code that I'm using is added to the functions.php
The shortcode that I'm using is [showblog cat="planning" number=4]
function showblog_func( $atts ) {
$atts = shortcode_atts([
'number' => '-1',
'cat' => '',
], $atts, 'showblog' );
$numberposts = $atts['number'];
$categorie = $atts['cat'];
//args
$args = array(
'post_type' => 'post',
'posts_per_page' => $numberposts,
'order' => 'ASC',
);
if($categorie != ""){
$args = array(
'post_type' => 'post',
'posts_per_page' => $numberposts,
'category_name' => $categorie,
'order' => 'ASC',
);
}
// The Query
$the_query2 = new WP_Query( $args );
// The Loop
if ( $the_query2->have_posts() ) {
echo '<ul class="blog-list clearfix">';
while ( $the_query2->have_posts() ) {
$the_query2->the_post();
echo '<li class="blog-block">';
echo ' <div class="blog-info">';
echo ' <h4>'.get_the_title().'</h4>';
echo ' <p>'.get_the_excerpt().'</p>';
echo ' <a href="'.get_the_permalink().'" class="blog-button">Read full post</a>';
echo ' </div>';
echo '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
}
add_shortcode( 'showblog', 'showblog_func' );