我在 WordPress 中遇到了一个关于短代码的奇怪问题。当我在添加短代码时保存页面时,它会将我重定向到 wp-admin/post.php
并显示一个白色页面,其中包含 html 格式的短代码结果(参见屏幕截图 1)。幸运的是,所有编辑都被保存了。
有趣的是,我已经用完全相同的方法这样做了十几次。但有一段时间它不再起作用了。
我确实希望您看到这个问题并知道我们可以采取哪些措施来解决它。
我正在使用的 PHP 代码已添加到 functions.php
我使用的短代码是[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' );
最佳答案
添加 ob_start();
即可解决就在第一个<?php
之后标签。
关于php - WordPress 自定义短代码破坏了保存过程(post.php),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51742464/