php - WordPress 自定义短代码破坏了保存过程(post.php)

标签 php wordpress shortcode

我在 WordPress 中遇到了一个关于短代码的奇怪问题。当我在添加短代码时保存页面时,它会将我重定向到 wp-admin/post.php 并显示一个白色页面,其中包含 html 格式的短代码结果(参见屏幕截图 1)。幸运的是,所有编辑都被保存了。

有趣的是,我已经用完全相同的方法这样做了十几次。但有一段时间它不再起作用了。

我确实希望您看到这个问题并知道我们可以采取哪些措施来解决它。

Screenshot 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/

相关文章:

php - file_get_contents 不输出 svg

wordpress - 使用 .htaccess 将 HTTP 重定向到 HTTPs

php - Wordpress/PHP - 做一个简码

css - 是否可以使用 css 编辑在 wordpress 中使用简码创建的按钮?

php - 想要匹配php中的模式

表单提交时调用 Javascript 函数

php - 在没有asc和desc的mysql中排序

javascript - 从 HTML 上传(不是读取).xlsx 文件并将其(以任何方式)发送到 PHP 文件

javascript - WordPress 博客介绍

wordpress - 可以将YouTube标题添加到Wordpress [embed] [/embed]吗?