How to add the latest articles, random articles, and popular articles to the WordPress website template?, Wordpress interesting plug-in

1 year ago (2023-12-01) Chief Editor
6 minutes
three hundred and ten
zero

In the previous chapter, we have added the content code of the header and the left body for the homepage of the wordpress website template, and realized the call and paging effect of the wordpress whole site article list. In this section, we will modify the right sidebar of the homepage template of the wordpress website. Generally, the right sidebar of the WordPress blog theme template will have a list of "latest articles, random articles, hot reviews, hot tags, and hot articles". Today, we will add "Latest Articles, Random Articles, and Hot Reviews" to the sidebar of the wordpress template (as shown in the effect below). Let's have a look.

1、 Add the latest articles to the wordpress template sidebar.

Open the sidebar template file sidebar.php of the wordpress theme, and add the following code:

<div class="right_new"><h3>Latest Articles</h3><ul><? phpquery_posts(cat=0&posts_per_page=5&caller_get_posts=1&orderby=new); // Start query. while (have_posts()) : the_post(); // Loop query results. echo < li>< a href=".get_the_permalink()." title=".get_the_title().">; echo get_the_title().</ a></li>; endwhile; wp_reset_query(); // End the query.?></ ul></div>

In this code, we use the wordpress loop code again, which is described in the article list on the home page, but not more. Here we also use a function, query_posts(), to query the list of articles from the database wp_posts data table on the wordpress website.

The above query_posts() function uses four parameters:

Cat=0: specifies the classification ID of the article list, that is, which classification article to call. 0 indicates all classifications. Posts_per_page=5: indicates that several articles are called. Caller_get_posts=1: Excludes top articles. Orderby=new: indicates the latest order.

Also note that after using the query_posts() function to query, we will add wp_reset_query() after the loop statement to end the query.

2、 Add random articles to the wordpress template sidebar.

Add the following code to the sidebar.php file in the public sidebar of the wordpress website:

<div class="right_new"><h3>Random Articles</h3><ul><? Php $arr=array (cat=>119,//Only articles with a classification ID of 119 are called. posts_per_page=>5, caller_get_posts=>1, orderby=>rand//rand here means random calls.); query_posts($arr); // The parameter is an array. while (have_posts()) : the_post(); echo < li>< a href=".get_the_permalink()." title=".get_the_title().">; echo get_the_title().</ a></li>; endwhile; wp_reset_query(); ?></ ul></div>

In the latest article, the parameter of query_posts() function is a string. Like other wordpress functions, the parameter of query_posts() can also be an array. Therefore, we use array parameters in calling code in random articles.

3、 Add comments to the wordpress template sidebar.

The so-called hot review article is to sort the article list according to the number of comments of wordpress articles. In the sidebar.php sidebar file of the wordpress website, add the following code:

<div class="right_new"><h3>Hot reviews</h3><ul><? Php $arr=array (posts_per_page=>5, caller_get_posts=>1, orderby=>comment_count//Sort by the number of comments on the article); query_posts($arr); while (have_posts()) : the_post(); echo < li>< a href=".get_the_permalink()." title=".get_the_title().">; echo get_the_title().</ a></li>; endwhile; wp_reset_query(); ?></ ul></div>

Through the above three steps, we have added the calling code of "the latest article, random article, hot review article" to the sidebar of the wordpress website template. Is it very simple. Here, the query_posts() query function and wordpress circular function statement are mainly used. In the following chapters, we will introduce how to add "hot articles and hot tags".

If you have something you don't understand or want to know, you can leave a message for me in the comments below. At the same time, welcome to [follow] me.

This article is written by: Chief Editor Published on Software Development of Little Turkey , please indicate the source for reprinting: //hongchengtech.cn/blog/1339.html
Kuke_WP editor
author

Related recommendations

1 year ago (2024-02-20)

What are the main contents of wms system in warehouse management

Original title: What does the wms system mainly embody in warehouse management? What does the wms system mainly embody in warehouse management? Warehouse management has standardized and intelligent process oriented management. A good warehouse management mechanism can improve the efficiency of warehouse managers, relieve their pressure, and complete efficient and accurate work. 1. Warehouse management is accompanied by the progress of the times
1 year ago (2024-02-18)

How to implement the mptt comment function of CMS content management system in Django?, Django management page

During the daily development of content related Web systems in the directory, whether it is Blog or CMS, if you need to add links to interact with users, you must need the comment function. Next, you can implement the comment reply function in Django based on Python's MPTT framework. Note: Because the user comment function will involve a
three hundred and ninety-four
zero
1 year ago (2024-02-18)

Best CMS content management system in 2022, good novel in 2021

Looking for the best CMS software to build your website? At a high level, CMS or content management systems can help you create functional websites without having to use code to build every page from scratch. However, different CMS software has different advantages and disadvantages, so you need to choose the tool that best suits your specific needs and budget. To help, we accept
four hundred and six
zero
1 year ago (2024-02-18)

Shenzhen promotes the access of 5G base station energy storage system to the virtual power plant management center in the city. Does the Shenzhen 5g government subsidize the flow package charge

Xinhua News Agency, Shenzhen, December 14 (Reporter Wang Feng) At the 2022 Carbon Peak Carbon Neutralization Forum and Shenzhen International Low Carbon City Forum held here in Shenzhen, Shenzhen Virtual Power Plant Management Center signed a cooperation agreement on virtual power plant construction with China Tower, China Telecom, China Mobile, China Unicom, Huawei Digital Energy and other units on the 13th, which will jointly promote the city's 5G base station energy storage system
three hundred and forty-three
zero
1 year ago (2024-02-18)

Common website cms content management system recommendation, common website cms content management software

CMS is the abbreviation of "Content Management System", which means "Content Management System" in Chinese. These systems have developed common website functions and provided them to users for download, greatly improving the efficiency of website construction. The most common functions of CMS are column management, article management, product management, picture management
three hundred and twenty-eight
zero

comment

0 people have participated in the review

Scan code to add WeChat

contact us

WeChat: Kuzhuti
Online consultation: