WordPress Blog Home How to exclude a specified category of articles

Source: Internet
Author: User
Keywords WordPress article today specify the points

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

Today, I saw WordPress bloggers asked "WordPress blog home page How to exclude the specified classification of the article?" Why users want to do this for a lot of reasons, maybe you do not want to display on the home page on the display of their own life class articles, but only show some news class articles.

To do this, you need to change the function of the calling article in the loop. The WordPress default theme Twenty Ten and twenty eleven uses the The_post () function to invoke all blog posts.

<?php while (Have_posts ()): The_post ();?>

The The_post () function uses Wp_query. We can use the Wp_query function itself to customize which articles are invoked, or which archives, categories, and time articles can be displayed on the home page.

In addition, we can use the Query_posts function to control the loop cycle. This method is undoubtedly the best because it does not change your cycle. However, I will explain both methods, you can choose one that suits you.

Using function Wp_query

If you want to change your home page display effect, you need to edit your subject index.php file. Most topics use archive.php files as a display page for categories (category), Time (date), tags (tag), so you want to change an archive page as long as you modify the archive.php file. There are also a number of topics have specialized category.php, date.php, tag.php templates, modify the corresponding is good.

The loop for most theme files starts with the "Start loop." For example, in the twenty eleven theme, it looks like this:

<?php/* Start the Loop/?>
<?php while (Have_posts ()): The_post ();?>

You need to replace the following function:

The Query
$query = new Wp_query ($args);

The Loop
while ($query->have_posts ()): $query->the_post ();

To control which articles are displayed in the loop, you need to define the parameters in the Wp_query, that is, to modify

$query = new Wp_query ($args);.

At present, there are 5 kinds of parameters, the most common is the parameter is cat and Category_name.

If you want to display your tech category and its category ID is 1, then you can use the parameter cat:

$query = new Wp_query (' cat=1 ');

Or you can use parameter category_name to achieve the same effect:

$query = new Wp_query (' Category_name=tech ');

If you want to add a few more categories, you need to separate them with commas.

$query = new Wp_query (' cat=1,7,121 ');

Or

$query = new Wp_query (' category_name=news,reviews,tutorials ');

Exclude all articles from a category by adding a minus sign before the classification ID. Note that the Category_name argument cannot be used to exclude a category.

$query = new Wp_query (' cat=-1 ');

As mentioned above, there are 5 parameters, and I've already explained two of them: Cat and Category_name, all of which include or exclude certain categories from your list of articles.

The other three parameters are Category_and, category_in and category_not_in.

Category_and displays multiple categories of articles:

$query = new Wp_query (Array (' Category__and ' => Array (1, 7));

Category_in is an OR function:

$query = new Wp_query (Array (' category__in ' => Array (7, 127));

Exclude multiple categories:

$query = new Wp_query (Array (' category__not_in ' => Array (2, 6));

WP query is a very flexible function. Classification is only one of the parameters, you can also use:

Author parameters– Displays a list of articles from an author or multiple authors, and can also be excluded.

Tag parameters– displays or excludes some of the tag's list of articles.

Taxonomy parameters– Displays a list of articles for one or more custom categories ...

Post & page parameters– use IDs or page slugs to display specific articles.

Type & Status parameters– displays articles of some article type or articles with attachments.

Pagination parameters– determines the number of articles displayed per page.

The Offset parameter– article is displayed from somewhere.

Order & by parameters– Change the arrangement of the list of articles.

Sticky Post parameters– displays a list of articles that include or exclude top articles.

Time parameters– displays articles for certain periods.

Custom field parameters– The article that displays the related custom fields.

Permission parameters– displays published or private articles.

The Wp_query page provides an example for each parameter, and there is no specific usage here.

Using function query_posts

Query_posts can be very simple to customize the article you display on the page, and you do not have to change the existing loop code.

Query_posts ($args);

To use its functionality, you must call it before your loop loop starts. For example:

The Query
Query_posts ($args);

The Loop
while (Have_posts ()): The_post ();


If you plan to call the second loop in the page, make sure you use Wp_reset_query () to reset your loop. For example:

The Query
<?php query_posts ($args);?>

<?php/* Start the Loop/?>
<?php while (Have_posts ()): The_post ();?>

<?php Get_template_part (' content ', Get_post_format ());?>

<?php Endwhile;?>
<?php wp_reset_query ();?>


To display certain categories of articles, you need to use the cat parameters:

<?php query_posts (' cat=1,5,6 '); ?>

Again, to exclude certain categories, you need to add a minus sign in front:

<?php query_posts (' cat=-3,-5 '); ?>

It can also be used to display specific articles:

Query_posts (' p=2 ');

To define the number of articles to display per page, you can use the Posts_per_page parameter:

Query_posts (' posts_per_page=5 ');

To get more information, visit the Query_posts page.

Use Plug-ins to exclude certain categories of articles

For those who are not too cold to the code, you can also use the WordPress plugin. It is recommended that you use stencils Exclude and ultimate Category excluder.

Stencils Exclude

Stencils exclude allows you to exclude or include certain categories, authors, and tags in your home page, archive pages, search pages, RSS feeds, and use them very simply.

Ultimate Category Excluder

Ultimate Category Excluder allows you to easily exclude certain categories from the Home page, archive page, and RSS. Unlike stencils exclude, he can only exclude certain categories, not exclude authors, labels, etc.

Summary

Although stencils exclude and ultimate Category excluder plug-ins are very good, if you have a certain programming base, then you have to modify it manually.

Original link: http://laygle.com/2012/01/wordpress-exclude-categories/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.