简体   繁体   中英

Wordpress showing page name instead of blog posts

I'm currently working on my first Wordpress theme and I've a problem:

I want to have my last 3 posts on every site. Everything works fine on my home page, but when I go to another page it just shows the page name and the "Read more..." tag after it.

The code I use is:

<?php while(have_posts()) : the_post(); ?>
  <div class="article-preview">
    <p>» <?php the_time('l, j. F Y')?></p>
    <b><?php the_title(); ?></b>
    <?php the_excerpt(); ?><a href="<?php echo get_permalink(); ?>" style="color:white"> Mehr...</a>
    <hr style="margin-top:5px" />

  </div>
<?php endwhile; ?>

Does anyone know how to fix this issue? Thanks in advance!

For displaying posts on other pages, you have to display custom query before while loop.

Here is the updated code of your code:

<?php $the_query = new WP_Query( 'post_type'=>'post', 'posts_per_page=3' ); ?>

// Start our WP Query
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
  <div class="article-preview">
    <p>» <?php the_time('l, j. F Y')?></p>
    <b><?php the_title(); ?></b>
    <?php the_excerpt(); ?><a href="<?php echo get_permalink(); ?>" style="color:white"> Mehr...</a>
    <hr style="margin-top:5px" />

  </div>
<?php endwhile; 
wp_reset_postdata();
?>

May be this would be helpful for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM