In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 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 achieve the top of archived pages such as WordPress classification and tags. 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 achieve the top of archived pages such as WordPress classification and tags?
This paper gives an example of how to achieve the top of archived pages such as WordPress classification and tags. Share it with you for your reference. The specific analysis is as follows:
By default, the top article in wordpress is only the home page. If we want archived pages such as categories / tags to also be able to top the article, we need secondary development.
Now refer to the wp-includes/query.php home page top code, a little modification, so that the classification page, tab page, author page and date page and other archived pages can also be like the home page in the top of its range of articles, put the following code on the current topic under the functions.php on it.
The code is as follows:
Add_filter ('the_posts',' putStickyOnTop'); function putStickyOnTop ($posts) {if (is_home () | |! is_main_query () | |! is_archive () return $posts; global $wp_query; $sticky_posts = get_option ('sticky_posts'); if ($wp_query- > query_vars [' paged'] $sticky_posts)) Foreach ($stickies1 as $sticky_post1) {/ / determine whether the current category page if ($wp_query- > is_category = = 1 &! has_category ($wp_query- > query_vars ['cat'], $sticky_post1- > ID)) {/ / remove articles $offset1 = array_search ($sticky_post1- > ID, $sticky_posts); unset ($sticky_posts [$offset1]) } if ($wp_query- > is_tag = = 1 & & has_tag ($wp_query- > query_vars ['tag'], $sticky_post1- > ID)) {/ / remove articles that are not part of this tag $offset1 = array_search ($sticky_post1- > ID, $sticky_posts); unset ($sticky_posts [$offset1]) } if ($wp_query- > is_year = = 1 & & date_i18n ('Yee, strtotime ($sticky_post1- > post_date))! = $wp_query- > query [' m']) {/ / remove articles $offset1 = array_search ($sticky_post1- > ID, $sticky_posts) that do not belong to this year; unset ($sticky_posts [$offset1]) } if ($wp_query- > is_month = = 1 & & date_i18n ('Ym', strtotime ($sticky_post1- > post_date))! = $wp_query- > query [' m']) {/ / remove articles that do not belong to this month $offset1 = array_search ($sticky_post1- > ID, $sticky_posts); unset ($sticky_posts [$offset1]) } if ($wp_query- > is_day = = 1 & & date_i18n ('Ymd', strtotime ($sticky_post1- > post_date))! = $wp_query- > query [' m']) {/ / remove articles that do not belong to this date $offset1 = array_search ($sticky_post1- > ID, $sticky_posts); unset ($sticky_posts [$offset1]) } if ($wp_query- > is_author = = 1 & & $sticky_post1- > post_author! = $wp_query- > query_vars ['author']) {/ / remove articles that do not belong to this author $offset1 = array_search ($sticky_post1- > ID, $sticky_posts); unset ($sticky_posts [$offset1]);} $num_posts = count ($posts); $sticky_offset = 0; / / Loop over posts and relocate stickies to the front. For ($I = 0; $I
< $num_posts; $i++ ) { if ( in_array($posts[$i]->ID, $sticky_posts) {$sticky_post = $posts [$I]; / / Remove sticky from current position array_splice ($posts, $I, 1); / / Move to front, after other stickies array_splice ($posts, $sticky_offset, 0, array ($sticky_post)); / / Increment the sticky offset. The next sticky will be placed at this offset. $sticky_offset++; / / Remove post from sticky posts array $offset = array_search ($sticky_post- > ID, $sticky_posts); unset ($sticky_posts [$offset]);}} / / If any posts have been excluded specifically, Ignore those that are sticky. If (! emptyempty ($sticky_posts) & &! emptyempty ($wp_query- > query_vars ['post__not_in'])) $sticky_posts = array_diff ($sticky_posts, $wp_query- > query_vars [' post__not_in']) / / Fetch sticky posts that weren't in the query results if (! emptyempty ($sticky_posts)) {$stickies = get_posts (array ('post__in' = > $sticky_posts,' post_type' = > $wp_query- > query_vars ['post_type'],' post_status' = > 'publish',' nopaging' = > true)); foreach ($stickies as $sticky_post) {array_splice ($posts, $sticky_offset, 0, array ($sticky_post)); $sticky_offset++ } return $posts;}
Code description:
1. If you want the archive page to show all the top articles, delete 11-43 lines of code
2. If you do not want to display the top article on a classified page, set the
The code is as follows:
If (/ / changed to: / / abc is the classification name if (is_category ('abc') | |
3. If you don't want a tab to display the top article, put the code on line 3
The code is as follows:
If (/ / changed to: / / abc is the tag name if (is_tag ('abc') | |
4. If you do not want an author's page to display the top article, set the
The code is as follows:
If (/ / changed to: / / abc is the author's nickname if (is_author ('abc') | |
5. The above code is only valid for the main loop. If you use WP_Query or query_posts to get the list of articles on the archive page, or if the top articles are displayed at the top of these lists, you can delete the following code in line 3 (Note: the number of articles displayed may not be the same as the one you set):
The code is as follows:
The code is as follows:
| |! is_main_query () |
Set-top style: if you want to add a style to the set-top article, add the following code to functions.php, and add a class named sticky to the set-top article, specific css code, and customize it:
The code is as follows:
Add_filter ('post_class',' addStickyClass', 10 sticky' 3); function addStickyClass ($classes, $class, $post_id) {if (is_sticky () & & is_category () & &! isset ($classes ['sticky'])) {$classes [] =' sticky';} return $classes } on the WordPress classification and tags and other archived pages to achieve the top of the method to share here, I hope the above content can be of some help to you, can 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.