📜 ⬆️ ⬇️

Perversions with WordPress Themes for Newbies and More (Part 2) - query_posts

Thank you comrade. curlybrace for having opened eyes to existing rules and regulations in the Habré posting. We will delve into, for now, the second part of the article on WordPress and themes.

Query Posts


Did you want to determine which messages should be displayed on the page and when? There is nothing easier, because there is a miracle function query_posts, which determines which entries will fall into the issue. The function works as a kind of filter that selects posts by the specified criteria. Now everything will become clearer with examples, and then I will simply list most of the existing parameters, after which everything will be limited to your imagination.

List of recent entries


You probably know about the existence of a standard widget that performs this function, but what if you want to display a list of records in some other place?
  <? php query_posts ('showposts = 5');  ?>
 <ul>
   <? php while (have_posts ()): the_post ();  ?>
       <li> <a href="<?php the_permalink () ?> "> <? php the_title ();  ?> </a> </ li>
   <? php endwhile;?>
 </ ul> 

As you can see from the code, we passed the parameter showposts equal to 5 to the function query_posts. Even without being a special expert in English, it is clear that 5 posts will be shown. They are sorted by default by date of publication - from the last to the first. the_permalink () - gives us a link to the post, and the_title () - the title.

N-posts from a specific category


Just a bit more complicated the task - we will remove the last 5 posts from the category with ID 2.
  <? php query_posts ('cat = 2 & showposts = 5');  ?>
 <ul>
   <? php while (have_posts ()): the_post ();  ?>
     <li> <a href="<?php the_permalink () ?> "> <? php the_title ();  ?> </a> </ li>
   <? php endwhile;?>
 </ ul> 

Everything is simpler - only 6 characters, and what effect, what scale :-) I think there is no explanation required, so let's move on to
')

Exclude entries from the output


Suppose there is a certain category (for example with ID = 3), whose posts do not want to be displayed on the main one, for this purpose we remove the symbol from the output with a wave of the miracle of the "-" symbol (minus).
  <? php query_posts ('cat = -3');  ?>
 <? php while (have_posts ()): the_post ();  ?>
   // here is standard output
 <? php endwhile;?> 


Expand horizons or list of available options


I think you appreciate the beauty of this small but powerful query_posts function, and although you can always study it more deeply in the code, I will allow myself to list the parameters that may be useful to you:
cat and category_name — select a rubric by ID or by name, how to exclude a rubric — see above.
Hint : if you need to transfer several rubrics, then you do not need to write cat = 1 & cat = 2 several times, it is enough to list rubrics separated by commas cat = 1,2 . Incidentally, this technique applies to any parameter that can take several values.
author and author_name are posts of a certain author, by ID ( author = 3 ) and name ( author_name = Tapac ).
p and name - select posts by id ( p = 5 ) or short name ( name = this_post_slug ).
page_id and pagename are the same, only for pages.
showposts - how many of the filtered posts / pages to show when issuing.
Temporary ( hour , minute , second , day , monthnum , year ) - posts for a specified period.
paged - the parameter allows you to show those posts that are normally available when clicking on the links “Previous page”, i.e. paged = 2 will show posts, as if we were rewinding 2 pages in the past (when displaying 10 posts per page, we would get records from 21 to 30 in the output).
posts_per_page - how many posts per page. Well grouped with the previous parameter.
order is the sorting order by date, accepts ASK values ​​- from old to new, or DESC - from new to old (it is by default).
offset - the so-called indent Skips (shifts) a certain number of entries.

Display subcategories


At the end of today's post I want to share a trick that can be useful when using WordPress as a CMS.
I will start from far away, someone vzldd (in info - Dmitry) knocked on me in icq with a request to help with a site on films, or rather he wanted to place 2 columns in the right sidebar - in one list of movie categories, and in the second - just headings. I advised him to start two large headings and scatter existing in them as a child. Then he suggested inserting the following code into the template:
  <ul>
 <? php wp_list_categories ('child_of = 12345');  ?>
 </ ul> 

where 12345 is the parent heading ID (i.e. in our Download example)
But when outputting in this way, a list heading with the name of the parent heading was issued before the list, and I wanted to point it out separately with my hands. Digging the code further.
  <ul>
 <? php wp_list_categories ('child_of = 12345 & title_li =');  ?>
 </ ul> 

In theory, we should get a list, but without a title, but in fact we get an empty list with the inscription "No rubrics". Strange? Yes, not the word, but if you read a little more in the code about the syntax and parameters wp_list_categories, then we find such a line
"If the parameter (child_of) is used, the hide_empty parameter is set to false."
i.e. The hide_empty parameter (which allows or denies showing rubrics if there are no posts in them) should automatically switch to the mode — show everything, but alas, this “switch” for some reason only happens when there are no other parameters besides child_of . And here is the final version of the script that displays all the child categories for our chosen one:
  <ul>
 <? php wp_list_categories ('hide_empty = 0 & child_of = 12345 & title_li =');  ?>
 </ ul> 



Original article “Perversions with WordPress Themes for Newbies and Not Only (Part 2) - query_posts” .

Source: https://habr.com/ru/post/15311/


All Articles