Can be found in Leeds’ source files. The
Import data: in the query standard loop, use the following lines to import data from the database: The load summary a1c19f0ba93ceb4826444f77f7397053 is in our template, which is located in each template file (such as page.php and single.php). After checking whether there is a post about the current content type, while (have_posts(): the_post(); Repeat each post while (have_posts(): the_post(); This is a standard query (or default query) used in the template file to find database content suitable for what word press is currently viewing. Therefore, if you are on a single post or page, use the ID of that post or page to import a single item from the database, and if you are on an archive page, use categories, post types, labels, and taxonomies. To import all posts in the archive, the term, date, or author id.wordpress will automatically process these posts. You do not need to specify what to display in the query. The
However, sometimes you may want to execute a custom query to get additional content that is not related to the page you are currently viewing. For example, you want to print a list of the latest posts in a single post, or a list of posts in the same category, after the content of the post. This is possible. To do this, create a custom query using the wp_query class. We will learn more about this soon. If you use wp_query to encode a custom query, the query looks slightly different. The load summary abbffc09c15bf5a8a2ad0280d67b2422 notes that there are several additional codes here. The
The $args variable contains an array of arguments that define the type of post we are looking for. It may include parameters, such as post type, category, publishing date, number of posts, import order, etc. If in the if ($query->have\u posts() {}) statement, and before running each post, the title or post in the post list contains other content, such as the open UL element. From the list. Then close the post after it runs. In addition, in the if{} statement, while ($query->have_posts(): $query->the_post(); Use the loop $query->the\u post(), which performs very similar to the standard loop; It runs the loop the same way, but based on the defined $query variable instead of the default query. If you loop through each post, the tags and annotations used to output the content are exactly the same as the standard loop. The
You can also modify the main query without creating a new query. To do this, use the pre\u get\u Posts hook that runs when WordPress runs the query. Instead of adding it to the relevant template file, you create a function (in the function file or plug-in of the topic) that modifies the query in a given set of cases specified using the condition tag. Later, we will show you how to do this. Display content: after defining circular query, execute circular query on each imported post. You can use static text and various template tags to display the annotations you want to add to elements, IDS, and classes and the content imported from the database in this ring. This includes: The
The_title() – the_permalink() – the URL of a single page of the post or page. If you manually add the male element around the title, the recommended image, and \/ or, use it in read more links. The_exercise
Let’s look at the most common ways to modify this query or create a new one. Pre\u get\u posts the main query modifies pre\u get\u posts, including pre\u get\u posts. Add custom post types to queries that are not normally included, such as default blog pages, category or tag archives, or search results. Removes a specific category or taxonomy term from a query or home page for a specific post type. Modify the number of Posts returned by the default query. In fact, you can change most of the contents of the main query by pre_get\u posts. Let’s look at an example. In part 4 of this series, you will remember to add category classifications to the pages of your site. Added pages and assigned the category \
Use the alink(), get_the_title() and the_extract() functions to output them respectively. Among them, the_permalink() and so on only run on the main loop, or only run on the loop defined by wp_query, so they are not available. Get_the_exercise() uses the_exercise() instead of echo get_the_exercise() because the post ID cannot be used as a parameter. Close the foreach loop and rerun it for each subsequent post. Save the file now. All functions include: If you visit the load summary abbfc09c15bf5a8a2ad0280d67b2422 non default blog page, the latest post will be displayed after the content. The current style is not good, so some white space will be added in the style sheet of the theme to further reduce the space. You can find styles in the source files of the series. The current list of my recent posts is as follows: If you like, you can also use more methods to add style sheets to the plug-in of specific styles (as learned earlier in this series, add them to the queue correctly), and add style sheets to the style sheets of themes. Use wp\u query class to create advanced custom queries. So far, we have modified the default query using pre\u get\u posts, and only use get\u posts() to query posts. But how do you run queries based on other? Well, you can. The wp_query class allows you to query all the content you need, including post type, classification terms, publication date, author, and metadata. In Codex, you can find detailed descriptions of parameters and useful examples of how to use wp_query main parameters in this series. We will show you how to use wp_query to query the latest items and add them to the sidebar. Then try it! Note: this only works if you have added the project article type you did in the previous part of this series. If you haven’t done so, check the source code of the plug-in that added it. First, create another plug-in. This code is the sidebar of the subject. Instead of adding to the PHP file, we will connect to the wpmu_aft\u sidbar hook created in the topic. If the theme’s sidebar is not hooked, it must be added. Place the theme in a folder instead of your own file, and place a subfolder of styles in that folder style. Add CSS file. Put the style sheet in the queue as in part 3 of this series. This will not be repeated because it has been learned. However, if you encounter a block, please check the source file of this part in the series. As a result, the plug-in has a plug-in file that contains folders with style sheets and has the ability to queue annotative text and style sheets. If you copied the previous plug-in, you must specify a unique name for all projects, including plug-in files, folders, and style sheet queuing. Add the following to the plug-in file: Load summary abbffc09c15bf5a8a2ad0280d67b2422, which is an empty function connected to the relevant action hook. Now let’s start populating the function. First, add arguments to the query. Load summary abbffc09c15bf5a8a2ad0280d67b2422 now run the query and add a check on the post. After loading the summary abbffc09c15bf5a8a2ad0280d67b2422, if there are posts in brackets, add a loop to the output content. The load summary abbffc09c15bf5a8a2ad0280d67b2422 adds the rewind_posts() function at the end of the corresponding loop. After you run a query using wp\u query, you must always add the query before you can reset word press yourself and target the current page
You can return to the default query operation. Save the file. The following are all the functions required: Load summary abbffc09c15bf5a8a2ad0280d67b2422 now when viewing the site, the latest items will be displayed on the sidebar. The side bar shows, but it can be seen more clearly. To be more visible in the plug-in’s style sheet, we will add some additional styles. My code is as follows: Load summary abbffc09c15bf5a8a2ad0280d67b2422 you can specify styles as needed. Will look much more beautiful than myself! My final version is: This is how to use wp_query to get the most recent posts of a given post type. For example, you can: On the service archive page, remove the service from the query to avoid sidebar duplication. Make sure that different items are displayed each time the screen is refreshed. Use different classifications for the recommended items, and only show the items. For example, if you run queries only on pages or default blog pages, use the criteria tab. There are many possibilities. Please try several! Queries and loops are the elements that make WordPress tick. If there is no loop, the site will only have blank pages without content. As you learned here, WordPress will pass the query on all pages, then run a loop to output the posts imported from the query. The default query is the query that WordPress runs by default. Identify the item you are currently viewing and query the database accordingly. However, you can also create your own queries. You can use pre\u get\u posts to directly write the default query, or you can use the get\u pages() or get\u posts() function or the wp\u query class. Custom queries allow you to control what is displayed on your site, not just the default content displayed on a specific page. It is often used for various purposes, such as adding a list of other posts behind the content or in the sidebar, and displaying posts by category terms rather than by date order. Create your own custom query and use it! The next part of this series will cover metadata. Word press uses various types of metadata to store posts, comments, and other data for authors. You will learn how to operate, display on the site, and encode custom metadata boxes so that users can add metadata through the administration screen. See you next time! Is this tutorial helpful? Why do you want to learn WordPress development? What else do you want to know? Please tell us the following comments. Labels: development intermediate