Chapter 11: Working with Index Templates
11.2 - Customizing and styling index templates
In layouts/content-sidebar.css
.site-content {
padding-bottom: 4em;
background: #b2b2b2 url(../images/pattern.svg) repeat;
}
In content.php
<div class="index-box">
(...)
</div>
In layouts/content-sidebar.css
.index-box {
max-width: 780px;
max-width: 78rem;
padding: 1em 0 2em;
margin: 0 auto 2em;
background: #fff;
background: hsl(0, 0%, 100%);
}
.index-box blockquote.alignleft,
.index-box blockquote.alignright {
background: #fff;
background: hsl(0, 0%, 100%);
border: none;
}
@media screen and (min-width: 1820px) {
.index-box blockquote.alignright { padding-right: 2em; }
}
@media screen and (min-width: 1320px) {
.index-box blockquote.alignleft { padding-left: 2em; }
}
Completed content.php
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="index-box">
<header class="entry-header clear">
<?php
/* translators: used between list items, there is a space after the comma */
$category_list = get_the_category_list( __( ', ', 'my-simone' ) );
if ( my_simone_categorized_blog() ) {
echo '<div class="category-list">' . $category_list . '</div>';
}
?>
<h1 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
<?php if ( 'post' == get_post_type() ) : ?>
<div class="entry-meta">
<?php my_simone_posted_on(); ?>
<?php
if ( ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) {
echo '<span class="comments-link">';
comments_popup_link( __( 'Leave a comment', 'my-simone' ), __( '1 Comment', 'my-simone' ), __( '% Comments', 'my-simone' ) );
echo '</span>';
}
?>
<?php edit_post_link( __( ' | Edit', 'my-simone' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-meta -->
<?php endif; ?>
</header><!-- .entry-header -->
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'my-simone' ) ); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'my-simone' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<?php endif; ?>
<footer class="entry-footer">
</footer><!-- .entry-footer -->
</div><!-- .index-box -->
</article><!-- #post-## -->
In style.css
.posted-on:before {
content: ' on ';
}
.posted-on:after {
content: '.';
}
@media screen and (min-width: 1320px) {
.single .posted-on:before,
.single .posted-on:after {
content: '';
}
}
11.3 - Displaying excerpts or full content on index pages
In content.php
<div class="entry-content">
<?php the_excerpt(); ?>
</div><!-- .entry-content -->
11.4 - Adding a custom Read More link
In content.php
<footer class="entry-footer continue-reading">
<?php echo '<a href="' . get_permalink() . '" title="' . __('Continue Reading ', 'my-simone') . get_the_title() . '" rel="bookmark">Continue Reading<i class="fa fa-arrow-circle-o-right"></i></a>'; ?>
</footer><!-- .entry-footer -->
In style.css
/*--------------------------------------------------------------
Index and Archive
--------------------------------------------------------------*/
.continue-reading {
margin: 0 4rem;
font-weight: 900;
font-size: 18px;
font-size: 1.8rem;
}
.continue-reading i { margin-left: .5em; }
11.5 - Adding featured images
In content.php
<?php
if (has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . get_permalink() . '" title="' . __('Read ', 'my-simone') . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
?>
In style.css
.small-index-thumbnail {
margin-top: -1em;
margin-bottom: 1em;
}
11.6 - Creating custom pagination
In inc/template-tags.php
function my_simone_paging_nav() {
// Don't print empty markup if there's only one page.
if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $GLOBALS['wp_query']->max_num_pages,
'current' => $paged,
'mid_size' => 2,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => __( '← Previous', 'my-simone' ),
'next_text' => __( 'Next →', 'my-simone' ),
'type' => 'list',
) );
if ( $links ) :
?>
<nav class="navigation paging-navigation" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'my-simone' ); ?></h1>
<?php echo $links; ?>
</nav><!-- .navigation -->
<?php
endif;
}
In style.css
.paging-navigation ul {
margin: 0;
}
.paging-navigation li { display: inline; }
a.page-numbers,
span.page-numbers {
padding: .3em .7em;
color: #333;
color: hsl(0, 0%, 20%);
}
a:hover.page-numbers {
color: #000;
color: hsl(0, 0%, 0%);
}
.paging-navigation .current {
font-weight: bold;
color: #000;
color: hsl(0, 0%, 0%);
}
In layouts/content-sidebar.css
.paging-navigation {
padding-top: 1em;
padding-bottom: 1em;
margin-bottom: 2em;
list-style-type: none;
background: #fff;
}
11.7 - Highlighting Sticky Posts
In content.php
<?php
// Display a thumb tack in the top right hand corner if this post is sticky
if (is_sticky()) {
echo '<i class="fa fa-thumb-tack sticky-post"></i>';
}
?>
In style.css
.sticky-post {
position: absolute;
top: 1rem;
right: 3rem;
}
11.8 - Creating custom Post Format templates
In content-aside.php
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="index-box">
<div class="entry-content">
<?php the_content(); ?>
</div><!-- .entry-content -->
<footer class="entry-footer">
<?php my_simone_posted_on(); ?>
<?php edit_post_link( __( ' | Edit', 'my-simone' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-footer -->
</div>
</article><!-- #post-## -->
In style.css
/*--------------------------------------------------------------
10.2 Asides
--------------------------------------------------------------*/
.format-aside .entry-content {
margin-top: 1em;
}
11.9 - Creating a custom display for the most recent post
In content.php
<?php
if( $wp_query->current_post == 0 && !is_paged() && is_front_page() ) {
echo '<div class="entry-content">';
the_content( __( '', 'my-simone' ) );
echo '</div>';
echo '<footer class="entry-footer continue-reading">';
echo '<a href="' . get_permalink() . '" title="' . __('Read ', 'my-simone') . get_the_title() . '" rel="bookmark">Read the article<i class="fa fa-arrow-circle-o-right"></i></a>';
echo '</footer><!-- .entry-footer -->';
} else { ?>
<div class="entry-content">
<?php the_excerpt(); ?>
</div><!-- .entry-content -->
<footer class="entry-footer continue-reading">
<?php echo '<a href="' . get_permalink() . '" title="' . __('Continue Reading ', 'my-simone') . get_the_title() . '" rel="bookmark">Continue Reading<i class="fa fa-arrow-circle-o-right"></i></a>'; ?>
</footer><!-- .entry-footer -->
<?php } ?>
In content.php
<?php
if( $wp_query->current_post == 0 && !is_paged() && is_front_page() ) { // Custom template for the first post on the front page
if (has_post_thumbnail()) {
echo '<div class="front-index-thumbnail clear">';
echo '<div class="image-shifter">';
echo '<a href="' . get_permalink() . '" title="' . __('Read ', 'my-simone') . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('large-thumb');
echo '</a>';
echo '</div>';
echo '</div>';
}
echo '<div class="index-box';
if (has_post_thumbnail()) { echo ' has-thumbnail'; };
echo '">';
} else {
echo '<div class="index-box">';
if (has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . get_permalink() . '" title="' . __('Read ', 'my-simone') . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
}
?>
In style.css
.front-index-thumbnail {
margin-bottom: -3.2em;
}
@media screen and (max-width: 779px){
.front-index-thumbnail {
margin-bottom: 0;
}
}
.single-post-thumbnail img,
.front-index-thumbnail img {
display: block;
margin: 0 auto;
}
.has-thumbnail {
position: relative;
margin-top: -8em;
}
11.11 - Working with archive.php
In layouts/content-sidebar.css
.page-header,
.paging-navigation {
padding-top: 1em;
padding-bottom: 1em;
margin-bottom: 2em;
list-style-type: none;
background: #fff;
}
.page-header {
margin-top: 1em;
margin-bottom: 1em;
}
In style.css
.taxonomy-description {
padding-top: 2rem;
margin: 2rem 0;
font-family: 'Lato', sans-serif;
font-style: italic;
border-top: 1px solid hsl(0, 0%, 75%);
border-bottom: 1px solid hsl(0, 0%, 75%);
}
In archive.php
<?php
if ( is_category() ) :
printf( __( 'Posts in the ', 'my-simone' ) );
echo '<em>';
single_cat_title();
echo '</em> ' . __('category', 'my-simone') . ':';
elseif ( is_tag() ) :
printf( __( 'Posts with the ', 'my-simone' ) );
echo '<em>';
single_tag_title();
echo '</em> ' . __('tag', 'my-simone') . ':';
elseif ( is_author() ) :
printf( __( 'Author: %s', 'my-simone' ), '<span class="vcard">' . get_the_author() . '</span>' );
elseif ( is_day() ) :
printf( __( 'Posts from %s', 'my-simone' ), '<span>' . get_the_date() . '</span>' );
elseif ( is_month() ) :
printf( __( 'Posts from %s', 'my-simone' ), '<span>' . get_the_date( _x( 'F Y', 'monthly archives date format', 'my-simone' ) ) . '</span>' );
elseif ( is_year() ) :
printf( __( 'Posts from %s', 'my-simone' ), '<span>' . get_the_date( _x( 'Y', 'yearly archives date format', 'my-simone' ) ) . '</span>' );
elseif ( is_tax( 'post_format', 'post-format-aside' ) ) :
_e( 'Asides', 'my-simone' );
else :
_e( 'Archives', 'my-simone' );
endif;
?>
11.12 - Customizing the search results and 404 templates
In content-none.php
<?php
/**
* The template part for displaying a message that posts cannot be found.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package my-simone
*/
?>
<section class="<?php if ( is_404() ) { echo 'error-404'; } else { echo 'no-results'; } ?> not-found">
<div class="index-box">
<header class="entry-header">
<h1 class="entry-title">
<?php
if ( is_404() ) { _e( 'Page not available', 'my-simone' ); }
else if ( is_search() ) { printf( __( 'Nothing found for <em>', 'my-simone') . get_search_query() . '</em>' ); }
else { _e( 'Nothing Found', 'my-simone' );}
?>
</h1>
</header>
<div class="entry-content">
<?php if ( is_home() && current_user_can( 'publish_posts' ) ) : ?>
<p><?php printf( __( 'Ready to publish your first post? <a href="%1$s">Get started here</a>.', 'my-simone' ), esc_url( admin_url( 'post-new.php' ) ) ); ?></p>
<?php elseif ( is_404() ) : ?>
<p><?php _e( 'You seem to be lost. To find what you are looking for check out the most recent articles below or try a search:', 'my-simone' ); ?></p>
<?php get_search_form(); ?>
<?php elseif ( is_search() ) : ?>
<p><?php _e( 'Nothing matched your search terms. Check out the most recent articles below or try searching for something else:', 'my-simone' ); ?></p>
<?php get_search_form(); ?>
<?php else : ?>
<p><?php _e( 'It seems we can’t find what you’re looking for. Perhaps searching can help.', 'my-simone' ); ?></p>
<?php get_search_form(); ?>
<?php endif; ?>
</div><!-- .entry-content -->
</div><!-- .index-box -->
<?php
if ( is_404() || is_search() ) {
?>
<header class="page-header"><h1 class="page-title">Most recent posts:</h1></header>
<?php
// Get the 6 latest posts
$args = array(
'posts_per_page' => 6
);
$latest_posts_query = new WP_Query( $args );
// The Loop
if ( $latest_posts_query->have_posts() ) {
while ( $latest_posts_query->have_posts() ) {
$latest_posts_query->the_post();
// Get the standard index page content
get_template_part( 'content', get_post_format() );
}
}
/* Restore original Post Data */
wp_reset_postdata();
}
?>
</section><!-- .no-results -->
In 404.php
<?php get_template_part( 'content', 'none' ); ?>