简体   繁体   中英

Wordpress posts displaying the same content

I have a simple custom page template with a simple loop that displays links to posts of the same category ID=11. However, the problem is that although the links are working correctly, all posts are displaying the same content (the content of the first post). I can't work out why this is. Any help would be really appreciated, thanks.

Here is the loop on the custom page template

  <?php 
          $args = array('cat' => 11);
          $category_posts = new WP_Query($args);

          if($category_posts->have_posts()) : 
          while($category_posts->have_posts()) : 
          $category_posts->the_post();
        ?>

        <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>

        <?php
          endwhile;
          else: 
          // no posts.
          endif;
        ?>

And here is what I have on single.php

      <h1><?php the_title(); ?></h1>
      <?php the_content(); ?>

You should call the_post() in single.php before doing anything else.

Try this:

<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>

Leave your other code alone. It looks like it should be working as expected.

Worked it out through some trial and error. I had a list of post titles in the sidebar and needed to use wp_reset_query.

In single.php use the following code to get the content and title.

while ( have_posts() ) : the_post();

    the_title();    // For post title
    the_content();  //For post content

endwhile;

use this in your custom page, i used wp_reset_postdata();

<?php 
      $args = array('cat' => 11);
      $category_posts = new WP_Query($args);

      if($category_posts->have_posts()) : 
      while($category_posts->have_posts()) : 
      $category_posts->the_post();
    ?>

    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

    <?php
      endwhile;
     wp_reset_postdata();
      else: 
      // no posts.
      endif;
    ?>

And on single.php use this

<?php

while ( have_posts() ) : the_post();

the_title();    // For post title
the_content();  //For post content

endwhile; ?>

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