1

I'm trying to add a custom post type custom loop in a widget.

Everything here worked correctly until I added the loop to the our_team_section_after() function.

I can't find any syntax errors, however, Whenever I add the loop I get a 500 internal server error. When I take it back out, it works fine.

I'm using the Genesis Framework, so some of the hooks are genesis exclusive.

I've really been searching the web for an answer and I've returned no luck. Any help would be appreciated!

<?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;
}
6
  • Hi. Please elaborate your problem. I don't see the question here.
    – Johansson
    Jul 3, 2017 at 15:57
  • @JackJohansson Sorry about that! Better?
    – J Robz
    Jul 3, 2017 at 16:00
  • 1
    You can't use the_post_thumbnail in assignment (or any WP functions that start with the_), you need to use get_the_post_thumbnail.
    – Milo
    Jul 3, 2017 at 16:00
  • 1
    Yes that is. I am unfamiliar with the $query -> while( have_posts() ). Did you mean while($query->have_posts());? Also you should move your wp_reset_postdata() out of the conditional. the_post_thumbnail() will echo the content, while you need to return it. So as Milo mentioned, you should use get_the... instead.
    – Johansson
    Jul 3, 2017 at 16:00
  • 1
    I made both those edits, and I got it working. I'm pretty sure the 500 error was from @JackJohansson 's edit that I missed. Thank you!
    – J Robz
    Jul 3, 2017 at 16:07

1 Answer 1

1

There are several problems in your provided code.

$query -> while( have_posts() )

The WP_Query() return type is object. You are referring to a method that doesn't exist. Instead, you should use the following:

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

wp_reset_postdata(); inside the conditional

This function resets the post's data, as it suggests. If you use it inside a conditional, and the conditional doesn't run, your post's data won't be reset, leaving an empty query active. So, you should use it outside the conditional, or before return, if you are returning the data instead.

the_... instead of get_the_...

If you want to store the data in a variable, you should use a function that returns the data, not echo it. the_post_thumbanil() echoes the thumbnail, while you need to return it. So, you should use get_the_post_thumbnail() instead. This applies to every WordPress function that begins with the_..., as far as i know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.