WordPress.org

Support

Support » How-To and Troubleshooting » How-To Link to the First and Last Posts for the Category of the Current Page

How-To Link to the First and Last Posts for the Category of the Current Page

  • 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

  • link that jumps to first item in Posts Array
    $link = $posts[0]->guid;
    echo ‘<anch href=’.$link.’>FIRST</anch>’;
    }
    ?>
  • 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

  • link that jumps to first item in Posts Array
    $link2 = $posts2[0]->guid;
    echo ‘<anch href=’.$link2.’>LATEST</anch>’;
    }
    ?>
  • 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.

  • The topic ‘How-To Link to the First and Last Posts for the Category of the Current Page’ is closed to new replies.
Skip to toolbar