Home >CMS Tutorial >WordPress >A brief analysis of how to pin articles to the top in WordPress? How to distinguish it from ordinary articles?

A brief analysis of how to pin articles to the top in WordPress? How to distinguish it from ordinary articles?

青灯夜游
青灯夜游forward
2023-03-13 20:18:144468browse

How to set the article to the top in WordPress? How to distinguish pinned articles? The following article will introduce to you several ways to distinguish pinned articles in WordPress. I hope it will be helpful to you!

A brief analysis of how to pin articles to the top in WordPress? How to distinguish it from ordinary articles?

Many blogs have their own pinned articles. By default, WP only ranks their pinned articles at the front. In addition to the position difference, it is different from other articles. There is no difference. So how can we tell them apart? I have several methods here:

1. How to set the article to the top in WordPress?

I believe many friends don’t know that WordPress itself has the function of pinning articles, and it is really a waste to look for plug-ins to implement the pinning function. I think it is necessary to explain how to pin articles. . Setting the article to the top in WordPress is very simple. Open the article editing page and click the Publish column in the right column to set it, as shown in the picture:

A brief analysis of how to pin articles to the top in WordPress? How to distinguish it from ordinary articles?

2. Add the words “sticky” to the title

In fact, the function we will use in this article is only one is_sticky(), this function is used to determine Whether this article is a pinned article. Pinned articles are generally only displayed on the home page, so now open index.php in your theme directory, search for: the_title(); and change it to:

the_title(); if( is_sticky() ) echo &#39;  <span style="color:red;">置顶</span>&#39;;

3. The pinned article does not display the article summary

Also open index.php, search: the_content or the_excerpt, give It adds a judgment so that the pinned article does not display the summary, such as:

if( !is_sticky() ) the_content(....);

or

if( !is_sticky() ) the_excerpt(....);

4. Pin it to the top Add a border or background to the article

This method does not need to determine whether it is a pinned article, we only need to add a CSS style to it. Open index.php in the theme directory and find the div where each article is located. General theme search: class="post" This div is the div where each article is located. We now add a class to this div, such as

Change to (class="post" in the following code can be removed):
<div <?php post_class(); ?> class="post">

If it is a pinned article, this div will be automatically added Add a class .sticky. Now we open style.css in the theme directory and add the class attribute:

.sticky {
   background: #ff0000;
   border: 1px solid #ccc;
}

In this way, the sticky article will have a background and border. Isn’t it ugly? ? You can modify this class attribute as needed to obtain the effect you are satisfied with.

Recommended learning: "WordPress Tutorial"

The above is the detailed content of A brief analysis of how to pin articles to the top in WordPress? How to distinguish it from ordinary articles?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:ludou.org. If there is any infringement, please contact admin@php.cn delete