• Resolved thatgoodlookingguy

    (@thatgoodlookingguy)


    I’ve looked around the tutorials but they all seem to have different codes and stuff so I couldn’t figure this problem out with my extremely limited knowledge. I’m using the Big Brother Theme.

    I want to make my previous / next buttons go to the previous and next post in the same category and I also want the buttons to say previous chapter and next chapter.

    This is my single.php

    <?php
    /**
     * The Template for displaying all single posts.
     *
     * @package big-brother
     */
    
    get_header(); ?>
    	<?php if( function_exists( 'big_brother_the_breadcrumbs' ) ) : ?>
    		<div class="breadcrumbs">
    			<?php big_brother_the_breadcrumbs(); ?>
    		</div>
    	<?php endif; ?>
    	<div class="primary content-area">
    		<main id="main" class="site-main" role="main">
    
    		<?php while ( have_posts() ) : the_post(); ?>
    			<div class="article-wrapper">
    				<?php get_template_part( 'content', 'single' ); ?>
    			</div>
    
    			<?php big_brother_content_nav( 'nav-below' ); ?>
    
    			<?php
    				// If comments are open or we have at least one comment, load up the comment template
    				if ( comments_open() || '0' != get_comments_number() )
    					comments_template();
    			?>
    
    		<?php endwhile; // end of the loop. ?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    and this is index.php

    <?php
    /**
     * The main template file.
     *
     * This is the most generic template file in a WordPress theme
     * and one of the two required files for a theme (the other being style.css).
     * It is used to display a page when nothing more specific matches a query.
     * E.g., it puts together the home page when no home.php file exists.
     * Learn more: https://codex.www.ads-software.com/Template_Hierarchy
     *
     * @package big-brother
     */
    
    get_header(); ?>
    	<div class="primary content-area">
    		<main id="main" class="site-main" role="main">
    
    		<?php if ( have_posts() ) : ?>
    
    			<?php /* Start the Loop */ ?>
    			<?php while ( have_posts() ) : the_post(); ?>
    
    				<?php
    					/* Include the Post-Format-specific template for the content.
    					 * If you want to override this in a child theme, then include a file
    					 * called content-___.php (where ___ is the Post Format name) and that
    					 * will be used instead.
    					 */
    					get_template_part( 'content', get_post_format() );
    				?>
    
    			<?php endwhile; ?>
    
    			<?php big_brother_content_nav( 'nav-below' ); ?>
    
    		<?php else : ?>
    
    			<?php get_template_part( 'no-results', 'index' ); ?>
    
    		<?php endif; ?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Website: Christo Enzo

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    It depends on what the function declaration for big_brother_content_nav() looks like. If it’s pluggable, you can create your own version. If not, it may still have appropriate filters and arguments to do what you want.

    If none of that is true, you can replace that function call on single.php with a custom function that uses WP functions to get the next/prev post link in the same category and using your own link text instead of what is provided by default.

    Thread Starter thatgoodlookingguy

    (@thatgoodlookingguy)

    <?php
    /**
     * Custom template tags for this theme.
     *
     * Eventually, some of the functionality here could be replaced by core features
     *
     * @package big-brother
     */
    
    if ( ! function_exists( 'big_brother_content_nav' ) ) :
    /**
     * Display navigation to next/previous pages when applicable
     */
    function big_brother_content_nav( $nav_id ) {
    	global $wp_query, $post;
    
    	// Don't print empty markup on single pages if there's nowhere to navigate.
    	if ( is_single() ) {
    		$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
    		$next = get_adjacent_post( false, '', false );
    
    		if ( ! $next && ! $previous )
    			return;
    	}
    
    	// Don't print empty markup in archives if there's only one page.
    	if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
    		return;
    
    	$nav_class = ( is_single() ) ? 'post-navigation' : 'paging-navigation';
    
    	?>
    	<nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>">
    		<h1 class="screen-reader-text"><?php _e( 'Post navigation', 'big-brother' ); ?></h1>
    
    	<?php if ( is_single() ) : // navigation links for single posts ?>
    
    		<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'big-brother' ) . '</span> %title' ); ?>
    		<?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'big-brother' ) . '</span>' ); ?>
    
    	<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
    
    		<?php if ( get_next_posts_link() ) : ?>
    		<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'big-brother' ) ); ?></div>
    		<?php endif; ?>
    
    		<?php if ( get_previous_posts_link() ) : ?>
    		<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'big-brother' ) ); ?></div>
    		<?php endif; ?>
    
    	<?php endif; ?>
    
    	</nav><!-- #<?php echo esc_html( $nav_id ); ?> -->
    	<?php
    }
    endif; // big_brother_content_nav
    
    if ( ! function_exists( 'big_brother_posted_on' ) ) :
    /**
     * Prints HTML with meta information for the current post-date/time and author.
     */
    function big_brother_posted_on() {
    	$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
    	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) )
    		$time_string .= '<time class="updated" datetime="%3$s">%4$s</time>';
    
    	$time_string = sprintf( $time_string,
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() ),
    		esc_attr( get_the_modified_date( 'c' ) ),
    		esc_html( get_the_modified_date() )
    	);
    
    	printf( __( '<span class="posted-on">%1$s</span> <span class="byline">%2$s</span>', 'big-brother' ),
    		sprintf( '<a href="%1$s" rel="bookmark">%2$s</a>',
    			esc_url( get_permalink() ),
    			$time_string
    		),
    		sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>',
    			esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    			esc_html( get_the_author() )
    		)
    	);
    }
    endif;
    
    /**
     * Returns true if a blog has more than 1 category
     */
    function big_brother_categorized_blog() {
    	if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
    		// Create an array of all the categories that are attached to posts
    		$all_the_cool_cats = get_categories( array(
    			'hide_empty' => 1,
    		) );
    
    		// Count the number of categories that are attached to the posts
    		$all_the_cool_cats = count( $all_the_cool_cats );
    
    		set_transient( 'all_the_cool_cats', $all_the_cool_cats );
    	}
    
    	if ( '1' != $all_the_cool_cats ) {
    		// This blog has more than 1 category so big_brother_categorized_blog should return true
    		return true;
    	} else {
    		// This blog has only 1 category so big_brother_categorized_blog should return false
    		return false;
    	}
    }
    
    /**
     * Flush out the transients used in big_brother_categorized_blog
     */
    function big_brother_category_transient_flusher() {
    	// Like, beat it. Dig?
    	delete_transient( 'all_the_cool_cats' );
    }
    add_action( 'edit_category', 'big_brother_category_transient_flusher' );
    add_action( 'save_post',     'big_brother_category_transient_flusher' );
    
    /**
     * Returns the URL from the post.
     *
     * @uses get_the_link() to get the URL in the post meta (if it exists) or
     * the first link found in the post content.
     *
     * Falls back to the post permalink if no URL is found in the post.
     *
     * @return string URL
     */
    function big_brother_get_link_url() {
    	$content = get_the_content();
    	$has_url = get_url_in_content( $content );
    
    	return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
    }

    Sorry for taking up your time, I don’t anything about programming. Is this the right file?

    Moderator bcworkz

    (@bcworkz)

    Don’t worry about my time, if it felt like a waste, I wouldn’t do this ?? I’m happy do be able to help. Sorry about the slow response, I took a short break from these forums.

    Yes! That is the right file. The fact that the function declaration begins with if ( ! function_exists()): means it is a “pluggable” function. You can “plug in” your own version with the same name in a child theme and your version will be used instead of the original.

    First, you need a child theme, if you haven’t already done so. (It’s pretty straight forward). While you’re editing and uploading files, define WP_DEBUG as true in wp-config.php so you will be notified of any problems with your edits. Set your new child theme as the active theme.

    Copy the entire function declaration for big_brother_content_nav() to your child’s functions.php, including the if ( ! function_exists()): part. Don’t forget the closing endif; before the next function declaration. You can edit this version as you please.

    There’s several conditionals in this function, I’m not sure which ones apply to your situation. You can temporarily add distinguishing text within quotes, or add an echo 'distinguishing text 1'; line inside the conditional to determine which part applies to your situation.

    In each case, the link is determined with previous_post_link() or similar variations. Check out the docs page for the function in question.
    https://developer.www.ads-software.com/reference/functions/previous_post_link/

    In this case, the theme’s version provides the first two parameters. It’s rather tricky to parse what constitutes one parameter. Try copying the line to a blank page and removing all characters except () ' " . , (except parenthesis, quotes, dots, commas). The structure is more clear.
    previous_post_link( '""', '"'.('','','').'' )
    There’s only one comma inside the outermost parenthesis, everything after that comma is one parameter – quotes and function returns all concatenated together with dots.

    The first parameter you should probably leave alone. The second is the link text. You could change all of that to simply ‘Previous Chapter’. There is an optional third parameter that is not supplied in the original. $in_same_term defaults to false. You want this to be true so that the link leads to the previous post/chapter in the same category. You probably do not need the additional parameters to exclude certain terms or specify a different taxonomy. You should end up with this:
    <?php previous_post_link( '<div class="nav-previous">%link</div>', 'Previous Chapter', true ); ?>

    Similar logic applies to the other function calls. Check the docs page for each one to ensure you’re setting or altering the correct parameters in each case.

    After this. you can no longer say you don’t know anything about programming ??

    Thread Starter thatgoodlookingguy

    (@thatgoodlookingguy)

    Wow, thanks for that thorough explanation. Nice and easy to understand and it solved my problem. I can’t thank you enough, thanks a bunch!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Previous / Next Buttons in the same Category’ is closed to new replies.