Content (); prints nothing on my Wordpress template page

I need to display 2 articles on a custom template page on a Wordpress blog, but this code shows nothing.

  $myposts = get_posts("numberposts=2&category=3"); 
foreach($myposts as $post) : the_content(); endforeach;

but if I try print_r($myposts);, I can say that there is an array. How can I solve this problem? many thanks

+3
source share
3 answers

I need to use a loop !

query_posts("numberposts=2&category=3");
while ( have_posts() ): the_post();
    the_content();
endwhile;
+3
source

Write this line:

the_post();

before using:

the_content();
+6
source

You can try: -

    <ul>
     <?php
     global $post;
     $myposts = get_posts('numberposts=5&offset=1&category=1');
     foreach($myposts as $post) :
       setup_postdata($post);
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php the_content() ?>
     <?php endforeach; ?>
     </ul> 

can help http://codex.wordpress.org/Template_Tags/get_posts Thanks.

0
source

Source: https://habr.com/ru/post/1776860/


All Articles