• Luke

    (@lukejongibson)


    Hi,

    I’ve got the following code and it’s only displaying the latest post, any idea why it’s only showing one post? It used to work but something changed and now it’s broken. I’m thinking maybe an update to the latest version of WordPress and now it’s using some depreciated code.

    Cheers

    <?php get_header(); ?>
    
    	<div id="primary" class="content-area">
    		<main id="main" class="site-main row" role="main">
    		<?php
      $temp = $wp_query;
      $wp_query = null;
      $wp_query = new WP_Query();
      $wp_query->query('showposts=6&post_type=post'.'&paged='.$paged); 
    
      while ($wp_query->have_posts()) : $wp_query->the_post();
    ?>
    				<article id="post-<?php the_ID(); ?>" <?php post_class('col-sm-6'); ?>>
    
    	<div class="entry-content">
    <?php
    
      echo '<div class="ashi-content">';
      ?>
     <a href="<?php the_permalink(); ?>">
       <?php echo '<div class="ashi-imagehead">';
    
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    	the_post_thumbnail( 'large' );
    }
      echo '</div>';
    
       echo '<h2>';
    
        the_title();
          echo '</h2>';
    echo '</a>';
    
          echo '<p class="ashi-date">';
     the_time('F j, Y');
          echo '</p>';
          echo '<div class="ashi-excerpt">';
    
    the_excerpt();
          echo '</div>';
              echo '<div class="ashi tags">';
      the_tags();
      echo '</div>';
    
      echo '</div>';
    
    	?>
    </article><!-- #post-## -->
    
    			<?php endwhile; // end of the loop.
    			?>
    		<div class="paged-nav">
        <div class="prev"><?php previous_posts_link('&laquo; Back') ?></div>
        <div class="next"><?php next_posts_link('Next &raquo;') ?></div>
    </div>
    
    <?php
      $wp_query = null;
      $wp_query = $temp;  // Reset
    ?>
    </div>
    		</main> <!-- #main -->
    	</div> <!-- #primary -->
    
    <?php get_footer(); ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Phil

    (@websterwebguru)

    Hello,

    I know these things can be frustrating. It looks like you have some old code here. showposts was deprecated. You now need to use posts_per_page. I would also check out https://codex.ww.wp.xz.cn/Class_Reference/WP_Query#Description for the new usage of WP_Query

    Thread Starter Luke

    (@lukejongibson)

    I thought that might be the case, thank you.

    Thread Starter Luke

    (@lukejongibson)

    Okay so I updated the code, but still have the problem.

    I’m trying to get the posts to display on a standard page. Not the main posts page selected in the setting menu.

    <?php
    /*
    Template Name: News Decoded
    */
    ?>
    <?php get_header(); ?>
    <div id="primary" class="content-area">
    <?php query_posts('cat=1,2,3,4,5,6,7,8,9,10&posts_per_page=6'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class('col-sm-6'); ?>>
    <div class="entry-content">
    
    <div class="ashi-content">
     <a href="<?php the_permalink(); ?>"><div class="ashi-imagehead">
    
    <?php the_post_thumbnail( 'large' );  ?>
    </div>
    
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <p class="ashi-date">
     <?php the_time('F j, Y'); ?>
        </p>
        <div class="ashi-excerpt"><?php the_excerpt(); ?></div>
         <div class="ashi tags"><?php the_tags(); ?></div>
    </div>
    </article><!-- #post-## -->
    
    <?php endwhile; else: ?>
    
    	<p>Sorry, no posts to list</p>
    
    <?php endif; ?>
    
    <div class="paged-nav">
        <div class="prev"><?php previous_posts_link('&laquo; Back') ?></div>
        <div class="next"><?php next_posts_link('Next &raquo;') ?></div>
    </div>
    
    </div>
    	</div> <!-- #primary -->
    <?php get_footer(); ?>

    Any ideas what I’m still doing wrong?

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Loop only displaying one post.’ is closed to new replies.