Template tags

//
// Display the terms that are associated with the post (or posttype)
for a specific taxonomy (taxonomy-name)<code>
<?php the_terms( $post->ID, 'taxonomy-name', '', ', ', '' ); ?>
 
// or
<?php echo get_the_term_list( $post->ID, 'taxonomy-name', '', '<br /> ', '' ); ?>
 
// Without hyperlink
<?php $terms = get_the_terms( $post->ID, 'taxonomy-name'); 
$term_list = array_pop($terms)->name ; foreach( $terms as $os ) 
{    $term_list .= ", " . $os->name ; } echo $term_list ; ?>
 
// Excluding a specific term
<?php $terms = get_the_terms( $post->ID, 'taxonomy-name' ); 
foreach ( $terms as $term  ) { if($term->term_id != 104) { 
// the term ID you want to exclude 
$term_IDs[] = $term->term_id; } } $terms = implode(',', $term_IDs); ?>
 
// Display current term on archives
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), 
get_query_var( 'taxonomy' ) ); echo $term->name; ?>

Conditionals

// Basic conditional with different options
 
<?php if ( in_category('saving')) { ?> 
   <strong>STUFF 1</strong> 
<?php } else if ( in_category('housing')) { ?> 
   <strong>STUFF 2</strong> 
<?php } else { ?> 
   <strong>STUFF 3</strong>
<?php } ?>
  
// ACF Conditional
<?php if (get_field('field_name') ) { ?>
<?php the_field('field_name'); ?>
<?php } else if (get_field('field_name2')) { ?>
   <strong>STUFF 2</strong> 
<?php } else { ?> 
    <strong>STUFF 3</strong>
<?php } ?>
 
// Taxonomy conditional - if archive is a specific term
<?php if ( is_tax('taxonomy-name','term-one' ) ) {?>
    <div class="test" style="color: red"><?php the_field('field_name'); ?></div>
<?php } elseif ( is_tax('taxonomy-name','term-two' ) ) {?>
    <div class="test" style="color: green"><?php the_field('field_name'); ?></div>
<?php } ?>
 
 
// Taxonomy conditional - if post has a specific term
<?php if ( has_term('term-name','taxonomy-name' ) ) {?>	
<strong>Online Event</strong>
<?php } ?>	

Loop queries

//
// Show all post-types that have a specific term
<?php query_posts( array( 'post_type' => 'post-type-name', 'taxonomy-name' => 'term-name' ) );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<strong>POSTS</strong> 
<?php endwhile; endif; wp_reset_query(); ?>
 
// Show all posts-types and order those posts by a specific term (alphabetical)
<?php $posts = query_posts('post_type=post-type-name&orderby=taxonomy-name&amp;order=asc&posts_per_page=-1'); 
if (have_posts()) : while (have_posts()) : the_post(); ?>
<strong>POSTS</strong> 
<?php endwhile; endif; ?>

General

// oEmbed with ACF
<?php echo apply_filters('the_content', get_field('field-name')); ?>
 
// URL for child theme directory
<?php echo get_stylesheet_directory_uri(); ?>
 
// Display a random block of code
<?php $Block_ID = rand(1,4); ?> 
<?php if ($Block_ID == 1) { ?> <strong>STUFF 1</strong> <?php } ?> 
<?php if ($Block_ID == 2) { ?> <strong>STUFF 2</strong> <?php } ?> 
<?php if ($Block_ID == 3) { ?> <strong>STUFF 3</strong> <?php } ?> 
<?php if ($Block_ID == 4) { ?> <strong>STUFF 4</strong> <?php } ?>
 
// Query with ACF
<?php $args = array( 'posts_per_page' => 1, 'post_type' => 'post', 
'tax_query' => array( array( 'taxonomy' => 'number', 'field' => 'slug', 
'terms' => array( get_field('field_name') ) ) ) ); query_posts( $args ); 
while ( have_posts() ): the_post(); // do stuff here ?>
 
 
/** * Remove buttons from editor */
add_action('admin_head','check_post_type_and_remove_media_buttons');
 
add_action('admin_head', 'hide_add_form_icon');
function hide_add_form_icon() {
    global $post_type;
    // edited July 3 2014 to fix PHP warnings
    if ((isset($_GET['post_type']) && $_GET['post_type'] == 'post') || ($post_type == 'post')) {
        echo "<style type='text/css'>a#add_gform, #mceu_35 {display:none;}</style>\n";
    }
}

Last updated: November 3rd, 2020

Creative Commons Licence
WordPress Code Snippets by actionskills.co is licensed under a Creative Commons Attribution 4.0 International License.
https://actionskills.co/resource/wordpress-snippets/.