{"id":428,"date":"2016-04-26T09:24:34","date_gmt":"2016-04-26T15:24:34","guid":{"rendered":"http:\/\/develop.ibox.mx\/?p=428"},"modified":"2016-08-02T08:26:52","modified_gmt":"2016-08-02T13:26:52","slug":"el-loop-de-wordpress-en-accion","status":"publish","type":"post","link":"https:\/\/webirix.com\/en\/el-loop-de-wordpress-en-accion\/","title":{"rendered":"The WordPress Loop in action"},"content":{"rendered":"<p>Well the Loop, it is simply a recursive call that is responsible for going through the number of posts that we have in our database and shows them according to the criteria that we tell it, its functionality is complex but its use is easy on the nose.<br \/>\n<!--more--><\/p>\n<p><strong>The code<\/strong>.<\/p>\n<p>I&#039;m going to try to compress the code as much as possible to make the post as small as possible since then I get giant posts that no one wants to read.<\/p>\n<pre class=\"lang:php decode:true\"><!--?php get_header(); if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile; endif; get_sidebar(); get_footer(); ?-->\r\n<\/pre>\n<p>In this code we see the famous Loop in bold, it really is 4 lines, but they have a lot of strength. Let&#039;s gut the code a bit more.<\/p>\n<p>First we can see two functions that give the Loop a lifetime.<\/p>\n<pre class=\"lang:php decode:true\"><!--?php if (have_posts()) : ?-->\r\n<!--?php while (have_posts()) : the_post(); ?-->\r\n<\/pre>\n<p>These lines (for those who know something about the language of <a title=\"php\" href=\"http:\/\/www.php.net\/\">PHP programming<\/a>, they will see that this is bullshit, that they go to another blog to read interesting things) they are in charge of checking that there are posts and if there are posts we go through them. The function <strong><code>have_posts ()<\/code>,<\/strong> it is responsible for returning the posts that we are filtering when going through the index, a dark and sinister process that surely more than one would not be prepared for it (me among them).<\/p>\n<p><strong><code>have_posts ()<\/code><\/strong>, is responsible for differentiating the posts that are in <strong>draft <\/strong>and those who are in<strong>publish.<\/strong> It is also responsible for differentiating if the posts are included between the dates that we are looking for, categories or other criteria that we may wish to search.<\/p>\n<p>With the <strong>while<\/strong> we make it go through them while it finds posts, the process of the objects that it creates to carry out this tour really seem very complicated to explain the operation of the Loop and it is useless to put it into operation, the magic of <a title=\"OOP\" href=\"http:\/\/es.wikipedia.org\/wiki\/POO\">OOP<\/a>.<\/p>\n<p>With <strong><code>the_post ()<\/code>, <\/strong>we load in memory all data of the current post. And after him <a title=\"Post_tags\" href=\"http:\/\/codex.wordpress.org\/Template_Tags#Post_tags\">all calls<\/a> that we need to show the post data will refer to the current post, this <a title=\"Green\" href=\"http:\/\/show.plukk.com\/2006\/01\/28\/have-a-green\/\">allows us to assemble curious constructions<\/a> for our templates.<\/p>\n<p>A simple idea without any originality would be to mount the one we have in the<a title=\"kubrick\" href=\"http:\/\/binarybonsai.com\/kubrick\/\"> Kubrick<\/a> : D, but it helps us to show how it works.<\/p>\n<pre><\/pre>\n<h2 id=\"post-&lt;?php the_ID(); ?&gt;\"><\/h2>\n<p><small><!--?php the_time('F jS, Y') ?--> by <!--?php the_author() ?--><\/small><\/p>\n<p>In this way we assemble our first Loop with a more crappy appearance than you have ever seen, but less gives a stone ... <img decoding=\"async\" class=\"wp-smiley\" src=\"http:\/\/www.anieto2k.com\/wp-includes\/images\/smilies\/icon_biggrin.gif\" alt=\":D\" \/><\/p>\n<p>We are going to finish our Loop, for this we will have to close the previous tags.<\/p>\n<pre><!--?php endwhile; ?-->\r\n<\/pre>\n<p>With this tag we will close the while that ran through the posts that we were going to show, now we only need a beautiful, distinctive and attention-grabbing system to be able to move through the different pages with their respective Loops.<\/p>\n<pre><\/pre>\n<div class=\"navigation\">\n<div class=\"alignleft\"><\/div>\n<p>&nbsp;<\/p>\n<div class=\"alignright\"><\/div>\n<\/div>\n<p>Now with our super nice Loop armed we have to think that it may be the case that there are no posts to show, then we will have to contemplate this possibility with the following code.<\/p>\n<pre><!--?php else : ?-->\r\n<\/pre>\n<h2 class=\"center\">It has not been found<\/h2>\n<p>&nbsp;<\/p>\n<p class=\"center\">\n<p>Now if we already have our Loop well armed, we only need to close it (mainly because if we do not do it, we will not see more than a php error), and for this:<\/p>\n<pre><!--?php endif; ?-->\r\n\r\n<\/pre>\n<p>If you have followed the steps you will now have a file similar to this (perhaps in Spanish):<\/p>\n<pre><!--?php get_header(); ?-->\r\n<\/pre>\n<div id=\"content\" class=\"narrowcolumn\"><!--?php if (have_posts()) : ?--> <!--?php while (have_posts()) : the_post(); ?--><\/p>\n<div class=\"post\">\n<h2 id=\"post-&lt;?php the_ID(); ?&gt;\"><\/h2>\n<p><small><!--?php the_time('F jS, Y') ?--> by <!--?php the_author() ?--><\/small><\/p>\n<\/div>\n<p><!--?php endwhile; ?--><\/p>\n<div class=\"navigation\">\n<div class=\"alignleft\"><\/div>\n<p>&nbsp;<\/p>\n<div class=\"alignright\"><\/div>\n<\/div>\n<p><!--?php else : ?--><\/p>\n<h2 class=\"center\">Not found<\/h2>\n<p>&nbsp;<\/p>\n<p class=\"center\">\n<\/div>\n<p>Well, you&#039;re done with this, you can start assembling your own Loop and formatting all the knowledge that I tried to convey to you. You can also take a look at the<a title=\"Loop\" href=\"http:\/\/codex.wordpress.org\/The_Loop_in_Action\"> WordPress codex<\/a> And if you understand something (not because of the language but because it is a bit confusing) you will be able to find very curious things.<\/p>","protected":false},"excerpt":{"rendered":"<p>Well the Loop, it is simply a recursive call that is responsible for going through the number of posts that we have in our database and shows them according to the criteria that we tell it, its functionality is complex but its use is easy on the nose.<\/p>","protected":false},"author":21,"featured_media":37817,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2013,2166,2023],"tags":[2188,2192,2187],"class_list":["post-428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-disenoweb","category-desarrollo","category-wordpress","tag-loop","tag-nucleo","tag-wordpress"],"jetpack_featured_media_url":"https:\/\/webirix.com\/wp-content\/uploads\/2016\/04\/train_loop.jpg","_links":{"self":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/comments?post=428"}],"version-history":[{"count":1,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/428\/revisions"}],"predecessor-version":[{"id":37818,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/428\/revisions\/37818"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/media\/37817"}],"wp:attachment":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/media?parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/categories?post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/tags?post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}