previous and next post of same category on singlepost.php

I want to show previous and next posts of same category in wordpress My code is : <?php $prev_post = get_previous_post(); $next_post = get_next_post(); ?> <nav class="post-nav section"> <div class="row no-margin"> <?php // Display the thumbnail of the previous post ?> <div class="col-xs-12 col-sm-6 prev-post" style="padding:0px"> <?php $prevPost = get_previous_post(); $prevthumbnail = get_the_post_thumbnail($prevPost->ID); $post_tile=get_the_title($prevPost->ID); $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($prevPost->ID), 'full' ); ?> <div class="post-nav-thumb" style="background-image: url('<?php echo $thumb['0'];?>')" ></div> <div class="post-nav-content"> <div class="post-nav-subtitle"><?php previous_post_link('%link', 'Prev Post'); ?></div> <h2 class="post-nav-title"><span><?php previous_post_link('%link', $post_tile); …
Category: Web

Next/Previous Product with custom order by price & products inside the product category

How to create next/prev buttons that are price sorted and give results inside the product category you are in? This question has been asked multiple times and despite the multiple threads there wasn't an exact answer. Only relatively close ones that didn't do all of the above. Below you will find my answer which works in my tests so far.
Category: Web

Can the Next/Prev Post links be ordered by menu order or by a meta key?

I have a series of posts that are ordered by a meta_key value. They could also be arranged by menu order, if necessary. The next/prev post links (generated by next_post_link, previous_post_link, or posts_nav_link all navigate by chronology. While I understand this default behaviour, I don't understand how to change it. I found that it maps through to adjacent_post_link in link-template.php, but then it starts to seem fairly hard-coded. Is it recommended to re-write this from scratch to replace it, or …
Category: Web

Post in multiple categories do not include in previous / next post if one category in excluded

I have read the following post which appears to solve my problem but doesn't... excluded_categories parameter in next_post_link() behaving unexpectedly I have posts in say 3 categories but if they are included in category 'update' (which is ID 96) I would like them excluded from the previous / next navigation system. From the above post I have edited the following code (this is all in my functions.php file as the single.php template calls for the navigation from there). <?php previous_post_link( …
Category: Web

Trying to get_adjacent_post - in same category not working

this is my first time here. I need my previous and next posts to show only posts on the same category. But I don't know how to do it. This is the code in my template (called Valenti) it is in a file called core.php /********************* CLEAN NEXT/PREVIOUS LINKS *********************/ if ( ! function_exists( 'cb_previous_next_links' ) ) { function cb_previous_next_links() { $cb_previous = get_adjacent_post( false, '', true ); $cb_next = get_adjacent_post( false, '', false ); if ( ( $cb_next != …
Category: Web

Previous/Next custom post links within custom taxonomy

I have seen this issue in quite a few threads so apologies if I've replicated an issue here but I've had no joy in attempting to implement the solutions I have found. I have a custom post type: show, to which is assigned a custom taxonomy: show_status. This can be set either to current or past. In the single-show.php template of my theme I have placed the following: <div class="newerlink"><p><?php next_post_link('%link', 'Next ›', $in_same_term = true, $excluded_terms = '', $taxonomy …
Category: Web

How to get next post link of child custom post type from parent post and get next post link of parent post from the last child post?

I have a custom post type with this structure: First Parent 1st child of First Parent 2ndchild of First Parent 3rd child of First Parent Second Parent 1st child of Second Parent 2nd child of Second Parent 3rd child of Second Parent I'm trying to make previous_post_link() and next_post_link() to work that when you are in the First Parent, the next_post_link() will go to the 1st child of First Parent and then when you are in the 1st child of …
Category: Web

Next / previous posts link doesn't show up with Posts 2 Posts

I am working on a Wordpress project where I use the Posts 2 Posts plugin to show posts from one post type to posts from another post type. This works great but for some reason my next_posts_link() and previous_posts_link() doesn't show up. I have specified the paged variable in my code so it should work. Can anyone see what I am doing wrong? $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; $connected = new WP_Query( array( 'connected_type' …
Category: Web

get_next_post_link() and get_previous_post_link() return wrong posts

I'm using get_next_post_link() and get_previous_post_link() within the loop but the returned posts are not correct. get_next_post_link() shows the previous post and get_previous_post_link() gives the current post. Below the context of these links: $args['name'] = $postname; $query = new WP_Query($args); if($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $id = get_the_ID(); $title = get_the_title(); $content = get_the_content(); $nextpost = get_next_post_link('Next: %link'); $previouspost = get_previous_post_link('Prev: %link'); } }
Category: Web

WordPress not showing pagination links

In my WordPress Index.html I am pulling out the 3 latest posts by using <?php query_posts('posts_per_page=3') ?> <?php while(have_posts()) : the_post(); ?> //rest of the post related data <?php endwhile; wp_reset_query(); ?> <?php do_action('show_navigation'); after that I want to fetch the next 3 or previous 3 posts and for this I am trying if ( ! function_exists( 'theme_content_navigation' ) ) { function theme_content_navigation( ) { global $wp_query; if ( $wp_query->max_num_pages > 1 ) { ?> <nav class="navigation" role="navigation"> <div class="nav-previous"><?php …
Category: Web

CPT : Next or Previous term link when has not adjacent post

My structure is like this : Book name (cpt) --- chapters (taxonomy) -------- chapter 1 (term) -------- Chapter 2 (term etc) I have some posts into "book" added in terms I wish in single-book.php add next and previous links but on the last post (so if there is not next post) display the link to chapter 2 to continue reading for the user. I don't know how to do this... This is my code : <div class="previous-post-link position-absolute"> <?php global …
Category: Web

Add anchor tag on previous_image_link / next_image_link

I am using big headers on my website.I need the next/previous_image_link to target to the picture , not the top of the page. Is it possible to add anchor or to set the target on next/previous_image_link? or any other method to go to specific position on page. I found on my image.php: <div class="next l mb"><i><?php echo previous_image_link('','Previous Image') ?> </i></div> </i></div>
Category: Web

How to filter get previous post function by meta value DESC and post date DESC?

My loop displays featured posts and then posts in reverse chronological order. However when I use <?php echo get_previous_post(); ?> it grabs posts in reverse chronological order. How do I add a filter to the previous post function similiar to this but instead filter by an array like this: 'orderby' => array( 'meta_value' => 'DESC', 'date' => 'DESC') ? Code: UPDATE: Cleaned up index.php loop after help from @sMyles and @JackJohansson Index.php loop: $args = array( 'posts_per_page' => - 1, …
Category: Web

How to get next and previous post links, alphabetically by title, across post types, in a shortcode

I need next and previous post links in a single post page using a "page-builder" type of CMS—specifically the DIVI theme by Elegant Themes. So, I need the links to be able to appear using Shortcodes, as the controls in the template, along with a lot of the default template stuff, are hidden when you use the page builder. AND, my client needs to be able to manipulate where in the page layout the next/previous post controls appear, depending on …
Category: Web

How to get next and previous post into ajax formed modal windows?

On the archive page, by clicking on a news item, I need to implement the appearance of news in a modal window. Well, there is nothing difficult here if you use ajax. Here is my js code: const newsItem = document.querySelectorAll('.news-item'); if(newsItem !== null){ newsItem.forEach(item => { item.addEventListener('click', function(){ let dataAttribute = item.getAttribute('data-new-id'); $.ajax({ type : 'POST', url : wp_helper.ajax_url, data : { postid : dataAttribute, action : 'loadmodal', }, success : function( data ){ $('#exampleModal .modal-body').html(data); lazyLoad(); } }); …
Category: Web

How can I limit the length of the previous/next posts in my Wordpress Theme?

I am using the following code to show next/previous posts in my wordpress theme. <?php previous_post_link('<span class="left">← %link</span>'); next_post_link('<span class="right">%link →</span>'); ?> It's working but I want to limit the post title which is displayed in the link to a specific length because too big links don't work because of the space I have. And I want to have the arrow within the link so that it shows link and arrow with the same style. Is that possible aswell? Thank …
Category: Web

Order by post_date does not work in Previous and Next post pagination

I have added a previous and next pagination with thumbnail for my single post template but it does not showing the correct previous and next post. Also, saw its showing the previous and next post order by post ID and tried to change the order by post_date but it does not work. Below are the code of post navigation. Can someone help me please... function wpse73190_adjacent_post_sort( $orderby ){ return "ORDER BY p.post_date DESC LIMIT 1"; } add_filter( 'get_previous_post_sort', 'wpse73190_adjacent_post_sort' ); …
Category: Web

Infinite next / prev post in single.php page

I have a question regarding this code. When the last entry appears in then "next" I show the same page all the time. In addition, I would like to add a thumbnail display of the next post and a title to this. Is it possible? <?php if( get_adjacent_post(false, '', true) ) { previous_post_link('%link' , '%title', '← '); } else { $first = new WP_Query('cat=3&posts_per_page=1&order=DESC'); $first->the_post(); echo '<a href="' . get_permalink() . '">←</a>'; echo ('%title'); wp_reset_query(); }; if( get_adjacent_post(false, '', false) …
Category: Web

Using previous_post_link and next_post_link to wrap around post sequence

For a single custom post type, I'm using previous_post_link and next_post_link with simple arrows. (Although text arrows are shown here, in the actual I'm using graphics with the text hidden via CSS.) Code: <nav class="arrowNav"> <div class="nav-previous"> <?php previous_post_link('%link', '«'); ?> </div> <div class="nav-next"> <?php next_post_link('%link', '»'); ?> </div> </nav> This works fine except for the edge cases. I'd like the posts to wrap around: that is, for the first post, I'd like previous_post_link to link to the last post, …
Category: Web

next/previous post links for specific category when post belongs to multiple categories

I have a category called "Featured Photo" that is category ID 2. I want to create next-post and previous-post links that contain links only for the next and previous posts in category ID 2. However, many "Featured Photo" posts also belong to more than one category, and I have so far been unable to find a solution that limits the "Next" and "Previous" links to category 2. If I do not exclude any categories in the previous_post_link() and next_post_link() calls, …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.