Forums

[resolved] Link to most recent post in one category? (16 posts)

  1. solidsmoke
    Member
    Posted 1 year ago #

    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?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  3. solidsmoke
    Member
    Posted 1 year ago #

    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?

  4. vtxyzzy
    Member
    Posted 1 year ago #

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

  5. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  6. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  7. solidsmoke
    Member
    Posted 1 year ago #

    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?

  8. vtxyzzy
    Member
    Posted 1 year ago #

    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>
  9. solidsmoke
    Member
    Posted 1 year ago #

    Works beautifully! Thank you so much vtxyzzy!

    You can see the finished product here.

  10. solidsmoke
    Member
    Posted 1 year ago #

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

  11. vtxyzzy
    Member
    Posted 1 year ago #

    You are welcome!

  12. seanx820
    Member
    Posted 1 year ago #

    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)

  13. seanx820
    Member
    Posted 1 year ago #

    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 }
    ?>
  14. seanx820
    Member
    Posted 1 year ago #

    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>
  15. aminabbasian
    Member
    Posted 1 year ago #

    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.

  16. Frumph
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic