How to exclude articles of a specified category from the WordPress blog homepage

Source: Internet
Author: User
Tags wordpress blog

Today, I saw a WordPress blogger asking "How do I exclude articles of a specified category on the WordPress blog homepage ?". There are many reasons why users want to do this. Maybe you don't want to display your own articles on the homepage, but only some news articles.

To do this, you need to change The function that calls The article in The loop. WordPress uses the the_post () function to call all blog posts by default with the Twenty Ten and Twenty Eleven themes.

<? Php while (have_posts (): the_post ();?>

The the_post () function uses WP_Query. We can use the WP_Query function to customize which articles are called, or which archive, category, and time articles can be displayed on the homepage.

In addition, we can use The query_posts function to control The Loop. 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.

Use the WP_Query Function

If you want to change the Display Effect of your homepage, You need to edit the index. php file of your topic. Most topics use archive. the PHP file is used as the display page for classification, date, and tag. To change an archive page, you only need to modify archive. PHP file. There are also some special templates for topics such as category. php, date. php, and tag. php. You can modify them accordingly.

The cycle of most topic files starts with "Start the Loop. For example, in the Twenty Eleven topic, it looks like this:

<? Php/* Start the Loop */?>
<? Php while (have_posts (): the_post ();?>

Replace it with the following functions:

// The Query
$ Query = new WP_Query ($ args );
 
// The Loop
While ($ query-> have_posts (): $ query-> the_post ();

To control which articles are displayed in a loop, you need to define the parameter in WP_Query, that is, modify

$ Query = new WP_Query ($ args );.

Currently, there are five parameters, which are commonly used: cat and category_name.

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

$ Query = new WP_Query ('cat = 1 ');

Alternatively, you can use the category_name parameter to achieve the same effect:

$ Query = new WP_Query ('category _ name = tech ');

To add more categories, use commas (,) to separate them.

$ Query = new WP_Query ('cat = 121, 100 ');

Or

$ Query = new WP_Query ('category _ name = news, reviews, tutorial ');

Add a minus sign before the category ID to exclude all articles of a certain category. Note that the category_name parameter cannot be used to exclude a category.

$ Query = new WP_Query ('cat =-1 ');

As mentioned above, there are five parameters. I have explained two of them: cat and category_name, both of which include or exclude certain categories from your article list.

The other three parameters are category_and, category_in, and category_not_in.

Category_and:

$ 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 flexible function. Classification is only one of the parameters. You can also use:

Author Parameters-displays the list of articles of one or more authors, which can also be excluded.

Tag Parameters-lists of articles that display or exclude some tags.

Taxonomy Parameters-displays a list of articles of one or more custom categories ..

Post & Page Parameters-use IDs or page slugs to display certain articles.

Type & Status Parameters-displays articles of certain Article types or articles with attachments.

Pagination Parameters-determine the number of articles displayed on each page.

Offset Parameter-the beginning of the article.

Order & Orderby Parameters-change the Order of the article list.

Sticky Post Parameters-displays a list of articles that include or exclude top posts.

Time Parameters-displays articles for certain Time periods.

Custom Field Parameters-displays articles about Custom fields.

Permission Parameters-displays published or private articles.

The WP_Query page provides examples for each parameter. The specific usage is not described here.

Use the query_posts Function

Query_posts can easily customize the articles you display on the page, and you do not have to change the existing circular code.

Query_posts ($ args );

To use its function, you must call it before your 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 articles of certain categories, you need to use the cat parameter:

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

Similarly, to exclude certain categories, you need to add a minus sign before:

<? 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 displayed on each page, you can use the posts_per_page parameter:

Query_posts ('posts _ per_page = 5 ');

For more information, visit the query_posts page.

Use plug-ins to exclude articles of certain categories

If you are not familiar with the code, you can also use the WordPress plug-in. We recommend that you use Simply Exclude and Ultimate Category Excluder.

Simply Exclude

Simply Exclude allows you to Exclude or include certain types, authors, and tags in the homepage, archive page, search page, and RSS. It is also very easy to use.

Ultimate Category Excluder

Ultimate Category Excluder allows you to easily exclude certain categories on the home page, archive page, and RSS. Unlike Simply Exclude, Simply Exclude can only Exclude certain categories, but cannot Exclude authors and tags.

  Summary

Although the Simply Exclude and Ultimate Category Excluder plug-ins are both good, if you have a certain programming Foundation, You Should manually modify it.

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.