• Resolved djones8520

    (@djones8520)


    I originally used $key->{“post_content”} in the code below, but that didn’t take any of the formatting into account. From what I’ve read the_content() should work with get_posts(), but it isn’t working.

    What happens is it appears to get stuck in the loop, because the title of the first post is the only thing that displays on the page and it is repeated multiple times. I didn’t count it, but I would guess 100+ times.

    // $att sets up the parameter to pull the posts.  The array(...) is used if there is no parameter in the shortcode
    	$att = shortcode_atts ( array('category_name' => 'none', 'orderby' => 'post_date'), $args);
    	// Pulls the posts
    	$posts_array = get_posts($att);
    	//print_r(array_values($posts_array));
    
    	// $posts_array is a array of WP_Post objects.  To use a element use $key->{"name"}
    	// The div has to be created her, because if you put the shortcode in a div the html content doesn't get put into the div
    	echo '<div id="post_allnews">';
    	foreach ($posts_array as $key){
    		echo '<div class="post_allnews">';
    		// guid is the address to the post.  post_title is the title
    		echo '<h1><a href="'.$key->{"guid"}.'">'.$key->{"post_title"}.'</a></h1>';
    		// post content
    		echo '<p>';
    		the_content();
    		echo '</p>';
    		echo "</div>";
    	}
    	echo "</div>";
    
    	// This line was recommended when using get_posts()
    	wp_reset_postdata();

    I’m not sure what I’m doing wrong.

    Thanks for any suggestions.

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

    (@djones8520)

    I figured it out. To use the_content with get_posts, you need the code below. I put it as the first line inside the foreach loop.

    setup_postdata($key);

Viewing 1 replies (of 1 total)
  • The topic ‘get_posts the_content() problem’ is closed to new replies.