WP查询出错。如果变量为空,则显示先前的POST值

时间:2016-09-17 作者:Ben H

我有一个wp查询,但如果变量为空,那么它似乎会显示以前的帖子内容。例如,如果company name为空,则不应输出任何内容,但它会显示上一篇文章的company name。我真的很不明白为什么会发生这种事。

1模板的代码如下:

else if ( isset( $atts[\'testimonial_template\'] ) && ($atts[\'testimonial_template\'] == \'standard_divi_4_column\') ) { 
        $testimonial_layout_query = null;
        $testimonial_layout_query = new WP_Query( $args );
        $testimonial_layout_output = \'\';
        if ( $testimonial_layout_query->have_posts() ) {
            $i = 1; // start counting the number of testimonials
            $testimonial_layout_output .= \'<div class="et_pb_row et_pb_gutters2 divi-testimonial">\'; // open the first row for the testimonials 
            while ( $testimonial_layout_query->have_posts() ) : $testimonial_layout_query->the_post();
                $thumb = \'\';
                $testimonial_content = get_the_content();
                $testimonial_job_title = get_post_meta( get_the_ID(), \'testimonial_options_job_title\', true );
                $company_name = get_post_meta( get_the_ID(), \'testimonial_options_url_text\', true );
                $company_url = get_post_meta( get_the_ID(), \'testimonial_options_url\', true );
                $testimonial_name = get_the_title();
                if ( has_post_thumbnail() ) {
                    $thumb_id = get_post_thumbnail_id();
                    $thumb_url_array = wp_get_attachment_image_src($thumb_id, \'thumbnail-size\', true);
                    $thumb_url = $thumb_url_array[0];
                    $thumb_id = get_post_thumbnail_id();
                $thumb_url_array = wp_get_attachment_image_src($thumb_id, \'thumbnail-size\', true);
                $thumb_url = $thumb_url_array[0];
                $thumb = $thumb_url;
                }

                //$testimonial_layout_output .= $testimonial_content.\'<br>\'.$testimonial_job_title.\'<br>\'. $company_name.\'<br>\'.$company_url.\'<br>\'.$testimonial_name.\'<br>\'.$thumb.\'<br>\';


            $testimonial_layout_output .= \'<div class="et_pb_column et_pb_column_1_4 divi-testimonial_4"><div class="\' . $module_class . \' et_pb_testimonial \' . $class . \' clearfix" style="\'.$style.\'">\';
                if ( \'\' !== $thumb && \'on\' === $show_thumbnail ) {
                    $testimonial_layout_output .= \'<div class="et_pb_testimonial_portrait" style="background-image: url(\'.$thumb_url .\')"></div>\';
                }
    $testimonial_layout_output .= \'<div class="et_pb_testimonial_description">
        <div class="et_pb_testimonial_description_inner"><p>
            \' . $testimonial_content . \'</p>
            <strong class="et_pb_testimonial_author">\' . $testimonial_name . \'</strong>
            <p class="et_pb_testimonial_meta">\';
            if ( ! empty( $testimonial_job_title ) ) {
            $testimonial_layout_output .= \'\' . $testimonial_job_title . \'\';
            }

            if ( ! empty($company_url) && ! empty($company_name)   ) {
                if ( ! empty( $testimonial_job_title ) ) {
                    $link_output = sprintf( \', <a href="%1$s"%3$s>%2$s</a>\', esc_url( $company_url ),
                                    ( \'\' !== $company_name ? esc_html( $company_name ) : esc_html( $testimonial_name ) ),
                                    ( \'on\' === $url_new_window ? \' target="_blank"\' : \'\' )
                                    );
                } else {
                    $link_output = sprintf( \'<a href="%1$s"%3$s>%2$s</a>\',
                                    esc_url( $company_url ),
                                    ( \'\' !== $company_name ? esc_html( $company_name ) : esc_html( $testimonial_name ) ),
                                    ( \'on\' === $url_new_window ? \' target="_blank"\' : \'\' )
                                    );
                }
                if ( \'\' !== $company_name ) {
                    //$company_name = $link_output;
                } else {
                    //$testimonial_name = $link_output;
                }
            }
            if ( isset($company_name)   ) {
            $testimonial_layout_output .= \'\' . $link_output .\'\';
        }

            $testimonial_layout_output .= \'</p>
        </div> <!-- .et_pb_testimonial_description_inner -->
    </div> <!-- .et_pb_testimonial_description -->
</div> <!-- .et_pb_testimonial -->
</div> <!-- .et_pb_column -->\';



                if($i % 4 == 0) {$testimonial_layout_output .= \'</div><div class="clr"></div><div id="alt row" class="et_pb_row et_pb_row_19 et_pb_gutters2 divi-testimonial">\';}

                    $i++;

            endwhile; // End of the while loop.
            //wp_reset_query();
            $testimonial_layout_output .= \'</div>\';
            $testimonial_layout_output .= \'<div class="clr"></div><style>.et_pb_column .et_pb_row { width:100%; }</style>\';
        //} wp_reset_postdata();
        //return $testimonial_layout_output;

        } /*else {
            if ( et_is_builder_plugin_active() ) {
                include( ET_BUILDER_PLUGIN_DIR . \'includes/no-results.php\' );
            } else {
                get_template_part( \'includes/no-results\', \'index\' );
            }
        }

        $posts = ob_get_contents();

        ob_end_clean();

        $output = $posts;

        return $output;*/
        //ob_flush();
        return $testimonial_layout_output;

    }
如果帖子是空的,有人能告诉我为什么$company\\u name会显示以前帖子的公司名称吗。此外,如果公司url为空,则无法从我看到的内容中获取以前的帖子url。

根据@Pim的答案进行修订。仍然无法正常工作:

if ( isset( $atts[\'testimonial_template\'] ) && ($atts[\'testimonial_template\'] == \'standard_divi_4_column\') ) { 
        $testimonial_layout_query = null;
        $testimonial_layout_query = new WP_Query( $args );
        $testimonial_layout_output = \'\';
        if ( $testimonial_layout_query->have_posts() ) {
            $i = 1; // start counting the number of testimonials
            $testimonial_layout_output .= \'<div class="et_pb_row et_pb_gutters2 divi-testimonial">\'; // open the first row for the testimonials 
            while ( $testimonial_layout_query->have_posts() ) : $testimonial_layout_query->the_post();
                $thumb = \'\';
                $testimonial_content = get_the_content();
                $testimonial_job_title = get_post_meta( get_the_ID(), \'testimonial_options_job_title\', true );
                $company_name = get_post_meta( get_the_ID(), \'testimonial_options_url_text\', true );
                $company_url = get_post_meta( get_the_ID(), \'testimonial_options_url\', true );
                $testimonial_name = get_the_title();
                if ( has_post_thumbnail() ) {
                    $thumb_id = get_post_thumbnail_id();
                    $thumb_url_array = wp_get_attachment_image_src($thumb_id, \'thumbnail-size\', true);
                    $thumb_url = $thumb_url_array[0];
                    $thumb_id = get_post_thumbnail_id();
                $thumb_url_array = wp_get_attachment_image_src($thumb_id, \'thumbnail-size\', true);
                $thumb_url = $thumb_url_array[0];
                $thumb = $thumb_url;
                }

                //$testimonial_layout_output .= $testimonial_content.\'<br>\'.$testimonial_job_title.\'<br>\'. $company_name.\'<br>\'.$company_url.\'<br>\'.$testimonial_name.\'<br>\'.$thumb.\'<br>\';


            $testimonial_layout_output .= \'<div class="et_pb_column et_pb_column_1_4 divi-testimonial_4"><div class="\' . $module_class . \' et_pb_testimonial \' . $class . \' clearfix" style="\'.$style.\'">\';
                if ( \'\' !== $thumb && \'on\' === $show_thumbnail ) {
                    $testimonial_layout_output .= \'<div class="et_pb_testimonial_portrait" style="background-image: url(\'.$thumb_url .\')"></div>\';
                }
    $testimonial_layout_output .= \'<div class="et_pb_testimonial_description">
        <div class="et_pb_testimonial_description_inner"><p>
            \' . $testimonial_content . \'</p>
            <strong class="et_pb_testimonial_author">\' . $testimonial_name . \'</strong>
            <p class="et_pb_testimonial_meta">\';
            if ( ! empty( $testimonial_job_title ) ) {
            $testimonial_layout_output .= \'\' . $testimonial_job_title . \'\';
            }

            if ( ! empty($company_url) && ! empty($company_name)   ) {
                if ( ! empty( $testimonial_job_title ) ) {
                    $link_output = sprintf( \', <a href="%1$s"%3$s>%2$s</a>\', esc_url( $company_url ),
                                    ( \'\' !== $company_name ? esc_html( $company_name ) : esc_html( $testimonial_name ) ),
                                    ( \'on\' === $url_new_window ? \' target="_blank"\' : \'\' )
                                    );
                } else {
                    $link_output = sprintf( \'<a href="%1$s"%3$s>%2$s</a>\',
                                    esc_url( $company_url ),
                                    ( \'\' !== $company_name ? esc_html( $company_name ) : esc_html( $testimonial_name ) ),
                                    ( \'on\' === $url_new_window ? \' target="_blank"\' : \'\' )
                                    );
                }
                if ( \'\' !== $company_name ) {
                    //$company_name = $link_output;
                } else {
                    //$testimonial_name = $link_output;
                }
            }
            if ( isset($company_name)   ) {
            $testimonial_layout_output .= \'\' . $link_output .\'\';
        }

            $testimonial_layout_output .= \'</p>
        </div> <!-- .et_pb_testimonial_description_inner -->
    </div> <!-- .et_pb_testimonial_description -->
</div> <!-- .et_pb_testimonial -->
</div> <!-- .et_pb_column -->\';



                if($i % 4 == 0) {$testimonial_layout_output .= \'</div><div class="clr"></div><div id="alt row" class="et_pb_row et_pb_row_19 et_pb_gutters2 divi-testimonial">\';}

                    $i++;
                $thumb = \'\';
                $testimonial_content = \'\';
                $testimonial_job_title = \'\';
                $company_name = \'\';
                $company_url = \'\';
                $testimonial_name = \'\';
            endwhile; // End of the while loop.
            //wp_reset_query();
            $testimonial_layout_output .= \'</div>\';
            $testimonial_layout_output .= \'<div class="clr"></div><style>.et_pb_column .et_pb_row { width:100%; }</style>\';
        //} wp_reset_postdata();
        //return $testimonial_layout_output;

        } /*else {
            if ( et_is_builder_plugin_active() ) {
                include( ET_BUILDER_PLUGIN_DIR . \'includes/no-results.php\' );
            } else {
                get_template_part( \'includes/no-results\', \'index\' );
            }
        }

        $posts = ob_get_contents();

        ob_end_clean();

        $output = $posts;

        return $output;*/
        //ob_flush();
        return $testimonial_layout_output;

    }

1 个回复
SO网友:Pim

通过插入如下语句,在循环结束之前重置变量$company_name=\'\';. 由于如果post\\u meta为空,则无法检查以确保变量变为空,因此它将保留变量中存储的前一个变量。

相关推荐

WordPress Custom Post Loop

我正在尝试循环浏览自定义WordPress帖子,遇到了一个问题,比如我添加了自定义字段并想在中显示它<li> 使用循环。我成功地完成了操作,但数据/链接/类别正在重复,如果类别与以下内容相同,我希望只显示一次:如果我有2篇带有data1类别的帖子,那么链接将只显示data1once 但我有2个不同类别的帖子,然后它会分别显示每个帖子。Sample Code:<ul class="filter filter-top"> <li cla