快捷码导致白屏

时间:2015-07-02 作者:Zamalek Man

我有这个代码来显示区块中的新闻。但何时将此代码包括在functions.php 在短代码中调用它。它显示白色页面,不显示任何错误。

那么我的错误在哪里呢?

出于某种原因,当我编辑functions.php, 各种页面变白。例如,使用此代码:

function box_news_eight( $atts ) {
$code = \'<div class="cf"></div>
<div class="box-home box-news-seven nb-eight">
    <div class="box-inner">
        <div  class="box-wrap">\';
            $query = new WP_Query( array( \'posts_per_page\' => 1, \'cat\' => 4, \'ignore_sticky_posts\' => 1, \'no_found_rows\' => true, \'cache_results\' => false ) );
            if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
                if ( $i_cont == 0 ) { $post_class = \' ws-post-first\'; } else { $post_class = \' ws-post-sec\'; }
                if ( has_post_thumbnail() ) { $has_class =  \'\'; } else { $has_class =  \' no-thumb\'; }
               $code .= \' <div class="post\'.$post_class.$has_class.\'" role="article" itemscope="" itemtype="http://schema.org/Article">\';              
                    $post_sidebars = \'\';
                    if ( $post_sidebars == \'sideNo\' ) {
                        if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) : 
                            $code .= \' <div class="ws-thumbnail"><a href="\'.get_the_permalink().\'" rel="bookmark">
                                    .\'get_the_post_thumbnail( \'bd-normal\' )\'.
                                </a></div>\';
                            endif;
                        } else {
                                if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) :
                            $code .= \'<div class="ws-thumbnail"><a href="\'.get_the_permalink().\'" rel="bookmark">\';
                                        get_the_post_thumbnail( \'bd-large\' );
                               $code .= \'</a></div>\';
                            endif; 
                        }
                    $code .= \'<div class="ws-cap">
                        <div class="post-cats-bd">\';
                            get_the_category( \' \' );
                        $code .= \'</div>
                        <div class="ws-cap-inner">
                            <h3 itemprop="name" class="entry-title"><a itemprop="url" href="\'.get_permalink( $post->ID ).\'" rel="bookmark">\'.get_the_title().\'</a></h3>
                            <div class="post-date-bd">\'.
                                get_time().\'
                            </div>
                        </div>
                    </div>
                </div>\';
                 $i_cont++; endwhile; endif; wp_reset_postdata(); wp_reset_query();
            $query = new WP_Query( array( \'ignore_sticky_posts\' => 1, \'offset\'=>1, \'posts_per_page\' => 4, \'cat\' => 4, \'no_found_rows\' => true, \'cache_results\' => false ) );
            update_post_thumbnail_cache( $query );
            if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
                if ( $i_cont == 0 ) { $post_class = \' ws-post-first\'; } else { $post_class = \' ws-post-sec\'; }
                if ( has_post_thumbnail() ) { $has_class =  \'\'; } else { $has_class =  \' no-thumb\'; }


                if( $count % 3 == 1 ) { \'<div class="row">\'; }
                    $code .= \'<div class="post\'.$post_class.$has_class.\'" role="article" itemscope="" itemtype="http://schema.org/Article">
                        <div class="ws-meta">
                            <h3 itemprop="name" class="entry-title"><a itemprop="url" href="<\'.get_permalink( $post->ID ).\' rel="bookmark">\'.the_title().\'</a></h3>
                        </div>
                    </div>\';
                      if( $count % 3 == 0 ) { "</div>\\n"; }
                      $count++;

                      $i_cont++; endwhile; endif; wp_reset_postdata(); wp_reset_query();
                  if ( $count % 3 != 1 )  "</div>";
        $code .= \'</div>
    </div>
</div>\';

return $code;
}
add_shortcode( \'box_news_eight\', \'box_news_eight\' );

1 个回复
SO网友:Pieter Goosen

正如我在评论中已经指出的,您的代码是一团乱麻,很难阅读和调试。这很可能就是你很难解决问题的原因。

除了一些语法错误外,您的代码中还有一些错误,比如未定义的变量(比如计数器,您应该在使用计数器之前定义计数器),试图输出数组而不是字符串。此外,请停止垃圾,检查是否存在核心功能。这完全是浪费时间。

您应该使用一种语法,而不要使用多个不同的语法。您正在使用以下内容

if( \'something\' ) {
    if( \'something else\' ) :
        // Do something
    endif;
}
不要那样做。由于代码编辑器通常不支持:endif 语法。使用花括号,它们很容易调试,因为所有代码编辑器都支持它们。你应该使用

if( \'something\' ) {
    if( \'something else\' ) {
        // Do something
    }
}
你应该练习缩进。这对于代码的可读性非常重要。不要将大量类似条件语句的代码塞进一行。如果失败了,阅读和调试都很困难

而不是执行以下错误编码的操作

if ( has_post_thumbnail() ) { $has_class =  \'\'; } else { $has_class =  \' no-thumb\'; }
将代码拆分为多行并正确缩进。您的代码应该如下所示

if ( has_post_thumbnail() ) { 
    $has_class =  \'\'; 
} else { 
    $has_class =  \' no-thumb\'; 
}
您可以看到,这更易于阅读和调试

get_the_category() 返回一个数组,您试图将类别的完整数组作为字符串输出。这只会导致Array 正在输出到屏幕

如果要显示类别名称,可以执行以下操作(这将显示第一个类别的类别名称)

$category = get_the_category();
var_dump( $category[0]->name );
如果要将多个参数传递给WP_Query, 不要把它当作一条Loooonnnnggggg线来传递。将其拆分为多行,以便阅读

而不是这个

$query = new WP_Query( array( \'posts_per_page\' => 1, \'cat\' => 4, \'ignore_sticky_posts\' => 1, \'no_found_rows\' => true, \'cache_results\' => false ) );
执行以下易于阅读的操作

$args = array( 
    \'posts_per_page\'      => 1, 
    \'cat\'                 => 4, 
    \'ignore_sticky_posts\' => 1, 
    \'no_found_rows\'       => true, 
    \'cache_results\'       => false 
);
$query = new WP_Query( $args );
我不知道您为什么需要在这里运行2个查询,以及是否真的有必要,但无论如何,我没有触及这一点,因为我不知道您的意图是什么。请将我的代码与您的代码进行比较,看看我的代码与您的代码相比如何。

这是你的代码清理

add_shortcode( \'box_news_eight\', \'box_news_eight\' );
function box_news_eight() 
{
    $code = \'<div class="cf"></div>
    <div class="box-home box-news-seven nb-eight">
        <div class="box-inner">
            <div  class="box-wrap">\';

                $args = array( 
                    \'posts_per_page\'      => 1, 
                    \'cat\'                 => 4, 
                    \'ignore_sticky_posts\' => 1, 
                    \'no_found_rows\'       => true, 
                    \'cache_results\'       => false 
                );
                $query = new WP_Query( $args );

                if ( $query->have_posts() ) { 
                    // Setup your counter
                    $i_cont = 0;

                    while ( $query->have_posts() ) { 
                        $query->the_post();

                        if ( $i_cont == 0 ) { 
                            $post_class = \' ws-post-first\'; 
                        } else { 
                            $post_class = \' ws-post-sec\'; 
                        }

                        if ( has_post_thumbnail() ) { 
                            $has_class =  \'\'; 
                        } else { 
                            $has_class =  \' no-thumb\'; 
                        }
                        $code .= \'<div class="post\'.$post_class.$has_class.\'" role="article" itemscope="" itemtype="http://schema.org/Article">\';

                        $post_sidebars = \'\';
                        if ( $post_sidebars == \'sideNo\' ) {

                            if ( has_post_thumbnail() ) { 
                                $code .= \'<div class="ws-thumbnail">
                                            <a href="\'.get_the_permalink().\'" rel="bookmark">\'
                                                . get_the_post_thumbnail( \'bd-normal\' ) .
                                            \'</a>
                                        </div>\';
                            }

                        } else {

                            if ( has_post_thumbnail() ) {
                                $code .= \'<div class="ws-thumbnail">
                                            <a href="\'.get_the_permalink().\'" rel="bookmark">\'
                                                . get_the_post_thumbnail( \'bd-large\' ) .
                                            \'</a>
                                        </div>\';
                            } 

                        }
                        $category = get_the_category();
                        $code .= \'<div class="ws-cap">
                                    <div class="post-cats-bd">\'
                                        . $category[0]->name .
                                    \'</div>
                                    <div class="ws-cap-inner">
                                        <h3 itemprop="name" class="entry-title"><a itemprop="url" href="\' . get_permalink( $post->ID ) . \'" rel="bookmark">\' . get_the_title() . \'</a></h3>
                                        <div class="post-date-bd">\'
                                            . get_the_time() .
                                        \'</div>
                                    </div>
                                </div>
                            </div>\';

                        $i_cont++; 
                    }
                    wp_reset_postdata();
                }

                $args_2 = array( 
                    \'ignore_sticky_posts\' => 1, 
                    \'offset\'              => 1, 
                    \'posts_per_page\'      => 4, 
                    \'cat\'                 => 4, 
                    \'no_found_rows\'       => true, 
                    \'cache_results\'       => false 
                );
                $query_2 = new WP_Query( $args_2 );
                update_post_thumbnail_cache( $query );

                if ( $query_2->have_posts() ) { 
                    $i_cont_2 = 0;
                    $count = 0;

                    while ( $query_2->have_posts() ) { 

                        $query_2->the_post();
                        if ( $i_cont_2 == 0 ) { 
                            $post_class = \' ws-post-first\'; 
                        } else { 
                            $post_class = \' ws-post-sec\'; 
                        }

                        if ( has_post_thumbnail() ) { 
                            $has_class =  \'\'; 
                        } else { 
                            $has_class =  \' no-thumb\'; 
                        }

                        if( $count % 3 == 1 ) { 
                            $code .= \'<div class="row">\'; 
                        }

                        $code .= \'<div class="post\'.$post_class.$has_class.\'" role="article" itemscope="" itemtype="http://schema.org/Article">
                                    <div class="ws-meta">
                                        <h3 itemprop="name" class="entry-title">
                                            <a itemprop="url" href="<\' . get_permalink( $post->ID ) . \' rel="bookmark">\' . the_title() . \'</a>
                                        </h3>
                                    </div>
                                </div>\';

                        if( $count % 3 == 0 ) { 
                            $code .= "</div>\\n"; 
                        }

                        if ( $count % 3 != 1 ) { 
                            $code .= "</div>";
                        }

                        $count++;
                        $i_cont_2++; 
                    }
                    wp_reset_postdata(); 
                }

            $code .= \'</div>
        </div>
    </div>\';

    return $code;
}

结束

相关推荐

Apply_Filters(‘the_content’,$Content)与DO_ShortCode($Content)

假设我有一个主题选项或自定义的postmeta文本区域。现在我想执行多个短代码、一般文本、图像等。最佳实践是什么?为什么?选项1:$content = //my text area data; echo apply_filters(\'the_content\', $content); 选项2:$content = //my text area data; echo do_shortcode($content); 请告诉我哪一个是最佳实践,以及为什么。EDIT让我详细描