In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to reorder WordPress articles through custom fields. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
The default order of articles in WordPress is based on the release date and is displayed in reverse chronological order. This means that it will display the latest articles at the top.
By adding a custom field for WordPress, you can change the order based on that custom field.
I will explain how to create custom fields and change the order of articles on the blog page in the following step-by-step guide.
Sort WordPress articles by custom field
Let's start by creating a custom field in the post and using the article ID to save the values and display them.
Create a custom field in the publish meta-box
We will create a numeric custom field in the publish meta box in the right sidebar of the edit article page. The field values will be saved in the post meta table, which we will use later to change the order of the articles.
For custom fields in the publish meta-box, see the following code.
Add_action ('post_submitbox_misc_actions',' add_publish_meta_custom_field'); function add_publish_meta_custom_field ($post_obj) {global $post; $post_type = 'post'; $value = get_post_meta ($post_obj- > ID,' post_order', true); $val = ($value)? $value: "0" If ($post_type==$post- > post_type) {echo'. 'Post Order'.}}
The above will create a custom field in the publish meta-box, and you will see the field shown in the following figure.
Publish custom fields in the meta-box
Save the value of the custom field
Now we must save the value of the Post Order field. So we use save_post to run the action hook before saving the article, and update_post_meta () to save the field value of the corresponding article.
The update_post_meta () operation saves the value in the wp_ postmetastate table along with the article ID.
Yes. See the code to save the value of the custom field.
Add_action ('save_post',' save_publish_meta_custom_field', 10, 3); function save_publish_meta_custom_field ($post_id, $post, $update) {$post_type = 'post'; if ($post_type! = $post- > post_type) {return;} if (wp_is_post_revision ($post_id)) {return } if (isset ($_ POST ['post_order'])) {update_post_meta ($post_id,' post_order', $_ POST ['post_order']);}}
The above code saves the custom field value in the database and displays it in the input number field.
Values saved in custom fields
Change the order of WordPress articles by custom field
Now we will change the default order of WordPress articles with our own order values. Before displaying the results, we will run the pre_get_posts action hook to modify the existing WP_Query article.
Pre_get_posts gives us some solutions to modify wp_query without writing a custom WP_Query.
Function pre_custom_post_order_sort ($query) {if (is_home () & & $query- > is_main_query ()) {$query- > set ('orderby',' meta_value'); $query- > set ('meta_key',' post_order'); $query- > set ('order',' ASC');} add_action ('pre_get_posts',' pre_custom_post_order_sort')
The above code example changes the order of WordPress articles on the blog page or home page.
In this article, we examined a major query and home page, and then set the query order using meta_key meta-values. It checks whether the article has custom field values and places them at the top based on the field value.
Sort WordPress articles by custom fields in the template file
You can also create your own custom WP_Query to display articles in order.
You must create a WordPress custom template page in which you can write your own queries.
See here for a step-by-step guide to how to create a custom page template in WordPress.
Yes. You now have a custom template file. Therefore, write the following code in the template.
$args = array ('post_type' = >' post', 'meta_key' = >' post_order', 'orderby' = >' meta_value', 'order' = >' ASC'); $query = new WP_query ($args); if ($query- > have_posts ()) {while ($query- > have_posts ()) {$query- > the_post () If (! empty (get_post_meta ($post- > ID, 'post_order', true)) {the_title ();} wp_reset_postdata ();}
The above code displays the article title order by custom field value and places the article at the top based on the custom field value.
You can also create an abbreviated code of the above code in the functions.php file and use it in gadgets, pages, or wherever you want to display the article.
okay. You have finished displaying the order of WordPress articles by custom field values.
On how to reorder WordPress articles through custom fields is shared here, I hope the above content can be of some help to you, 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.