Below are a list of functions I find myself using day in and day out when building for WordPress. There is a mixture in there of stuff you can find all over the internet to a few tailored pieces of code I’ve used. I thought they may come in handy so I’m sharing them here, if you know a better way of doing something please let me know – I’m always trying to learn.
The plan is to keep this post alive by adding and amending it as I go along. I do have a central WP Cheat Sheet on my mac, but there are a few snippets below that aren’t on that list. It makes more sense to put it all here as I find new useful stuff.
Basic Loop
rewind_posts();
if (have_posts()) :
while (have_posts()) : the_post();
// Do something
endwhile;
endif;
Blog Pagination
<?php if ( $wp_query->max_num_pages > 1 ) : ?> <div class="navigation"> <div class="nav-previous"><?php next_posts_link( __( 'Older posts.' ) ); ?></div> <div class="nav-next"><?php previous_posts_link( __( 'Newer posts.' ) ); ?></div> </div> <?php endif; ?>
Detect Blog Pagination and show alternate content if no prev/next
<?php
if (get_previous_posts_link() || get_next_posts_link()) {
?>
<nav class="navigation posts-navigation" role="navigation">
<h2 class="screen-reader-text"><?php esc_html_e('Posts navigation', 'textdomain');
?></h2>
<div class="nav-links<?php if (!get_previous_posts_link()) {
echo " nav-links--first";
}
?>"><?php if (get_previous_posts_link()) {
?><div class="nav-previous"><?php previous_posts_link(balanceTags(__('<i class="fa fa-long-arrow-left"></i> <span>Older Posts</span>', 'textdomain')));
?></div><?php
}
?><?php if (get_next_posts_link()) {
?><div class="nav-next"><?php next_posts_link(balanceTags(__('<span>Newer Posts</span> <i class="fa fa-long-arrow-right"></i>', 'textdomain')));
?></div><?php
}
?></div>
</nav>
<?php
}
Post Pagination
<nav class="pagination">
<div class="newer-posts"><?php next_post_link( '%link', __('Next Post →' , 'textdomain') ); ?></div>
<div class="older-posts"><?php previous_post_link( '%link', __('← Previous Post' , 'textdomain') ); ?></div>
</nav>
Detect Post Pagination and show alternate content if no prev/next
<?php
if (get_previous_post_link() || get_next_post_link()) {
?>
<nav class="navigation post-navigation" role="navigation">
<h2 class="screen-reader-text"><?php esc_html_e('Post navigation', 'textdomain');
?></h2>
<div class="nav-links<?php if (!get_previous_post_link()) {
echo " nav-links--first";
}
?>"><?php if (get_previous_post_link()) {
?><div class="nav-previous"><?php previous_post_link('%link', balanceTags(__('<i class="fa fa-long-arrow-left"></i> <span>%title</span>', 'textdomain')));
?></div><?php
}
?><?php if (get_next_post_link()) {
?><div class="nav-next"><?php next_post_link('%link', balanceTags(__('<span>%title</span> <i class="fa fa-long-arrow-right"></i>', 'textdomain')));
?></div><?php
}
?></div>
</nav>
<?php
}
Get Thumbnail URL
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'default' ); ?>
Use that Thumbnail URL
<?php echo esc_url( $image[0] ); ?>
<div class="hero-image"<?php if ( has_post_thumbnail() ) { ?> style="background-image: url( <?php echo esc_url( $image[0] ); ?>);"<?php } ?>>
Enable Excerpt for pages
<?php
add_action( 'init', 'page_excerpts' );
function page_excerpts() {
add_post_type_support( 'page', 'excerpt' );
}
?>
Set up a loop based on Page ID
<?php
$the_query = new WP_Query( 'page_id=10' );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
// Do stuff in here
<? }
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
Get Custom Meta
<?php echo get_post_meta($post->ID, '_single_format_video', true); ?>
Make Search only return posts
<?php
if ( !is_admin() ) {
// Make search only return posts
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
}
add_filter('pre_get_posts','SearchFilter');
?>
Set default values for the upload media box
<?php
function image_attachment_link() {
update_option('image_default_link_type', 'none' );
}
add_action('after_setup_theme', 'image_attachment_link');
?>
Check if there is content
<?php if( get_the_content() ) {
// Do something
} ?>
Create a Custom post type
<?php
add_action( 'init', 'create_book_post_type' );
function create_book_post_type() {
register_post_type( 'customer-book',
array(
'labels' => array(
'name' => _x('Books', 'post type general name','textdomain'),
'singular_name' => _x('Book', 'post type singular name','textdomain'),
'add_new' => _x('Add New', 'portfolio item','textdomain'),
'add_new_item' => __('Add New Book','textdomain'),
'edit_item' => __('Edit Book','textdomain'),
'new_item' => __('New Book','textdomain'),
'view_item' => __('View Book','textdomain'),
'search_items' => __('Search Books','textdomain'),
'not_found' => __('Nothing found','textdomain'),
'not_found_in_trash' => __('Nothing found in Trash','textdomain'),
'parent_item_colon' => ''
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'book'),
'supports' => array('title','editor','author','thumbnail','category','custom-fields','comments'),
)
);
}
}
?>
Register Taxanomy
<?php
function book_taxonomies() {
register_taxonomy( 'book-categories', 'customer-book',
array(
'hierarchical' => true,
'label' => __( 'Book Categories' , 'textdomain' ),
'query_var' => true,
'rewrite' => true
)
);
}
add_action( 'init', 'book_taxonomies', 0 );
?>
Loop for a Custom Post Type
<?php
$main_query = new WP_Query( array ( 'post_type' => 'customer-book', 'posts_per_page' => '99' ) );
while ( $main_query->have_posts() ) : $main_query->the_post();
// Do something
endwhile;
?>
Setup a Custom Post Filter ready for isotope
<div class="filter">
<ul>
<li class="filter-last"> <a data-filter="*" href="#all" class="all active" title="<?php _e( 'Filter: All', 'textdomain' ) ?>"><?php _e( 'Filter: All', 'textdomain' ) ?></a></li>
<?php $terms = get_terms('portfoliocat');
foreach ( $terms as $term ) {
$catname = $term->name;
$caturl = $term->slug;
echo '<li>' . ' / <a data-filter=".'. $caturl .'" class="'. $caturl .'" href="#'. $caturl .'" title="'. $catname .'">'. $catname .'</a> </li>';
}
?>
</ul>
</div>
Add current term into class name ready for isotope
<?php
// Get terms for post
$terms = get_the_terms( $post->ID , 'portfolio-item' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
echo esc_html( $this_term = $term->slug) . ' ';
// Get rid of the other data stored in the object, since it's not needed
unset($term);
}
}
?>
Add a checkbox to search only by a custom post type
<input type="checkbox" class="search-type" name="type" value="custompostslug" />
Fancy Post pagination
This brings in background images from the previous posts so it can be used as per my Myth Theme.
<section class="post-navigation">
<?php
if( $prev_post = get_previous_post() ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $prev_post->ID ), 'desktop' );
?>
<a class="grid-item-link" href="<?php echo get_permalink($prev_post->ID); ?>" title="<?php echo esc_html( $prev_post->post_title ); ?>">
<aside <?php post_class('grid-item'); ?> <?php if ( has_post_thumbnail() ) { ?> style="background-image:url(<?php echo esc_url( $image[0] ); ?>);"<?php } ?>>
<div class="grid-item-wrap">
<div class="grid-item-inner">
<div class="grid-item-content">
<div class="grid-item-content-inner">
<ul class="meta top">
<li class="next-last">
<?php _e( 'Previous Post', 'meanthemes' ); ?>
</li>
</ul>
<h4 class="entry-title"><?php echo esc_html( $prev_post->post_title ); ?></h4>
</div>
</div>
</div>
</div>
</aside>
</a>
<?php
}
if( $next_post = get_next_post() ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $next_post->ID ), 'desktop' );
?>
<a class="grid-item-link" href="<?php echo get_permalink($next_post->ID); ?>" title="<?php echo esc_html( $next_post->post_title ); ?>">
<aside <?php post_class('grid-item'); ?> <?php if ( has_post_thumbnail() ) { ?> style="background-image:url(<?php echo esc_url( $image[0] ); ?>);"<?php } ?>>
<div class="grid-item-wrap">
<div class="grid-item-inner">
<div class="grid-item-content">
<div class="grid-item-content-inner">
<ul class="meta top">
<li class="next-last">
<?php _e( 'Next Post', 'meanthemes' ); ?>
</li>
</ul>
<h4 class="entry-title"><?php echo esc_html( $next_post->post_title ); ?></h4>
</div>
</div>
</div>
</div>
</aside>
</a>
<?php
}
?>
</section>
I’ll keep adding more and more to this list
Please tell me if anything doesn’t work or if there is a better way and I’ll amend the above.
