Note that there are some explanatory texts on larger screens.

plurals
  1. POWordPress Previous and Next Button Exclude Multiple Categories
    primarykey
    data
    text
    <p>I have a WordPress blog where posts have multiple categories selected. I am using a "Previous Post" and "Next Post" button at the bottom of each post. I would like the next or previous post to be in the same category-- wordpress allows for this paramater in the <code>next_post_link</code> and <code>previous_post_link</code> functions, however it gets complicated when you have more than one category-- it chooses the category with the lowest category ID, which leads to inconsistent post navigation.</p> <p>What I want to do is get the post category from the permalink, which is formatted as /%category%/%postname% then get an array of all categories, remove the current post category from that array, then exclude that array from the next and previous post link, effectively leaving only the current category (as dictated by the permalink). For example, if the permalink is "myurl.com/music/september-guitar-post", I want to exclude all categories <em>except</em> music from the previous and next post links.</p> <p>Here is the code I'm using (that doesn't seem to be functioning how I want it to):</p> <pre><code>function remove_item_by_value($array, $val = '', $preserve_keys = true) { if (empty($array) || !is_array($array)) return false; if (!in_array($val, $array)) return $array; foreach($array as $key =&gt; $value) { if ($value == $val) unset($array[$key]); } return ($preserve_keys === true) ? $array : array_values($array); } $link = get_permalink(); $link = explode('/',$link); $currentCat = $link[3]; $currentCat = get_category_by_slug( $currentCat ); $currentCatId = $currentCat-&gt;term_id; $categories = get_categories(); foreach($categories as $category) { $catList[] = $category-&gt;term_id; } $exclude = remove_item_by_value($catList, $currentCatId); $exclude = implode(' and ',$exclude); previous_post_link('%link', '&amp;laquo; Previous post', TRUE, $exclude); next_post_link('%link', '&amp;laquo; Previous post', TRUE, $exclude); </code></pre> <p>All the code works until trying to use the $exclude string as a parameter in the next/prev post link functions. Upon further testing, simply trying to exclude a single category does not seem to work (<code>next_post_link('%link', '&amp;laquo; Previous post', TRUE, '3')</code> ) for example. Can anyone confirm that this code actually functions as the documentation says it does?</p> <p>How do I make this work?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload