Your code problem is you get only the first category of a post because $cat[0]
is the first, not current.
Inside The Loop:
<?php
previous_post_link( // prints the formatted link
'<span>« %link</span>', // beautifying around
'Prev in this cat', // the text (%link) to be used in the span above
true, // confirm that you want only this category
'', // no categories to exclude
'category' // taxonomy name
);
echo " | "; // just a separator between next and prev
/* The same setup here, not shown to keep the answer clean */
next_post_link();
See previous_post_link() for parameters.
If you are outside of The Loop, let me know, I’ll update the answer and supply the additional code and comments.
Related Posts:
- adding custom fields to next and previous post link
- WordPress previous_post_link exclude posts with multiple categories
- Can I use get_adjacent_post for specific categories?
- Exclude Posts From Specific Category from Next and Previous post links
- Show category from “Next post” and “Previous post” [edited]
- previous/next_post_link in the same sub-category?
- Link to the next/prev posts in index loop in same category?
- Single.php Category Post Count
- Get next post in same category not working WP 4.7.4
- Post in multiple categories do not include in previous / next post if one category in excluded
- Change next_post and previous_link to navigate throught parent categories only
- how to implement next/prev within category archive?
- Post in Multiple Categories to stay in current category (permalink, next previous post link)
- next_post_link and previous_post_link in custom post type single page, in same non-custom category
- Get Prevous/Next Post Specific Category Without Excludes
- posts_nav_link(); picks up index.php instead of category.php as template
- How to get Next/previous_post_link to go through articles in top parent category
- Show next post in same category but start from the latest
- Next/Prev category post in same button
- Is there a function to modify existing previous/next post link without modifying theme files?
- Can I set a default featured image for a category?
- What’s the URL for a category archive?
- Weird behaviour when adding terms/term_taxonomies programmatically
- how to use a different domain/subdomain for authors/catagories on single site?
- Category count inside the link
- Is there a way to have duplicate category slugs?
- Multiple level category drop-down
- how to show posts of category random by session
- Showing Thumbnail from Previous and Next Posts
- Remove “Parent” Selection When Adding/Editing Categories?
- Show post only if match all categories
- Output link to category from WP_Query loop of woocommerce products
- How to get product count with respect to categories in WooComerce
- Password protect a specific category page/post
- Different excerpt styles per category, but chronological
- Show only one category
- Pagination Not Working on Category.php page
- Filter Home Page Posts in Buddypress [closed]
- Broken category pagination
- Additional featured post on first page
- Color different for the current category
- Displaying a full post instead of a slug per category?
- one category template for multiple categories
- How can I define a custom archive for childs of a specific category?
- How to Add Default Content for all Category Pages Before and After Content?
- Next Posts Prev Posts for Standard post format only
- Sub Categories Keep Redirecting to Pages With Same Slugs
- How to append category name to category ID in a form field
- Category template to show different categories based on parent
- Customize category page for different custom taxonomies
- Conditional IF current cat is the smallest/last/end child cat THEN
- Limit number of posts on a custom category template – breaking pagination
- How to create a custom meta box field in category? [duplicate]
- How to create a subcategory to a PAGE on WP?
- Single: Prev and next Post in same order as main query, in all categories
- How to show 1-5 of X Total Results
- Display posts from a category
- Finding the category id’s on category intersection pages
- SQL query to return categories and category parents
- How to Remove a Title from All Category and Tags Page?
- Display sibling categories on category page
- Getting the category title / description returns first matching post?
- WordPress Show Parent Category Description for Sub-categories
- I want to know how to output child categories related to parent categories
- why get_the_post_thumbnail( the_ID()) echos extra post id
- Allow user to select categories that will display in post loop
- Two Sections on Woocommerce Catergory Page [closed]
- How to fetch the highest category in the hierachry for a specific post?
- Woocommerce search form with category select
- How to not display subcategories in Recent Posts widget area?
- Getting the Most Recent Posts from Multiple Categories
- Display category title and three latest post inside the category
- How to store category and tags separatly on wordpress?
- Is the part “category” fixed to an URL to an archive or post?
- How can I use AJAX with check-box categories?
- wp_delete_term or wp_delete_category?
- Get post content intro text on category.php?
- WP_Query and pagination AGAIN?
- List Categories By Specified First Character
- the category city made as a subdomain
- Unable to browser posts in categories
- Displaying Single Product Page Parent and Child Category
- wp_get_post_categories not work currently on loop.php
- Inline If statement to echo CSS [closed]
- Get category base permalink
- WooCommerce showing active product categories on single product page
- how to exclude specific category types (uncategorized) from blog page?
- Confused about category.php template – not working
- Previous/Next custom post links within custom taxonomy
- Style a category page the same way as the homepage?
- Display Taxonomies in loop with template args
- How to assign image on specified category and not show the image on other categories
- How can I add a Categories page link to a menu? [closed]
- Display only the latest post
- Way to show content of a post, but if exceeds character limit revert to excerpt?
- get posts from 2 categories. (2 posts)
- How to add location as category?
- Converting Categories (with ACF fields) to custom taxonomies
- Why does my WordPress index has a category?
- How to remove the category from the url only for a specific category of articles on wordpress