Home Forums WoodMart support forum Blog Post Naviagtion

Blog Post Naviagtion

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #212468

    luan0409
    Participant

    How to make blog post navigation on the same category?

    I mean when i click next post, next post is from the same category

    I dont want to use plugin cuz I have tested them all, nothing work well for me so far.

    Just help me to change some code is good

    #212686

    Hello,

    Please add this code to the functions.php of the child theme:

    function woodmart_posts_navigation() {
       ?>
          <div class="single-post-navigation">
              <?php
                   $next_post = get_next_post( true );
                   $prev_post = get_previous_post( true );
                   $archive_url = false;
                if ( get_post_type() == 'post' ) {
                   $archive_page = get_option( 'page_for_posts' );
                   $archive_url  = get_permalink( $archive_page );
                } elseif ( get_post_type() == 'portfolio' ) {
                   $archive_page = woodmart_tpl2id( 'portfolio.php' );
                   $archive_url  = get_permalink( $archive_page );
                }
                ?>
                <div class="blog-posts-nav-btn prev-btn">
                   <?php if ( ! empty( $next_post ) ) : ?>
                      <div class="posts-nav-inner">
                         <a href="<?php echo get_permalink( $next_post->ID ); ?>">
                            <span class="btn-label"><?php esc_html_e( 'Newer', 'woodmart' ); ?></span>
                            <span class="post-title"><?php echo get_the_title( $next_post->ID ); ?></span>
                         </a>
                      </div>
                   <?php endif; ?>
                </div>
                <?php if ( $archive_url && 'page' == get_option( 'show_on_front' ) ) : ?>
                   <div class="back-to-archive">
                      <a href="<?php echo esc_url( $archive_url ); ?>"><span class="woodmart-tooltip"><?php esc_html_e( 'Back to list', 'woodmart' ); ?></span></a>
                   </div>
                <?php endif ?>
                <div class="blog-posts-nav-btn next-btn">
                   <?php if ( ! empty( $prev_post ) ) : ?>
                      <div class="posts-nav-inner">
                         <a href="<?php echo get_permalink( $prev_post->ID ); ?>">
                            <span class="btn-label"><?php esc_html_e( 'Older', 'woodmart' ); ?></span>
                               <span class="post-title"><?php echo get_the_title( $prev_post->ID ); ?></span>
                         </a>
                      </div>
                   <?php endif; ?>
                </div>
          </div>
       <?php
    }

    Best Regards

    #356592

    b.brenner
    Participant

    Hello, I also miss a working post navigation within a category. I tried the code above in function.php but it doesn’t work.

    #356867

    Hello,

    Please replace the code with this one:

    function woodmart_posts_navigation() {
    		woodmart_enqueue_inline_style( 'page-navigation' );
    		?>
    			<div class="single-post-navigation">
    				 <?php
    						$next_post = get_next_post(true);
    						$prev_post = get_previous_post(true);
    
    						$archive_url = false;
    
    					if ( get_post_type() == 'post' ) {
    						$archive_page = get_option( 'page_for_posts' );
    						$archive_url  = get_permalink( $archive_page );
    					} elseif ( get_post_type() == 'portfolio' ) {
    						$archive_page = woodmart_tpl2id( 'portfolio.php' );
    						$archive_url  = get_permalink( $archive_page );
    					}
    					?>
    					<div class="blog-posts-nav-btn prev-btn">
    						<?php if ( ! empty( $next_post ) ) : ?>
    							<div class="posts-nav-inner">
    								<a href="<?php echo get_permalink( $next_post->ID ); ?>">
    									<span class="btn-label"><?php esc_html_e( 'Newer', 'woodmart' ); ?></span>
    									<span class="wd-entities-title"><?php echo get_the_title( $next_post->ID ); ?></span>
    								</a>
    							</div>
    						<?php endif; ?>
    					</div>
    
    					<?php if ( $archive_url && 'page' == get_option( 'show_on_front' ) ) : ?>
    						<div class="back-to-archive">
    							<?php woodmart_enqueue_js_script( 'btns-tooltips' ); ?>
    							<?php woodmart_enqueue_js_library( 'tooltips' ); ?>
    							<a href="<?php echo esc_url( $archive_url ); ?>"><span class="wd-tooltip"><?php esc_html_e( 'Back to list', 'woodmart' ); ?></span></a>
    						</div>
    					<?php endif ?>
    
    					<div class="blog-posts-nav-btn next-btn">
    						<?php if ( ! empty( $prev_post ) ) : ?>
    							<div class="posts-nav-inner">
    								<a href="<?php echo get_permalink( $prev_post->ID ); ?>">
    									<span class="btn-label"><?php esc_html_e( 'Older', 'woodmart' ); ?></span>
    									   <span class="wd-entities-title"><?php echo get_the_title( $prev_post->ID ); ?></span>
    								</a>
    							</div>
    						<?php endif; ?>
    					</div>
    			</div>
    		<?php
    	}

    Best Regards

Viewing 4 posts - 1 through 4 (of 4 total)