The Loop Examples - 3ev/wordpress-core GitHub Wiki
Listed below are some examples of typical things you my want to do in The Loop using the Model Layer.
###Common post data examples
#####Fetching basic post information
<?php while (have_posts()): $p = tev_post_factory(); ?>
<h1><?php echo $p->getTitle(); ?></h1>
<?php echo $p->getContent(); ?>
<?php endwhile; ?>#####Creating a post list
<?php if (have_posts()): ?>
<ul>
<?php while (have_posts()): $p = tev_post_factory(); ?>
<li>
<h2>
<a href="<?php echo $p->getUrl(); ?>">
<?php echo $p->getTitle(); ?>
</a>
</h2>
<p><?php echo $p->getExcerpt(); ?></p>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>#####Fetching post author and categories
<?php while (have_posts()): $p = tev_post_factory(); ?>
<h1><?php echo $p->getTitle(); ?></h1>
<p>
<a href="<?php echo $p->getAuthor()->getPostsUrl(); ?>">
<?php echo $p->getAuthor()->getFirstName(); ?> <?php echo $p->getAuthor()->getLastName(); ?>
</a>
</p>
<ul>
<?php foreach ($p->getCategories() as $cat): ?>
<li>
<a href="<?php echo $cat->getUrl(); ?>">
<?php echo $cat->getName(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endwhile; ?>#####Rendering a post's Featured Image
<?php while (have_posts()): $p = tev_post_factory(); ?>
<h1><?php echo $p->getTitle(); ?></h1>
<img src="<?php echo $p->getFeaturedImageUrl('large'); ?>" title="<?php echo $p->getTitle(); ?>" />
<?php endwhile; ?>#####Rendering a post's Featured Image with more information
<?php while (have_posts()): $p = tev_post_factory(); ?>
<?php $image = $p->getFeaturedImage(); ?>
<h1><?php echo $p->getTitle(); ?></h1>
<figure>
<img src="<?php echo $image->getImageUrl(); ?>" title="<?php echo $image->getTitle(); ?>" alt="<?php echo $image->getAlt(); ?>" />
<figcaption>
<?php echo $image->getDescription(); ?>
</figcaption>
</figure>
<?php endwhile; ?>###Custom fields examples
####Basic fields
#####Fetching basic custom field data
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('mybasicfield'); ?></p>
<?php endwhile; ?>####Date fields
#####Echo a date field with the default format
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('mydatefield'); ?></p>
<?php endwhile; ?>#####Formatting a date field
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('mydatefield')->format('Y-m-d'); ?></p>
<?php endwhile; ?>#####Using a Carbon instance from a date field
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('mydatefield')->date()->addDays(7)->format('Y-m-d'); ?></p>
<?php endwhile; ?>####File fields
#####Fetching the URL of a file field
Note that the echo'ing the file field directly returns the URL.
<?php while (have_posts()): $p = tev_post_factory(); ?>
<a href="<?php echo $p->field('myfilefield'); ?>"><?php echo $p->field('myfilefield'); ?></a>
<?php endwhile; ?>#####Fetching the title and URL of a file field
<?php while (have_posts()): $p = tev_post_factory(); ?>
<a href="<?php echo $p->field('myfilefield')->url(); ?>"><?php echo $p->field('myfilefield')->title(); ?></a>
<?php endwhile; ?>####Flexible content fields
#####Iterating over a flexible content field and its layouts
<?php while (have_posts()): $p = tev_post_factory(); ?>
<?php foreach ($p->field('myflexcontent') as $flexContent): // $flexContent is an instance of Tev\Field\Util\LayoutFieldGroup ?>
<?php switch ($flexContent->layout()): ?>
<?php case 'layoutone': ?>
<?php echo $flexContent->field('layoutonetitle'); ?>
<?php break; ?>
<?php case 'layouttwo': ?>
<?php echo $flexContent->field('layouttwotitle'); ?>
<?php break; ?>
<?php endswitch; ?>
<?php endforeach; ?>
<?php endwhile; ?>####Google Map fields
#####Fetching the lat and lng as a comma separated string
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('mymapfield'); ?></p>
<?php endwhile; ?>#####Fetching the lat and lng separately
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('mymapfield')->lat(); ?></p>
<p><?php echo $p->field('mymapfield')->lng(); ?></p>
<?php endwhile; ?>####Image fields
#####Fetching the full image
Note that the echo'ing the image field directly returns the URL to the full sized image.
<?php while (have_posts()): $p = tev_post_factory(); ?>
<img src="<?php echo $p->field('myimagefield'); ?>" />
<?php endwhile; ?>#####Fetching the thumbnail image
<?php while (have_posts()): $p = tev_post_factory(); ?>
<img src="<?php echo $p->field('myimagefield')->thumbnailUrl(); ?>" />
<?php endwhile; ?>#####Fetching any sized image
<?php while (have_posts()): $p = tev_post_factory(); ?>
<img src="<?php echo $p->field('myimagefield')->sizeUrl('medium'); ?>" />
<?php endwhile; ?>####Post fields
#####Fetch the post title
This will return a single post title or comma-separated list of titles, depending if the field is singular or multiple.
<?php while (have_posts()): $p = tev_post_factory(); ?>
<h2><?php echo $p->field('mypostfield'); ?></h2>
<?php endwhile; ?>#####Fetch data for a single post
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('mypostfield')->val()->getAuthor()->getFirstName(); ?></p>
<?php endwhile; ?>#####Fetch data for multiple posts
<?php while (have_posts()): $p = tev_post_factory(); ?>
<?php foreach ($p->field('mypostfield')->val() as $subPost): ?>
<p><?php echo $subPost->getAuthor()->getFirstName(); ?></p>
<?php endforeach; ?>
<?php endwhile; ?>####Repeater fields
#####Iterate over a repeater field
<?php while (have_posts()): $p = tev_post_factory(); ?>
<?php foreach ($p->field('myrepeater') as $repeaterFields): // $repeaterFields is an instance of Tev\Field\Util\FieldGroup ?>
<h2><?php echo $repeaterFields->field('title'); ?></h2>
<p><?php echo $repeaterFields->field('content'); ?></p>
<?php endforeach; ?>
<?php endwhile; ?>####Select and checkbox fields
#####Fetch the selected option for a single select or checkbox
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('myselectfield'); ?></p>
<?php endwhile; ?>#####Fetch the selected options for a multi select or checkbox
This will be a comma separated list of values.
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('myselectfield'); ?></p>
<?php endwhile; ?>#####Fetch the selected options for a multi select or checkbox as an array
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('myselectfield')->val(); ?></p>
<?php endwhile; ?>#####Fetch all available options for a select or checkbox
<?php while (have_posts()): $p = tev_post_factory(); ?>
<ul>
<?php foreach ($p->field('myselectfield')->options() as $val => $name): ?>
<li><?php echo $val; ?>: <?php echo $name; ?></li>
<?php endforeach; ?>
</ul>
<?php endwhile; ?>####Taxonomy fields
#####Fetch the selected term (or terms)
<?php while (have_posts()): $p = tev_post_factory(); ?>
<?php $term = $p->field('mytaxfield'); ?>
<?php if (is_array($term)): ?>
<ul>
<?php foreach ($term as $t): ?>
<li><?php echo $t->getName(); ?></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p><?php echo $term->getName(); ?></p>
<?php endif; ?>
<?php endwhile; ??#####Fetch the selected term name(s)
This will be a comma separated list of term names if this is a multi field.
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('mytaxfield'); ?></p>
<?php endwhile; ??#####Fetch the taxonomy
<?php while (have_posts()): $p = tev_post_factory(); ?>
<p><?php echo $p->field('mytaxfield')->taxonomy()->getName(); ?></p>
<?php endwhile; ??