• Resolved andrew666

    (@andrew666)


    Hi there!

    First off, love the simplistic layout of this theme. Thanks for taking the time to build it and offer it for free!

    My question is simple.. I need single post pages’ next/previous post links to traverse the same category to prevent category crossovers which are happening by default. I looked through single.php and found get_next_post_link and get_previous_post_link, removed “get_” and added TRUE to the end of the parameters to initiate “in_same_term” but that didn’t work. Sample below:

    $next_post_btn = next_post_link('%link', '<span class="btn-icon"><i class="fa fa-chevron-circle-right fa-4x"></i></span>', TRUE );

    I’m assuming I have to change something in class-tube-pagination.php but thought I’d ask for a quick solution instead of having to go through the code myself.

    Cheers!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Theme Author tubegtld

    (@tubegtld)

    Hey there, thanks for the kind words about the theme.

    I’d always recommend against hacking the theme directly.

    You’re almost always better off creating a child theme or plugin.

    To that end, please revert any changes to the theme as we won’t need them here.

    Also, the $next_post_btn code isn’t really used and should be removed. Sorry for the false lead.

    If you were to dig around in class-tube-pagination.php, you’d find that (unfortunately) the theme doesn’t use (or expose for filtering) the in_same_term, excluded_terms, or taxonomy arguments for get_next_post_link and get_previous_post_link.

    This would be a lot easier if it did.

    Since it doesn’t, the best approach would be to create a simple plugin.

    I’ve put one together below that you can drop into your plugins folder.

    It’s nothing glamorous, but based on my quick testing it should do exactly what you need.

    Drop a reply with any questions.

    ——————–

    <?php
    /*
    Plugin Name: .TUBE Prev / Next Post in Term
    Author: .TUBE gTLD
    Author URI: https://www.get.tube
    Version: 1.0.0
    Text Domain: tube-prev-next-in-term
    License: GPLv3 or later - http://www.gnu.org/licenses/gpl-3.0.html
    */
    
    TUBE_PrevNext_In_Term::init();
    
    class TUBE_PrevNext_In_Term {
      
      public static $instance;
      
      public static function init() {
        
        if ( is_null( self::$instance ) )
            self::$instance = new TUBE_PrevNext_In_Term();
        
        return self::$instance;
          
      }  
      
      
      // Constructor  
      function __construct() {
        
        add_action( 'wp_head', array( $this, 'setup_custom_pagination' ));
      
      }
      
      
      function setup_custom_pagination() {
        global $tube_theme;
        // remove action to show the previous / next posts links after the content
        remove_action('tube_after_post_content', array( $tube_theme::$tube_pagination, 'prevnext_post_links' ), 100 );
        remove_action('tube_before_post_content', array( $tube_theme::$tube_pagination, 'prevnext_post_links' ), 100 );
        
        // add action to show the custom previous / next posts links after the content
        add_action('tube_after_post_content', array( $this, 'prevnext_post_links_in_term' ), 100 );
        add_action('tube_before_post_content', array( $this, 'prevnext_post_links_in_term' ), 100 );
      
      }
      
      
      // output the custom prevnext_post_links
      function prevnext_post_links_in_term() {
        
        $prevnext_post_links = $this -> get_prevnext_post_links_in_term();
        
        echo wp_kses_post( $prevnext_post_links ); 
        
      }
      
      
      // get the prevnext_post_links
      function get_prevnext_post_links_in_term() {
        
        // get the current post object
        $the_post = get_queried_object(); 
        
        // get the current post's post type
        $post_type_obj = get_post_type_object( $the_post -> post_type );
      
        // get the singular name of the post type
        $post_type_name = $post_type_obj->labels->singular_name;
      
        $prev_label_text = sprintf(
          '%1$s %2$s', 
          _x('Previous', 'Pagination: prev / next post links (used on single post page)', 'tube-prev-next-in-term'), 
          esc_html( $post_type_name )
        );
            
        $next_label_text = sprintf(
          '%1$s %2$s', 
          _x('Next', 'Pagination: prev / next post links (used on single post page)', 'tube-prev-next-in-term'), 
          esc_html( $post_type_name )
        );      
        
        // get the next post link
        $next_post_link = get_next_post_link('%link', '<span class="btn-text">' . $next_label_text . '</span> <span class="btn-icon"><i class="fa fa-chevron-circle-right"></i></span></span>', true);
        
        // get the previous post link
        $prev_post_link = get_previous_post_link('%link', '<span class="btn-icon"><i class="fa fa-chevron-circle-left"></i></span> <span class="btn-text">' . $prev_label_text . '</span>', true);   
        
        // if neither previous nor next, do nothing
        if ( ! $next_post_link && ! $prev_post_link ):
          return;
        endif;
        
        // generate the prev / next post links HTML
        ob_start();
        ?>
        <div class="pagination-wrap pagination-prevnext clearfix"> 
            
          <?php 
          if ( $prev_post_link ):
            echo $prev_post_link;
          endif;
          
          if ( $next_post_link ):
            echo $next_post_link;
          endif;
          ?>
            
        </div>
        
        <?php
        
        // grab the output and return
        $output = ob_get_clean();
        
        return $output;    
        
      }
    
    }
    Thread Starter andrew666

    (@andrew666)

    I guess I didn’t mention that I was using a child theme 🙂 I wouldn’t dare hack any of the well thought out code you developed (well.. let’s not discuss the leftover next/prev posts code in single.php, heh).

    The plugin you threw together works great for posts! Thanks! I can use it there for sure. Unfortunately, I’m using the Seriously Simple Podcasting plugin that adds some taxonomy that makes the prev/next links vanish completely on those pages. I’ll have to take a closer look tomorrow to see what might be happening. For all I know, it could just be a matter of adding some small code snippet to your plugin. If you have a chance before I do (road trip recovery is a harsh mistress), take a look at the plugin -> https://www.seriouslysimplepodcasting.com/ .. install it, make a couple posts in different “Series” and watch as the buttons vanish – magic, I tell you! 🙂 Alternatively, I can make you an admin account on my staging site so you don’t have to go through all the aforementioned fuss.

    Cheers,
    Drew

    Thread Starter andrew666

    (@andrew666)

    I fixed it! Albeit the quick and dirty way. Your plugin code was very helpful. I simply added an if/else to cover the custom podcast taxonomy. I hope this doesn’t slow down page loads. Here’s the code:

        // get the next post link
        if( has_term( '', 'series' ) ) {
        $next_post_link = get_next_post_link('%link', '<span class="btn-text">' . $next_label_text . '</span> <span class="btn-icon"><i class="fa fa-chevron-circle-right"></i></span></span>', true, '', 'series');
        }else{
        $next_post_link = get_next_post_link('%link', '<span class="btn-text">' . $next_label_text . '</span> <span class="btn-icon"><i class="fa fa-chevron-circle-right"></i></span></span>', true);
    }
        
        // get the previous post link
        if( has_term( '', 'series' ) ) {
        $prev_post_link = get_previous_post_link('%link', '<span class="btn-icon"><i class="fa fa-chevron-circle-left"></i></span> <span class="btn-text">' . $prev_label_text . '</span>', true, '', 'series'); 
        }else{
        $prev_post_link = get_previous_post_link('%link', '<span class="btn-icon"><i class="fa fa-chevron-circle-left"></i></span> <span class="btn-text">' . $prev_label_text . '</span>', true); 
    }

    Thanks for the help! Although I don’t think it’s my place to do so, may I suggest that a future update include code to support this feature (or make it easily modifiable).. perhaps a pref that allows users to choose in_same_term and/or excluded terms? It’s such an overlooked, yet useful feature in most themes. It wouldn’t be too difficult to add a query that pulls out both builtin and custom taxonomies and tosses them into a variable for parameter placement. If I ever get the time, I can probably do it for you, but it’s probably best to let the original dev do it since everyone develops in their own way… best not to step on toes 🙂

    Theme Author tubegtld

    (@tubegtld)

    Seems like an elegant enough solution! Thank you for sharing it here.

    Will definitely consider updating the theme to provide an easier way to achieve this.

    • This reply was modified 7 years ago by tubegtld.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘single post previous/next links by category’ is closed to new replies.