• Resolved solidsmoke

    (@solidsmoke)


    So I’m trying to get a webcomic kind of set up going. The idea is that users will stay in single post view, using next/previous buttons to read the comic page by page.

    I have the previous and next buttons working fine, but I want to add buttons for “First Page” and “Latest Page”.

    I looked in the codex for template tags but I couldn’t find any that were appropriate.

    I would just need a way to link to “first post in category” and “last post in category”. Any ideas?

Viewing 15 replies - 1 through 15 (of 15 total)
  • You could take the link returned for next post and alter it. Remove any page info for the ‘First Page’, and replace the page info with $wp_query->found_posts to get the ‘Last Page’ number.

    Thread Starter solidsmoke

    (@solidsmoke)

    I’m not entirely sure what you mean. I’m a bit new to PHP and wordpress in general, so I’m not entirely comfortable with writing my own code yet. šŸ™

    This is what I have so far for my little navigation bar

    <p class="details">
    <?php previous_post_link('%link', '<< Previous', TRUE); ?> |
    <?php next_post_link('%link', 'Next >>', TRUE); ?> </p>

    Would you be able to show me what you’re talking about?

    I can’t be more specific without knowing what your permalinks are like. Can you post a link to your site?

    I’m sorry, I just realized that my brain took a wrong turn when I read your post. When I saw ‘First Page’ and ‘Latest Page’, I was thinking pagination. Now, I realize that is not the case.

    How will you determine which post is ‘Next’ or ‘Previous’, much less ‘First’ and ‘Last’? The date is not reliable because you may insert or delete posts in the sequence at some later date. I suspect that you will need to add a custom field to your posts to set the order.

    Assuming you do stay with dates, here are the queries you need to get the first (earliest) and last (latest) posts:

    <?php
       $args = array(
         'cat' => 39,
         'posts_per_page' => 1,
         'order' => 'ASC'
    );
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) {
      $my_query->the_post(); ?>
      <a href='<?php the_permalink(); ?> ' >First Page</a>
      <?php }
    $args['order'] = 'DESC';
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) {
      $my_query->the_post(); ?>
      <a href='<?php the_permalink(); ?> ' >Last Page</a>
      <?php }
    ?>

    Replace 39 with your category ID and add styling for placement on the page.

    Thread Starter solidsmoke

    (@solidsmoke)

    Thank you so much! It’s so nice to have a solution after days of fruitless googling. šŸ™‚ Right now all the links work appropriately, though they are arranged kind of awkardly.

    “<< Previous Next >> First Page Last page”

    I’d like the previous/next buttons in between first/last, but if I move them there they stop working šŸ™ I guess the ordering of the query messes with how previous_post_link() and next_post_link() function?

    See if adding wp_reset_query() works, like this:

    <p class="details">
    <?php
       $args = array(
         'cat' => 39,
         'posts_per_page' => 1,
         'order' => 'ASC'
    );
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) {
      $my_query->the_post(); ?>
      <a href='<?php the_permalink(); ?> ' >First Page</a> |
    <?php }
    wp_reset_query(); ?>
    <?php previous_post_link('%link', '<< Previous', TRUE); ?> |
    <?php next_post_link('%link', 'Next >>', TRUE); ?>
    <?php $args['order'] = 'DESC';
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) {
      $my_query->the_post(); ?>
       | <a href='<?php the_permalink(); ?> ' >Last Page</a>
      <?php } ?>
    </p>
    Thread Starter solidsmoke

    (@solidsmoke)

    Works beautifully! Thank you so much vtxyzzy!

    You can see the finished product here.

    Thread Starter solidsmoke

    (@solidsmoke)

    Note: Needed another wp_reset_query(); after the last page link, otherwise all comments append to the last post.

    You are welcome!

    but i see one problem now, i was doing a similar thing, what do you do if you want 2 separate pages, 1 for comics, 1 for a blog, and you want the same functionality, i.e. go to first blog post if on a blog single page, or go to first comic page if on a single comic post…. i am thinking now, i may have a solution (hi again vtxzzy, you are the man)

    ok here is the solution, I added three things
    1) I grayed out next and previous when there is no next or previous post in that particular category
    2) I added some fancy >> with the &laquo; and &raquo;
    3) I checked which category I was actually in so I would only use this navigation for that particular category, so I could have one of those code blocks per type of page i have (i.e. one page displaying blog posts, one page displaying comic posts) or if i did not want this kind of navigation for blog posts, i would not use it

    <p class="top_nav">
    <?php $category = get_the_category();
    $category_id=$category[0]->cat_ID;
    if($category_id==4){
    //4 in this case is my category ID which is comics, you could also use cat_name as indicated here http://codex.wordpress.org/Function_Reference/get_the_category
    
       $args = array(
         'cat' => 4, //this indicates what category you are in
         'posts_per_page' => 1, //this displays 1 post per page (lol)
         'order' => 'ASC' //asscending (this will show 1st->last)
    );
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) { //this is the loop
      $my_query->the_post(); ?>
      <a href='<?php the_permalink(); ?> ' >&laquo;First Page</a> |
    <?php }
    wp_reset_query(); ?>
    	<span id="prev">
       <?php previous_post_link('%link', '&laquo; Previous', true); ?>
       <?php if(!get_adjacent_post(true, '', true)) { echo '<span>&laquo;Previous</span>'; } // if there are no older articles (greyed out) ?>
    </span> |
    <span id="next">
       <?php next_post_link('%link', 'Next &raquo;', true); ?>
       <?php if(!get_adjacent_post(true, '', false)) { echo '<span>Next &raquo;</span>'; } // if there are no newer articles (greyed out) ?>
    </span> |
    <?php $args['order'] = 'DESC';
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) {
      $my_query->the_post(); ?>
       <a href='<?php the_permalink(); ?> ' >Last Page&raquo;</a>
      <?php }
      wp_reset_query();
      ?>
    </p>
    <?php }
    ?>

    now i updated this nav to also have a random, AWESOME

    <p class="top_nav">
    <?php $category = get_the_category();
    $category_id=$category[0]->cat_ID;
    if($category_id==4){
    //4 in this case is my category ID which is comics, you could also use cat_name as indicated here http://codex.wordpress.org/Function_Reference/get_the_category
    
       $args = array(
         'cat' => 4, //this indicates what category you are in
         'posts_per_page' => 1, //this displays 1 post per page (lol)
         'order' => 'ASC' //asscending (this will show 1st->last)
    );
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) { //this is the loop
      $my_query->the_post(); ?>
      <a href='<?php the_permalink(); ?> ' >&laquo;First Page</a> |
    <?php }
    wp_reset_query(); ?>
    	<span id="prev">
       <?php previous_post_link('%link', '&laquo; Previous', true); ?>
       <?php if(!get_adjacent_post(true, '', true)) { echo '<span>&laquo;Previous</span>'; } // if there are no older articles (greyed out) ?>
    </span> |
    <span id="next">
       <?php next_post_link('%link', 'Next &raquo;', true); ?>
       <?php if(!get_adjacent_post(true, '', false)) { echo '<span>Next &raquo;</span>'; } // if there are no newer articles (greyed out) ?>
    </span> |
    <?php $args['order'] = 'DESC';
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) {
      $my_query->the_post(); ?>
       <a href='<?php the_permalink(); ?> ' >Last Page&raquo;</a>
      <?php }
      wp_reset_query();
     }
    ?>
    
    <div class="rando">
    <?php 
    
    $args = array(
         'cat' => 4, //this indicates what category you are in
         'posts_per_page' => 1, //this displays 1 post per page (lol)
         'orderby' => 'rand' //random
    );
    
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) { //this is the loop
      $my_query->the_post(); ?>
      <a href='<?php the_permalink(); ?> ' >Random</a>
    <?php }
    wp_reset_query(); ?>
    
    </div>
    </p>

    this is similar i think to what I am looking for.

    please view http://wordpress.org/support/topic/link-to-latest-post-by-category

    please let me know if you can help.

    You could always snag the comicpress theme and grab the functions/navigation.php file, inside of it is a function called comicpress_get_terminal_post_in_category() which is used to get either the first or last post in a particular category

    Probably better to get it from the Easel theme, the addons/commpress.php file, has first next previous last get permalink functions inside of it.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Link to most recent post in one category?’ is closed to new replies.