WordPress.org

Ready to get started?Download WordPress

Forums

How-To Link to the First and Last Posts for the Category of the Current Page (1 post)

  1. NoahRodenbeek
    Member
    Posted 2 years ago #

    FIRST:

    <?php
    $categories = get_the_category();
    if ( count( $categories ) ) {
    $category = array_shift( $categories );
    $cat_id = $category->cat_ID;

    //get array of Posts for Category, default is 'order by date', set it to Ascending instead of default Descending
    $args = array( 'category' => $cat_id, 'order' => 'ASC' );
    $posts = get_posts( $args );

    //display

  2. link that jumps to first item in Posts Array
    $link = $posts[0]->guid;
    echo '<anch href='.$link.'>FIRST</anch>';
    }
    ?>
  3. LAST:

    <?php
    $categories = get_the_category();
    if ( count( $categories ) ) {
    //get array of Posts for Category, default is 'order by date', set it to Ascending instead of default Descending
    $args = array( 'category' => $cat_id, 'order' => 'DESC' );
    $posts2 = get_posts( $args );

    //display

  4. link that jumps to first item in Posts Array
    $link2 = $posts2[0]->guid;
    echo '<anch href='.$link2.'>LATEST</anch>';
    }
    ?>
  5. I asked about this a few times here and never got a response. In case someone from the future comes a'lookin', here's how I actually did it.

Topic Closed

This topic has been closed to new replies.

About this Topic