Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of creating dynamic Top articles for wordpress

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to create a dynamic top article for wordpress. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

How to create a dynamic top article for wordpress?

The function of the top article is to attract the attention of readers for a long time, which can indicate the introduction of the site, copyright notice, exchange principles of links, etc. This article will introduce how to create a dynamic top article for wordpress, friends who need to know can refer to it.

"Sticky Posts" has been added since WordPress 2.7. The role of the top article is to attract the attention of readers for a long time, which can indicate the introduction of the site, the copyright notice, the exchange principles of links, and so on. But staying the same for too long can also cause an aversion to reading. This article will show you how to create a dynamic top article for wordpress.

From wordpress help forums's various discussions about multiple loops, it reminds me of a way to use loops twice. One is used to query and display only articles in a category. The other is used to query all articles in all categories. The first query is copied from the codex and is easy to understand:

The code is as follows:

$my_query = new WP_Query ('category_name=frontpage&showposts=1'); while ($my_query- > have_posts ()): $my_query- > the_post (); $do_not_duplicate = $post- > ID

What the above code means is that the variable my_query is assigned to query the results of all articles named frontpage and get only one article. The key point is in the last part, which assigns the variable do_not_duplicate to the ID value of the returned individual article. We need this value in the next step.

The following code is important and standard and can be found in many topics. It can get all the logs:

The code is as follows:

If (have_posts ()): while (have_posts ()): the_post (); / / do stuff like format each postendwhile

This code takes all the logs and displays them according to the format of the line / / do stuff (depending on the template). If you just write the code this way, it will happen that your selected article (top article) will be displayed twice (once in the top area and once in all other article areas). It's not good to show this, so the do_not_duplicate function is used here. Add to an if statement to prevent the formulation article from being displayed twice. Here is the code:

The code is as follows:

If (have_posts ()): while (have_posts ()): the_post (); if ($post- > ID = = $do_not_duplicate) continue;//do stuff like format each post endwhile

The second if statement in the code means to get all logs and break execution (continue statement) when the ID value of an article is found to be equal to the value of do_not_duplicate, otherwise all other articles are displayed according to the format of the line / / do stuff. Remember, the article represented by the value of the do_not_duplicate function has been shown.

That's it. What do you get when you put it all together? A dynamic top article! And the best part is that when the top article is replaced by a new one, the previous article will be displayed in the area of all the articles below (depending on how many articles you choose to display and how often they are published).

This is the end of the method of creating a dynamic top article for wordpress. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report