admin管理员组

文章数量:1122832

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');

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');
Share Improve this question edited Jul 23, 2014 at 7:28 kia4567 asked Jul 23, 2014 at 3:06 kia4567kia4567 2258 silver badges23 bronze badges 2
  • Your ID to exclude is wrong, you say your ID is 98 but in your code it is 97 – Pieter Goosen Commented Jul 23, 2014 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 Commented Jul 23, 2014 at 7:29
Add a comment  | 

2 Answers 2

Reset to default 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

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.

本文标签: phpLooking to exclude blog posts from category PreviousNext buttons