Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WordPress Stay in Category
- Author: Mac McDonald
- Contact: Use the Contact Us form under About on BlueGrassMiataClub =dot= com
- By default, the previous/next_post_link in single.php does not stay in
- category. If you view a category archive, then select a single post,
- the prev/next link may not show a post in the same category.
- If you set the 'in_same_cat' parameter to the previous/next_post_link
- calls in single.php to 'true', single.php will stay in category
- provided the posts belong only to one category. If posts belong to
- more than one category, the prev/next links may point to posts in one
- of the categories other than the one from the category
- archive. Changing 'in_same_cat' will also affect how a blog works,
- because you normally don't want to stay in category when coming from a
- blog post list.
- Solving this problem requires several steps:
- 1. Modify archive.php (or category.php, if it exists) to pass the
- 'stayincat' parameter in the URL of single posts.
- 2. Modify single.php to detect the 'stayincat' parameter, set a global
- category id for the use by following filters, and set the
- 'in_same_cat' to 'true' in previous/next_post_link.
- 3. Add filters for previous/next_post_join so that only the global
- category id is selected, instead of all categories for the current
- post.
- 4. Add filters for previous/next_post_link to pass the 'stayincat'
- parameter in the links to other single pages.
- 5. Add a replacement function for add_query_arg because it does not
- work on the links in the previous/next_post_link filters.
- Here is the code for archive.php (or category.php):
- <?php while (have_posts()) : the_post(); ?>
- <div <?php post_class() ?>>
- <?php $permalink = get_permalink(); // MAM modification to stay in category - see single.php
- if (is_category()) $permalink = add_query_arg('stayincat',get_query_var('cat'),$permalink); ?>
- <h3 id="post-<?php the_ID(); ?>"><a href="<?php echo $permalink; ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
- Here is the code for single.php:
- <div class="navigation">
- <?php $mam_global_stay_in_cat = ($_GET['stayincat']) ? $_GET['stayincat'] : false;
- if ( $mam_global_stay_in_cat ) { ?>
- <div class="alignleft"><?php previous_post_link('« %link','%title',true) ?></div>
- <div class="alignright"><?php next_post_link('%link »','%title',true) ?></div>
- <?php } else { ?>
- <div class="alignleft"><?php previous_post_link('« %link') ?></div>
- <div class="alignright"><?php next_post_link('%link »') ?></div>
- <?php } ?>
- </div>
- And here are the filters and functions. They can go in functions.php,
- or at the top of single.php:
- // MAM - modification to stay in the category set in archive.php
- function mam_get_previous_post_join_filter ( $join, $in_same_cat = false, $excluded_categories = '' ) {
- global $mam_global_stay_in_cat;
- if ($mam_global_stay_in_cat) {
- $join = preg_replace('/tt.term_id IN \([^(]+\)/',"tt.term_id IN ($mam_global_stay_in_cat)",$join);
- }
- return $join;
- }
- function mam_get_next_post_join_filter ( $join, $in_same_cat = false, $excluded_categories = '' ) {
- global $mam_global_stay_in_cat;
- if ($mam_global_stay_in_cat) {
- $join = preg_replace('/tt.term_id IN \([^(]+\)/',"tt.term_id IN ($mam_global_stay_in_cat)",$join);
- }
- return $join;
- }
- function mam_previous_post_link_filter ($link='') {
- global $mam_global_stay_in_cat;
- //echo '<p>PREV LINK BEFORE:' . htmlspecialchars($link) . '</p>';
- if ($mam_global_stay_in_cat && $link) {
- $link = mam_add_query_arg('stayincat',$mam_global_stay_in_cat,$link);
- //echo '<p>PREV LINK AFTER:' . htmlspecialchars($link) . '</p>';
- }
- return $link;
- }
- function mam_next_post_link_filter ($link='') {
- global $mam_global_stay_in_cat;
- //echo '<p>NEXT LINK BEFORE:' . htmlspecialchars($link) . '</p>';
- if ($mam_global_stay_in_cat && $link) {
- $link = mam_add_query_arg('stayincat',$mam_global_stay_in_cat,$link);
- //echo '<p>NEXT LINK AFTER:' . htmlspecialchars($link) . '</p>';
- }
- return $link;
- }
- function mam_add_query_arg ($key,$value,$link) {
- // Adds the parameter $key=$value to $link, or replaces it if already there.
- // Necessary because add_query_arg fails on previous/next_post_link.
- if (strpos($link,'href')) {
- $hrefpat = '/(href *= *([\"\']?)([^\"\' ]+)\2)/';
- } else {
- $hrefpat = '/(([\"\']?)(http([^\"\' ]+))\2)/';
- }
- if (preg_match($hrefpat,$link,$matches)) {
- $url = $matches[3];
- $newurl = add_query_arg($key,$value,$url);
- // echo '<p>OLDURL:' . htmlspecialchars($url) . '</p>';
- // echo '<p>NEWURL:' . htmlspecialchars($newurl) . '</p>';
- $link = str_replace($url,$newurl,$link);
- }
- return $link;
- }
- add_filter('get_previous_post_join','mam_get_previous_post_join_filter');
- add_filter('get_next_post_join','mam_get_next_post_join_filter');
- add_filter('previous_post_link','mam_previous_post_link_filter');
- add_filter('next_post_link','mam_next_post_link_filter');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement