ThemeShaper Forums » Thematic

[closed]

Conditional tag for nav_below

(4 posts)
  • Started 3 years ago by AnotherAndrew
  • Latest reply from middlesister
  • This topic is not resolved
  1. AnotherAndrew
    Member

    I am trying to have posts from just one category to show in the additional posts navigation in nav_below.

    However, I am getting all posts to show in every category and they are displaying in order of the published date.

    I am using a single post template for category 10, if that matters.

    This is my code in functions.php:

    // MGD nav_below
    // Remove the default nav_below
    function remove_thematic_actions() {
    remove_action('thematic_navigation_below', 'thematic_nav_below', 2);
    }
    // Call 'nav_below' (above) during WP initialization
    add_action('init','remove_thematic_actions');

    function mgd_thematic_nav_below() {
    if (is_category('10')) { ?>
    <h3 class="additional-posts">Additional Posts</h3>
    <div id="nav-below" class="navigation">
    <div class="nav-previous"><span class="nav-direction">Previous</span><?php thematic_previous_post_link() ?></div>
    <div class="nav-next"><span class="nav-direction">Next</span><?php thematic_next_post_link() ?></div>
    </div>

    <?php
    } else { ?>
    <h3 class="additional-posts">Navigate Additional Posts</h3>
    <div id="nav-below" class="navigation">
    <?php if(function_exists('wp_pagenavi')) { ?>
    <?php wp_pagenavi(); ?>
    <?php } else { ?>
    <div class="nav-previous uppercase"><?php next_posts_link(__('<span class="meta-nav">«</span> Previous', 'thematic')) ?></div>
    <div class="nav-next uppercase"><?php previous_posts_link(__('Next <span class="meta-nav">»</span>', 'thematic')) ?></div>
    <?php } ?>
    </div>

    <?php
    }
    } // end nav_below

    Anyone know what I am doing wrong?

    Posted 3 years ago #
  2. middlesister
    Member

    I don't think it is necessary to remove and replace nav_below. You can use conditionals to filter the arguments passed to previous_post_link and next_post_link instead.

    function childtheme_filter_nav_links($args) {
    	if (is_category('10')) {
    		$args['in_same_cat'] = true;
    	}
    	return $args;
    }
    add_filter('thematic_previous_post_link_args','childtheme_filter_nav_links');
    add_filter('thematic_next_post_link_args','childtheme_filter_nav_links');
    Posted 3 years ago #
  3. AnotherAndrew
    Member

    middlesister,

    Thanks for the suggestion but that didn't work.

    Is there not a way to use conditionals in the code I have above?

    It seems like this would work, but it doesn't:

    // ETC CODE
    function mgd_thematic_nav_below() {
    if (is_single() || is_category('10')) { ?>
    <h3 class="additional-posts">Additional Posts</h3>
    <div id="nav-below" class="navigation">
    <div class="nav-previous"><span class="nav-direction">Previous</span><?php thematic_previous_post_link() ?></div>
    <div class="nav-next"><span class="nav-direction">Next</span><?php thematic_next_post_link() ?></div>
    </div>
    // ETC CODE

    Posted 3 years ago #
  4. middlesister
    Member

    Ah sorry, I just reread your post a bit more carefully. is_category() is only true on category archive pages and you were on a single post page template you said?

    Maybe try is_page_template('name-of-template'), or is_page('page-ID') instead.

    function childtheme_filter_nav_links($args) {
    	if (is_page_template('my-template.php')) {
    		$args['in_same_cat'] = true;
    	}
    	return $args;
    }
    add_filter('thematic_previous_post_link_args','childtheme_filter_nav_links');
    add_filter('thematic_next_post_link_args','childtheme_filter_nav_links');

    Yes you can definately use conditionals in the code you posted. The trick is just finding the right one. :)
    I still think though that the reason that the previous and next post links are showing all posts instead of just one category is that you are calling thematic_previous_post_link() which by default refers to all the posts. We need to tell the previous_post_link function to only link to posts in the same category. Either filter the arguments to thematic_previous_post_link or use the wordpress functions directly in you own nav_below:

    // ETC CODE
    function mgd_thematic_nav_below() {
    if (is_page_template('my-template.php')) { ?>
    	<h3 class="additional-posts">Additional Posts</h3>
    	<div id="nav-below" class="navigation">
    	<div class="nav-previous"><span class="nav-direction">Previous</span><?php previous_post_link('%link', 'Previous in category', true); ?></div>
    	<div class="nav-next"><span class="nav-direction">Next</span><?php next_post_link('%link', 'Next post in category', true); ?></div>
    	</div>
    	<?php
    }
    // ETC CODE

    Check out the wordpress codex at http://codex.wordpress.org/Function_Reference/previous_post_link on how to format the link.

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.