Create interesting random with custom fields“ Favorites“ Create list

Custom fields are an uncommon aspect of word press. But with a little imagination and a little custom code, you can do anything interesting of any kind. This post will show how to combine the custom fields (metadata publishing in particular cases) with the wp_query class to print the list of common items on the website and their related information. The list is random, at least the default list obtained using the get_post\u meta() function. As shown below: What you need to imitate the post, you need: The
WordPress testing or development installation code editor post type registration, custom field operation and some knowledge of using wp_query. This post is quite advanced. If there is a problem, you can pause and save time in the WordPress development college course. My intermediate course provides the basis for all the code used in this post. Note: the code accompanying this guide is available for GitHub. Then break it off! In order to generate information about what we like to do, we perform some tasks. The
Register a custom post type named \
Let’s start by customizing the post type. You must create a new plug-in before registering a custom post type. If you know the method but are not sure, check the plug-in authoring guide. Now, add the code to register the custom post type in the plug-in. The load summary d86649fa7c6a6845bff9ddc8bfaf 7466 \
Now, let’s add the corresponding metabox. Add a metabox to the post edit screen now you need to add a metabox to the plug-in so that users can enter information about this particular favorite. It will contain two fields: advantages and disadvantages. First, you need to generate the code for the metabox. Load the summary d86649fa7c6a6845bf9ddc8bfaf7466, and then fill in the callback function. The function that loads the summary d86649fa7c6a6845bf9ddc8bfaf7466 and then stores all the input data: there is a lot of code for loading the summary d86649fa7c6a6845bf9ddc8bfaf7466, but it is not very different from the code used to create the meta box. If you need a foundation to create a meta box, please check the posts on the courses and topics of the intermediate WordPress development college. The
Now, if you open the edit my custom post type screen, a meta box will be displayed, but the content edit box will not be displayed when registering the post type because it does not contain. The next thing to do is to add the best and worst to the things you like. Added some things I like on my website. The generated query is now an interesting part. You must run a query to randomly get three items you like from the database. W
Ordpress provides an option to make it simple by using the RAND value to use the orderby argument to the query. The
The following are the codes for defining arguments, setting queries, and starting loops. The load summary d86649fa7c6a6845bf9ddc8bfaf7466 will now run the standard loop and store the relevant data in the variable array instead of outputting the data of each post at once. There are three:$ Use currentpost++ to create a variable named $currentpost, which starts with a value of 0, but has one for each ring$ Favorite[$currentpost] = get_the_title(); Use to add the title of each post to the array $favorite[$currentpost]=get_the_title(); Use get_post\u meta() to add two custom fields to the array, respectively. The codes are as follows: The
The load summary d86649fa7c6a6845bf9ddc8bfaf 7466 is now populated with the variable array, so some data can be output. Note that the ring has been closed and the query has been reset. Since it is already stored in the array, it is not necessary to output this data in the loop. This will all move inside the section element, showing part of the text and each value in the three posts. The final step in loading the summary d86649fa7c6a6845bf9ddc8bfaf7466 is to run the function in the topic. You can add the wpmu\u output\u favorite() function to the theme template file of the theme, or connect it when there are tasks attached in the theme. Because my demo website uses the free blog way theme, connect my function blog way before primary in the blog\u for\u primary hook provided at the top of the page to add content. The
The load summary d86649fa7c6a6845bf9ddc8bfaf7466 also places the code in the condition tag (inside the function) so that it can only be printed on the home page of my website. Therefore, the loading summary d86649fa7c6a6845bff9ddc8bfaf7466 provides the function of outputting the complete content: Load summary d86649fa7c6a6845bff9ddc8bfaf7466 now when visiting my demo site, text containing randomly generated information about what I like will be displayed at the top of the page. If you refresh the page, the following changes will be made: The
The combination of user-defined fields and user-defined queries is powerful. As this example shows, you can gain great flexibility by using custom fields while customizing queries. In this case, I use it to create a paragraph that contains information about my favorite pages that refresh randomly when they refresh. You can apply this technique to other uses of custom fields to display post metadata in the desired way. Then, by changing the arguments of the query, you can change it from a random list to a more targeted list. You can also use meta\u query as one of the query parameters to query the posts with specific values in the custom fields. The
Have you ever used custom fields and custom queries to output data in a unique way? Please tell me what you think! Tags: custom fields custom queries

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *