Posted by & filed under Dev Workflow. Edit: 26 July 2014

Step 3: Expand template with custom data

After creating basic layout of page templates, we would need to expand them with some more parts to include other dynamic content such as template tag and other custom fields...

We have to know about Loop for this purpose.

 Copyright: Yoast

WP Loop

From the previous post, you had known about minimal code for template pages.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

					<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="">


							<div class="page-header"><h1 class="page-title" itemprop="headline"><?php the_title(); ?></h1></div>

						</header> <!-- end article header -->

						<section class="post_content clearfix" itemprop="articleBody">
							<?php the_content(); ?>

						</section> <!-- end article section -->


							<?php the_tags('<p class="tags"><span class="tags-title">' . __("Tags","bonestheme") . ':</span> ', ', ', '</p>'); ?>

						</footer> <!-- end article footer -->

					</article> <!-- end article -->

					<?php comments_template('',true); ?>

					<?php endwhile; ?>		

					<?php else : ?>

					<article id="post-not-found">
					    	<h1><?php _e("Not Found", "bonestheme"); ?></h1>
					    <section class="post_content">
					    	<p><?php _e("Sorry, but the requested resource was not found on this site.", "bonestheme"); ?></p>

					<?php endif; ?>

This part is called loop.

Within a loop, all data of a post will be called, by default including title, content... We can choose which to be called and how to be called.

Default template tags are the_title, the_content, the_tags.../ All can be referred from Codex.

Other custom fields need calling with other functions, depending on how you created them.

Custom fields

There are many ways to make custom fields, some are native, some are library and some are by plugins. If using library we can try a search from github for custom fields and meta boxes. Otherwise we can use native function to create fields. Advantages of library are they easier to maintain and shorter.

Meta boxes are groups of fields that exist within admin. Once on frontend, we call custom fields of them.



Comments are closed.