WordPress Page Displays Specified Category Articles

Time:2019-8-14

In this paper, an example is given to illustrate the method of displaying specified classified articles on WordPress pages. Share for your reference. The concrete realization method is as follows:

The WordPress page displays the specified categories of articles:

First, you need to create a special page template, copy the index. PHP file directly in the theme, change the file name to the template name you want, such as tips. php, and then add the following code at the top of the tips. PHP file:

Copy code

The code is as follows:

<?php
/*
Template Name:tips
*/
?>
<?php if (have_posts()) ?>

Add the following code before the line above:

Copy code

The code is as follows:

<?php query_posts(‘showposts=10 & cat=1’);?>
// one of them is the classification ID.

Finally, create a new page, such as tips page, select the newly created tips template in the template selection, then publish, refresh the home page and click to enter this page to see the effect.

If you want to display a specific category article in the home page, you can refer to the following operation, the code is as follows:

Copy code

The code is as follows:

// Show only articles under Category 14
<?php while( have_posts()): the_post();update_post_caches($posts);?>
<?phpif(in_category(’14’)):?>
<div>
… Loop structure code…
</div>
<?phpendif;?>
<?phpendwhile;?>

The number in _category (’14’) represents the specified display of the classification ID, which can be found by editing the classification URL. The other case is that only articles under category 14 are hidden. Simply change if (in_category (’14’) to if (!(in_category (’14’)).

I hope this article will be helpful to the construction of WordPress.