Advertisement
  1. Code
  2. Cheat Sheets

Hoja de trucos de WordPress: El fragmento de código de bucle

Scroll to top
Read Time: 2 min

Spanish (Español) translation by Manuel (you can also view the original English article)

Dada la popularidad de nuestra antigua hoja de trucos de WordPress, hemos decidido lanzar un nuevo lote de estas guías rápidas de bolsillo que ustedes, chicos y chicas, pueden descargar, guardar en sus teléfonos para una referencia rápida, o incluso imprimir y mantener junto a tu escritorio mientras trabajan en la personalización de WordPress para hacer tu voluntad. La hoja de trucos de hoy: El fragmento de código del bucle.

El bucle es fácilmente una de las piezas más poderosas y cruciales de WordPress para entender, y aunque hay mucho ya escrito sobre él, también es bueno tener una referencia rápida para el bucle en el caso de que usted necesita un repaso rápido.


El fragmento de bucle corto

Esta es la versión rápida y sucia del bucle. Sin adornos, sin estilo adicional, contenido, categorías, etiquetas, ni nada por el estilo.

1
<?php if (have_posts()) : ?>
2
	<?php while (have_posts()) : the_post(); ?>
3
		<?php the_content('Read the rest of this entry »'); ?>
4
	<?php endwhile; ?>
5
	<?php else : ?>
6
	//Something that happens when a post isn’t found.
7
<?php endif; ?>

El fragmento de bucle largo

Este ejemplo es un poco más elaborado. Incluye muchas de las etiquetas principales de WP que probablemente utilizarás en un bucle básico de WordPress. Esto significa que este puede ser un poco más útil como punto de partida... pero de ninguna manera debe ser el límite de lo que puedes hacer con el bucle:

1
<?php if (have_posts()) : ?>
2
	<?php while (have_posts()) : the_post(); ?>
3
	<div class="post" id="post-<?php the_ID(); ?>">
4
		<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to

5
		<?php the_title(); ?>"><?php the_title(); ?></a></h2>
6
		<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
7
		
8
		<div class="entry">
9
			<?php the_content('Read the rest of this entry »'); ?>
10
		</div>
11
		
12
		<p class="postmetadata">Posted in <?php the_category(', ') ?> <strong>|</strong>
13
			<?php edit_post_link('Edit','','<strong>|</strong>'); ?>
14
			<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments

15
		»'); ?>
16
		</p>
17
	</div>
18
	<?php endwhile; ?>
19
	<div class="navigation">
20
		<div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
21
		<div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
22
	</div>
23
<?php else : ?>
24
	<h2 class="center">Not Found</h2>
25
	<p class="center">Sorry, but you are looking for something that isn't here.</p>
26
	<?php include (TEMPLATEPATH . "/searchform.php"); ?>
27
<?php endif; ?>

No te pierdas...

No te pierdas nuestra guía para principiantes sobre The_Loop(), o nuestra guía avanzada sobre las consultas de WordPress, que se ajusta al contenido de esta hoja de trucos.

Ah, y haremos todo lo posible por mantener estas fichas actualizadas a medida que WordPress vaya creciendo en nuevas versiones, pero si detectas un error (o simplemente quieres enviarnos algo de cariño), háznoslo saber en los comentarios.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.