0

I am new here, I do not have a ton of experience with PHP but if I have a correct block I can work the different functions. I have a static page, currently, I have it set to display 5 snippets of posts, we have a ton of them on this category, and I need this page to have a line of links with

Newest 1, 2, 3, ... Oldest ...

I cannot get it to display that...

My current code looks like this:

 <?php  while ( have_posts() ) : the_post(); ?> 
 <div class="title text-center">    
 <h1><strong><?php the_title();?></strong></h1>
 <img src="<?php echo get_template_directory_uri(); ?>/images/title.png" title="title-line" alt="title-line">

     </div>
 <div id="no-more-tables" class="legel">
 <?php the_content();?></div>
<?php 
$posts = get_posts(array(
  'posts_per_page'  => 5,
  'category'=>'13'
));
if( $posts ): ?>

<div>
<?php foreach( $posts as $post ): 

    setup_postdata( $post );


    ?>

What do I need to add in order to make it do that? I am looking at this example, but it is not making sense to me:

<?php if ( $the_query->have_posts() ) : ?>
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <?php the_title(); ?>
    <?php the_excerpt(); ?>

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

<?php else : ?>
  <p><?php __('No News'); ?></p>
<?php endif; ?>

I would appreciate any suggestions you may have...

2
0

Something like this should work. I've added some explanation to the code in comments:

<div class="title text-center">    
    <h1><strong><?php the_title(); ?></strong></h1>
    <img src="<?php echo get_template_directory_uri(); ?>/images/title.png" title="title-line" alt="title-line">
</div>

<div id="no-more-tables" class="legel">
    <?php the_content(); ?>
</div>

<?php
// Get the page from the url e.g. domain.com/articles?paged=2 would be page 2.
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

// Use WP_Query instead of get_posts() - it is much better for proper queries.
// get_posts() should only really be used for getting a few posts (e.g. related
// articles)
$category_posts = WP_Query(
    array(
        'posts_per_page' => 5,
        'category'       => '13',
        'paged'          => $paged,
    )
);
?>

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

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

    <?php endwhile; ?>

    <?php
    // Set $big as a massive integer
    $big = 999999999;
    // Echo the pagination (E.g. Prev 1 2 3 3 4 5 Next)
    echo paginate_links(
        array(
            'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format'  => '?paged=%#%', // Matches your query above
            'current' => max( 1, get_query_var( 'paged' ) ),
            'total'   => $category_posts->max_num_pages,
        )
    );
    ?>

    <?php wp_reset_postdata(); // This must be run after every WP_Query ?>

<?php else : ?>

    No Posts

<?php endif; ?>
1
  • The reason I was using get_posts() is that I do have several categories that qualify as $posts on the website, look: ccdconline.org/ccdc-sound-off Everything under the menu blogs qualifies as $posts. – Jose Torres-Vega Jan 12 '20 at 19:30
0

Andy, it did not work, but I think it is because it is not matching to out template's ending, after the setup_postdata( $post ); ... We have this padding:

  <div>
    <h3 style="padding-bottom:6px;">
      <a href="<?php echo esc_url(get_permalink($post->ID)); ?>"><?php the_title(); ?></a>
    </h3></div>
    <div style="color:#595959;">Posted on: <?php echo date('Y/m/d',strtotime($post->post_date)); //echo date('F j, Y', strtotime($post->post_date)); ?></div>
    <div><?php echo '<p style="padding-bottom:6px !important;">' . get_the_excerpt() . '</p>'?>
    </div>
  <?php endforeach; ?>
</div>
<?php wp_reset_postdata(); ?>

<?php endif; ?>
<?php   endwhile;  // End of the loop. ?>
<?php get_footer(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.