自定义Post类型循环在小部件中使用时引发500错误

时间:2017-07-03 作者:J Robz

我试图在一个小部件中添加一个自定义post类型的自定义循环。

在我将循环添加到our_team_section_after() 作用

但是,我找不到任何语法错误,每当我添加循环时,就会出现500个内部服务器错误。当我把它拿出来时,它工作得很好。

我使用的是Genesis框架,所以有些钩子是Genesis独有的。

我真的一直在网上搜索答案,但没有找到答案。任何帮助都将不胜感激!

<?php
//Register the widget areas
genesis_register_sidebar( array(
\'id\' => \'landing-section\',
\'name\' => __( \'Landing Section\', \'genesis\' ),
\'description\' => __( \'First Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'who-we-are-section\',
\'name\' => __( \'Who We Are Section\', \'genesis\' ),
\'description\' => __( \'Who We Are Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'creative-process-section\',
\'name\' => __( \'Creative Process Section\', \'genesis\' ),
\'description\' => __( \'Creative Process Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'services-section\',
\'name\' => __( \'Services Section\', \'genesis\' ),
\'description\' => __( \'Services Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'our-team-section\',
\'name\' => __( \'Our Team Section\', \'genesis\' ),
\'description\' => __( \'Our Team Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'case-studies-section\',
\'name\' => __( \'Case Studies Section\', \'genesis\' ),
\'description\' => __( \'Case Studies Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'worked-with-section\',
\'name\' => __( \'Who We\\\'ve Worked With\', \'genesis\' ),
\'description\' => __( \'Who We\\\'ve Worked With Homepage Section.\', \'Bootstrap Genesis\' ),
) );


//* Add the page widget in the content - XHTML
add_action( \'genesis_before_loop\', \'front_page_content\' );
function front_page_content() {
if ( is_front_page() ){
    genesis_widget_area (\'landing-section\', array(
        \'before\' => \'<div id="landing-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
    genesis_widget_area (\'who-we-are-section\', array(
        \'before\' => \'<div id="who-we-are-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
    genesis_widget_area (\'creative-process-section\', array(
        \'before\' => \'<div id="creative-process-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
    genesis_widget_area (\'services-section\', array(
        \'before\' => \'<div id="services-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
    genesis_widget_area (\'our-team-section\', array(
        \'before\' => our_team_section_before(),
        \'after\' => our_team_section_after(),
    ) );
    genesis_widget_area (\'case-studies-section\', array(
        \'before\' => \'<div id="case-studies-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
    genesis_widget_area (\'worked-with-section\', array(
        \'before\' => \'<div id="worked-with-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
}
}

function our_team_section_before() {
$before = \'<div id="our-team-section">\';
$before .= \'<div class="container">\';

return $before;
}

function our_team_section_after() {
$after = \'</div>\';
    $after .= \'<div class="container-fluid">\';
        $after .= \'<div class="row">\';
            $after .= \'<div class="col-lg-9 col-sm-8 col-xs-12">\';
                $after .= \'<div class="owl-carousel">\';

                    $args = array(
                        \'post_type\' => \'team_post_type\'
                    );
                    $query = new WP_Query($args);

                    if( $query -> have_posts() ){
                        $query -> while( have_posts() ){
                            $query -> the_post();
                            $after .= \'<div>\';
                            $after .= the_post_thumbnail();
                            $after .= \'</div>\';
                        }
                        wp_reset_postdata();
                    }

                $after .= \'</div>\';
            $after .= \'</div>\';
            $after .= \'<div class="col-lg-3 col-sm-4 col-xs-12">\';
                $after .= \'<div class="homepage-cta">\';
                    $after .= \'<span>See Entire Team &gt;</span>\';
                $after .= \'</div>\';
            $after .= \'</div>\';
        $after .= \'</div>\';
    $after .= \'</div>\';
$after .= \'</div>\';

return $after;
}

1 个回复
最合适的回答,由SO网友:Johansson 整理而成

您提供的代码中有几个问题。

$query -> while( have_posts() )

TheWP_Query() 返回类型为object。您所引用的方法不存在。相反,您应该使用以下选项:

while( $query->have_posts() ) {...}

wp_reset_postdata(); 在conditional

中,这个函数会按照它的建议重置帖子的数据。如果在条件中使用它,而条件未运行,则不会重置帖子的数据,从而使空查询处于活动状态。因此,您应该在条件之外或之前使用它return, 如果要返回数据。

the_... 而不是get_the_...

如果要将数据存储在变量中,应使用返回数据的函数,而不是回显数据。the_post_thumbanil() 当您需要返回缩略图时,会回显缩略图。所以,你应该使用get_the_post_thumbnail() 相反这适用于以the_..., 据我所知。

结束
自定义Post类型循环在小部件中使用时引发500错误 - 小码农CODE - 行之有效找到问题解决它

自定义Post类型循环在小部件中使用时引发500错误

时间:2017-07-03 作者:J Robz

我试图在一个小部件中添加一个自定义post类型的自定义循环。

在我将循环添加到our_team_section_after() 作用

但是,我找不到任何语法错误,每当我添加循环时,就会出现500个内部服务器错误。当我把它拿出来时,它工作得很好。

我使用的是Genesis框架,所以有些钩子是Genesis独有的。

我真的一直在网上搜索答案,但没有找到答案。任何帮助都将不胜感激!

<?php
//Register the widget areas
genesis_register_sidebar( array(
\'id\' => \'landing-section\',
\'name\' => __( \'Landing Section\', \'genesis\' ),
\'description\' => __( \'First Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'who-we-are-section\',
\'name\' => __( \'Who We Are Section\', \'genesis\' ),
\'description\' => __( \'Who We Are Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'creative-process-section\',
\'name\' => __( \'Creative Process Section\', \'genesis\' ),
\'description\' => __( \'Creative Process Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'services-section\',
\'name\' => __( \'Services Section\', \'genesis\' ),
\'description\' => __( \'Services Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'our-team-section\',
\'name\' => __( \'Our Team Section\', \'genesis\' ),
\'description\' => __( \'Our Team Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'case-studies-section\',
\'name\' => __( \'Case Studies Section\', \'genesis\' ),
\'description\' => __( \'Case Studies Homepage Section.\', \'Bootstrap Genesis\' ),
) );
genesis_register_sidebar( array(
\'id\' => \'worked-with-section\',
\'name\' => __( \'Who We\\\'ve Worked With\', \'genesis\' ),
\'description\' => __( \'Who We\\\'ve Worked With Homepage Section.\', \'Bootstrap Genesis\' ),
) );


//* Add the page widget in the content - XHTML
add_action( \'genesis_before_loop\', \'front_page_content\' );
function front_page_content() {
if ( is_front_page() ){
    genesis_widget_area (\'landing-section\', array(
        \'before\' => \'<div id="landing-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
    genesis_widget_area (\'who-we-are-section\', array(
        \'before\' => \'<div id="who-we-are-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
    genesis_widget_area (\'creative-process-section\', array(
        \'before\' => \'<div id="creative-process-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
    genesis_widget_area (\'services-section\', array(
        \'before\' => \'<div id="services-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
    genesis_widget_area (\'our-team-section\', array(
        \'before\' => our_team_section_before(),
        \'after\' => our_team_section_after(),
    ) );
    genesis_widget_area (\'case-studies-section\', array(
        \'before\' => \'<div id="case-studies-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
    genesis_widget_area (\'worked-with-section\', array(
        \'before\' => \'<div id="worked-with-section"><div class="container">\',
        \'after\' => \'</div></div>\',
    ) );
}
}

function our_team_section_before() {
$before = \'<div id="our-team-section">\';
$before .= \'<div class="container">\';

return $before;
}

function our_team_section_after() {
$after = \'</div>\';
    $after .= \'<div class="container-fluid">\';
        $after .= \'<div class="row">\';
            $after .= \'<div class="col-lg-9 col-sm-8 col-xs-12">\';
                $after .= \'<div class="owl-carousel">\';

                    $args = array(
                        \'post_type\' => \'team_post_type\'
                    );
                    $query = new WP_Query($args);

                    if( $query -> have_posts() ){
                        $query -> while( have_posts() ){
                            $query -> the_post();
                            $after .= \'<div>\';
                            $after .= the_post_thumbnail();
                            $after .= \'</div>\';
                        }
                        wp_reset_postdata();
                    }

                $after .= \'</div>\';
            $after .= \'</div>\';
            $after .= \'<div class="col-lg-3 col-sm-4 col-xs-12">\';
                $after .= \'<div class="homepage-cta">\';
                    $after .= \'<span>See Entire Team &gt;</span>\';
                $after .= \'</div>\';
            $after .= \'</div>\';
        $after .= \'</div>\';
    $after .= \'</div>\';
$after .= \'</div>\';

return $after;
}

1 个回复
最合适的回答,由SO网友:Johansson 整理而成

您提供的代码中有几个问题。

$query -> while( have_posts() )

TheWP_Query() 返回类型为object。您所引用的方法不存在。相反,您应该使用以下选项:

while( $query->have_posts() ) {...}

wp_reset_postdata(); 在conditional

中,这个函数会按照它的建议重置帖子的数据。如果在条件中使用它,而条件未运行,则不会重置帖子的数据,从而使空查询处于活动状态。因此,您应该在条件之外或之前使用它return, 如果要返回数据。

the_... 而不是get_the_...

如果要将数据存储在变量中,应使用返回数据的函数,而不是回显数据。the_post_thumbanil() 当您需要返回缩略图时,会回显缩略图。所以,你应该使用get_the_post_thumbnail() 相反这适用于以the_..., 据我所知。

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请