482 posts
  • Exclusive Author
  • Author Level 3
  • Meetup Participant
  • United Kingdom
+3 more
FlatKing
says

Hey everyone – Fingers crossed someone can help me here

I’m using a WP query to bring a loop for a custom post type within a shortcode.

I’ve got it bundled at the end into return $output;

$args = array( 'post_type' => 'news');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
        $output .= get_the_title();
        $output .= '<div class="entry-content">';
        $output .= the_excerpt();
        $output .= '</div>';
endwhile;

This is what’s being displayed http://prntscr.com/2qcn4a

If I change the_excerpt to get_the it just goes blank and doesn’t even echo the excerpt to a random place – http://prntscr.com/2qcnlc

Would love any help!!!

Quick Edit
Tried wrapping in tags
Tried global $post;
Tried get_the_excerpt($post->ID)
221 posts
  • 4 Years of Membership
  • United Kingdom
  • Author Level 3
  • Collector Level 2
+2 more
NuclearThemes
says

Get the excerpt doesn’t accept any parameters (frustratingly). You need to use wp_trim_words and get_the_content to get the excerpt by ID.

67 posts
  • 1 Year of Membership
  • Exclusive Author
DesignLinks
says

If the issue is that your loop content is being echoed on top of all page contents then wrap your loop code like this …

    ob_start(); 

    get_template_part( 'blog', 'index' );  // your loop here

    $return = ob_get_contents();
    ob_end_clean();
    return $return;

Hope it helps :)

482 posts
  • Exclusive Author
  • Author Level 3
  • Meetup Participant
  • United Kingdom
+3 more
FlatKing
says

If the issue is that your loop content is being echoed on top of all page contents then wrap your loop code like this …
    ob_start(); 

    get_template_part( 'blog', 'index' );  // your loop here

    $return = ob_get_contents();
    ob_end_clean();
    return $return;

Hope it helps :)

Is there a need to do the actual get template? or can I still just have the loop in there?

482 posts
  • Exclusive Author
  • Author Level 3
  • Meetup Participant
  • United Kingdom
+3 more
FlatKing
says

If the issue is that your loop content is being echoed on top of all page contents then wrap your loop code like this …
    ob_start(); 

    get_template_part( 'blog', 'index' );  // your loop here

    $return = ob_get_contents();
    ob_end_clean();
    return $return;

Hope it helps :)

Got it working – you beauty! I could kiss you!

by
by
by
by
by
by