0

I'm working on some conditional elements here and I thought I had it but it's still not working right. You can see my site here.

What I've done is displayed only category Legal Libation Columns (98) on this page and excluded it form the blog itself. Perfect.

But when you click on a post to view the whole article then head to either the previous or next links to read another blog post it has them all mixed up.

Here is what I've placed in my single.php to prevent that. By themselves they work but together in an if else they

<?php if (has_category('Legal Libation Columns') && has_tag('Legal Libation Columns')): ?> 
    <span class="nav-previous"><?php previous_post_link('%link', '<span class="meta-nav">&larr;</span> Previous in Legal Libation Columns', TRUE); ?></span>
    <span class="nav-next"><?php next_post_link('%link', 'Next in Legal Libation Columns <span class="meta-nav">&rarr;</span>', TRUE); ?></span>
<?php  else: ?>
    <span class="nav-previous"><?php previous_post_link( '%link', __( '<span class="meta-nav">&larr;</span> Previous', 'toolbox' ) ); ?></span>
    <span class="nav-next"><?php next_post_link( '%link', __( 'Next <span class="meta-nav">&rarr;</span>', 'toolbox' ) ); ?></span>
<?php endif; ?>

I basically want this category to be completely separate from the blog. I'm very poor with php or programming methods, any advice on how to change my code to do this?

UPDATE: This is how I'm filtering my category out of the blog

function exclude_category($query) {
    if ( $query->is_home() ) {
        $query->set('cat', '-98');
    }
    return $query;
}
add_filter('pre_get_posts', 'exclude_category');
  • Your ID to exclude is wrong, you say your ID is 98 but in your code it is 97 – Pieter Goosen Jul 23 '14 at 6:10
  • Thanks for pointing that out. It had used to be 97, so that was just a typo. I've corrected it now. – kia4567 Jul 23 '14 at 7:29
0

You can exclude categories from your posts page using pre_get_posts

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() && !is_admin()) {
        $query->set( 'cat', '-1347' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

Might be better to use the category slug or I.D. Same with the tag.

There's also 5 parameters you might want to look at for previous_post_link and next_post_link

  • Thanks for the reply Brad. I'm however not looking to filter out the category (as I hope I've already done this), you can see the code I'm using to do this already at the bottom of my updated question. I'm just looking for an correct if else statement so that I don't run across any category 98 posts when hitting the Previous or Next links to view more posts when in the blog section. – kia4567 Jul 23 '14 at 5:58
  • I see that there is an exclude option on the next_post_link however I don't want to exclude the category on both the blog and category page when looking through the posts, so I need that if else to say. If I'm browsing the category posts show these links "Previous in Legal Libation Columns" & "Next in Legal Libation Columns". Else, show these links "Previous" & "Next". – kia4567 Jul 23 '14 at 6:01
0

I'm not sure how your posts are interact with each other and how are they related to one another via categories and tags, but from what I understand from your question is that posts from this one particular category should be completely separate from your blog.

Working with built-in taxonomies (categories, tags and post formats) and built-in post types can sometimes give undesired results as they are by default included in the main query. Building a post system that should interact differently and that should have no relation to other posts can be for this matter be troublesome.

Yes, there are custom queries (like WP_Query or pre_get_posts that change the main query before it is executed, and then there are conditional tags to include or exlude certain elements. But this can become quite lumpy and just a big shlep.

Looking what is given in your question, have you considered using custom post types in conjuction with custom taxonomies for these posts that you need separate from you blog.

The pro's of custom post types

  • Post types are exluded from the main query

  • You can have a separate archive and single page for each custom post type. See: Template Hierarchy

  • Custom post types can have it's own taxonomies (hierarchial or not) outside of the default post system. Also, built-in taxonomies can be added to custom post types if desired which will then be shared with your normal post system

  • pre_get_posts can easily be used to include custom post types in the main query if needed.

There are plenty plugins and tutorials out there about these topics. There are also plugins to help you bulk change post types of certain posts.

This might not answer your question, but this is just considerations that you can keep in mind for the near future.

  • I appreciate the information. This is an older website so it doesn't have the framework I'm used to now so I'm having to rely on other means. Worst case scenario I'll just completely take these links out as I can't spend too much time on this now. – kia4567 Jul 23 '14 at 7:32
  • I most probably think that you will need to exclude all categories from your first post links for this to work. How many categories does these posts belongs to, just the one mentioned, or are there other categories attached – Pieter Goosen Jul 23 '14 at 7:56
  • I've got an idea to exclude all categories dynamically from your first statement, but I need to go and test this first. I'm currently at work. Will code and test later tonight, SA time, then report back. Let me know in the meantime if you where able to solve this. – Pieter Goosen Jul 23 '14 at 8:29
  • There is only the one category that I need excluded (but if it happens that it works with all categories being excluded then that's fine too!). It's currently 2am here so I will be heading off but will check back later. Thanks for everything! – kia4567 Jul 23 '14 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.