繁体   English   中英

WordPress 自定义短代码中断保存过程 (post.php)

[英]WordPress custom shortcode breaks saving process (post.php)

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

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

我确实希望您看到这个问题并知道我们可以做些什么来解决它。

截图 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();来解决ob_start(); 在第一个<?php标记之后。

我有同样的问题,找到了解决方法,只需添加ob_start(); 在 html 开始和ob_get_clean(); 就这样结束

function form_creation(){
  ob_start(); 
?>
    <form>
    First name: <input type="text" name="firstname"><br>
    Last name:  <input type="text" name="lastname"><br>
    Message:    <textarea name="message"> Enter text here...</textarea>
    </form>
<?php
  return ob_get_clean();
}
add_shortcode('test', 'form_creation');

您无法回显简码HTML。

您需要将HTML绑定到变量中,然后从简码中返回此HTML。

请检查下面的代码

function _login_popup() {

    $html = '<form id="user-login-form" method="post" action="#" class="js-form-redirect js-form-action" novalidate="">
               <div class="floating-label form-group">
                  <input id="useremail" name="user_name" required="" value="" type="email">
                  <label for="useremail">Email</label>
               </div>
               <div class="floating-label form-group">
                  <input id="userpassword" name="password" required="" value="" type="password">
                  <label for="userpassword">Password</label>
               </div>
               <div class="o-form__buttons text-right --small-center">
                  <button type="submit" id="submit_login" class="a-button-form --save a-no-before" value="edit">Sign in</button>
               </div>
      </form>';

return $html;
}
add_shortcode('yg-login-popup', '_login_popup');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM