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.