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

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

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

previous next with custom-post-type by order attribute

I have a custom-post-type called 'artists'. I list them by the order attribute (menu_order). $args = array( 'post_status' => 'publish', 'post_type' => array('artists), 'orderby'=> array('menu_order'=>'ASC' ), ); $wp_query = new WP_Query( $args ); When I use the_post_navigation() to get the previous and next links, it takes the date as the order instead of the menu_order. How can I get it to work right in this case?
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 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

ACF Relationship Previous Next links of CPT

I have a CPT for artworks with custom taxonomy for artists. Then I have another CPT for artists. The artist pages are basically the the page title (artist name) and a relationship field where you add the artist’s artwork from the artworks CPT where you can filter by the custom taxonomy (since there are 50+ artists and 100s of artworks). Each artwork is its own page as mentioned. When you click on the artwork from an artist page, we want …
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

Wordpress post pagination continuous

I have these shortcodes which I am using as links in my front end to get the next post and previous post. When I get to the last post it just keeps reloading the last post again. How can I get it to loop back to the first post? function prev_shortcode($atts) { $prev_post = get_previous_post(); $permalink = get_permalink($prev_post); return ($permalink); } add_shortcode( 'prev', 'prev_shortcode' ); function next_shortcode($atts) { $next_post = get_next_post(); $permalink = get_permalink($next_post); return ($permalink); } add_shortcode( 'next', 'next_shortcode' …
Category: Web

Get previous/next post from (specific) category

I have posts with two categories, for example, 1st post is in category "product" and "shoes", 2nd is in category "product" and "shirts", 3rd is in category "product" and "sport clothes"... So, posts have one mutual category ("product" in this example), and one more specific category. I tried to get previous/next post with: $next_post = get_next_post( true ); $previous_post = get_previous_post( true ); But then, for example on 2nd post I have 1st and 3rd post as previous/next posts, because …
Category: Web

About

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