• I’m working off of a local server, so can ‘t provide a link. Using Genesis with NewsPro theme.
    While successfully created a custom post_type, having trouble using a custom loop to display the posts in the sidebar. Am getting “Fatal error: Call to a member function the_post() on a non-object.”
    Here’s my code, which works fine up to the while loop. (I tested the If loop, and it places echoed text in the sidebar with no errors.)
    Have found a lot of posts on similar issues, but not this combo. Greatly appreciate some help. Thanks.

    //* Create a newsbit widget area below other sidebar widgets with a custom loop that displays newsbit post types

    //* Register a widget area called 'newsbit_widget'
    genesis_register_sidebar( array(
     'id' => 'newsbit_widget',
     'name' => __( 'Newsbit widget area' ),
     'description' => __( 'This is where newsbits go' ),
    ) );
    
    //* Hook newsbit_widget area after other sidebar widgets
    function add_newsbit_widget() {
    	$args = array( 'post_type' => 'newsbit', 'posts_per_page' => 10 );
    	$custom_loop = new WP_Query( $args );
    	if ( $custom_loop->have_posts() ) {
    		while( $custom_loop->have_posts() ): $loop->the_post();
    		echo 'this works';
    		endwhile;
    	}
    	wp_reset_postdata();
    }
    add_action( 'genesis_after_sidebar_widget_area', 'add_newsbit_widget' );

Viewing 1 replies (of 1 total)
  • Thread Starter beccaC

    (@beccac)

    Oops, just realized I didn’t include the While loop in the code–which is the part that seems to be creating the problem. Can someone please tell me what’s wrong with my code. Thanks.

    //* Register a widget area called ‘newsbit_widget’
    genesis_register_sidebar( array(
    ‘id’ => ‘newsbit_widget’,
    ‘name’ => __( ‘Newsbit widget area’ ),
    ‘description’ => __( ‘This is where newsbits go’ ),
    ) );

    //* Hook newsbit_widget area after other sidebar widgets
    function add_newsbit_widget() {
    $args = array( ‘post_type’ => ‘newsbit’, ‘posts_per_page’ => 10 );
    $custom_loop = new WP_Query( $args );
    if ( $custom_loop->have_posts() ) {
    while( $custom_loop->have_posts() ): $loop->the_post();
    echo ‘<li class=”widget newsbit_widget”>’;
    echo ‘<h3>Newsbits</h3>’;
    the_title();
    echo ‘<div class=”entry-content”>’;
    the_content();
    echo ‘</div>’;
    endwhile;
    wp_reset_query();
    echo ”;
    echo ”;
    endif;
    }
    add_action(‘thesis_hook_before_sidebar_1′,’widget_featured_posts’);

Viewing 1 replies (of 1 total)

The topic ‘Custom loop to display custom post_type in sidebar’ is closed to new replies.