WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Help creating a link that always points to the newest static page (10 posts)

  1. chrisanthropic
    Member
    Posted 3 years ago #

    Hi all,

    I'm using WordPress as a CMS with the loop displayed on the main page.
    My wife posts a new chapter of her book every week as a PAGE (paginated) and then writes a POST with notes and a link to it.

    We're creating a series of links in the header:
    "First Chapter"
    "Newest Chapter"
    "Next"
    "Previous"

    I'm using the "next-page-not-next-post" plugin for the next/previous links, but I can't for the life of me figure out how to make a "Newest Chapter" link that dynamically links to the most recent static PAGE.

    The site's still local only for now, but here's a link to a screenshot for clarification.

    Thanks for the help!

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    something like this might work:

    <?php $latest = get_pages('number=1&sort_column=post_date&sort_order=desc');
    echo '<a href="' . get_permalink($latest[0]->ID) . '">Newest Chapter</a>'; ?>

    http://codex.wordpress.org/Function_Reference/get_pages
    http://codex.wordpress.org/Function_Reference/get_permalink

  3. chrisanthropic
    Member
    Posted 3 years ago #

    It's grabbing the latest post but still isn't registering pages.

    Thanks though, that gives me somewhere to start - I'll dig in to get_permalink a bit more.

    **Edit**
    Then again, reading through wp_get_pages it doesn't make sense why it's even pulling posts at all...now I'm really confused.

  4. chrisanthropic
    Member
    Posted 3 years ago #

    Ah, got it. It didn't like the fact that the new page was a sub page. Knowing that, I can get it figured out -- Thank you so much for your help!

  5. chrisanthropic
    Member
    Posted 3 years ago #

    For anyone interested, here is how I solved the issue (thanks to alchymyth's code):

    <?php $latest = get_pages('number=1&sort_column=menu_order&sort_order=desc');
    echo '<a href="' . get_permalink($latest[0]->ID) . '">Newest Chapter</a>'; ?>

    No subpages but since she's postings chapters from a book I just let non-chapter pages default to "order 0" and chapters use a 2 digit number where the first number is the book # and the second is the chapter #;

    so book 1 chapter 3 would be #13, chapter 4 is #14, etc. Table of Contents would be 10.

    Hope this helps with anyone else trying to publish stories online.

  6. chrisanthropic
    Member
    Posted 3 years ago #

    Ok, I'm back.

    The code that Alchymyth provided works great unless I use parent/child pages.

    Our current setup is like this:
    -Books
    - Series 1
    - Book 1
    -Ch1
    -Ch2
    - Book 2
    -Ch 1
    - Series 2

    Etc.

    I've tried hundreds of variations of wp_list_pages but I don't understand the code provided in the codex to list children and how to mix it with the code provided by Alchymyth.

    Currently I can only get it to list the newest page in a single given parent.

    Any suggestions?

  7. alchymyth
    Forum Moderator
    Posted 3 years ago #

    you are right - for some unexplained reason, get_pages() fails in some levels of child pages :-(

    try this:

    <?php $latest = get_posts('numberposts=1&post_type=page&orderby=post_date&order=desc');
    echo '<a href="' . get_permalink($latest[0]->ID) . '">Newest Chapter</a>'; ?>
  8. chrisanthropic
    Member
    Posted 3 years ago #

    Damn, that was quick. It worked, thank you yet again for the help. I truly appreciate it.

  9. alchymyth
    Forum Moderator
    Posted 3 years ago #

    gee - you are even quicker testing it ;-)

    thanks for marking this thread as 'resolved'

  10. chrisanthropic
    Member
    Posted 3 years ago #

    Marked as solved.

    As a note, I tried orderby=menu_order and it works as well.

    Thanks again!

Topic Closed

This topic has been closed to new replies.

About this Topic