WordPress Code Snippets

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; ?>

//List terms
<?php $args = array(
'taxonomy' => 'topic',
'orderby' => 'name',
'show_count' => false,
'pad_counts' => false,
'hierarchical' => true,
'title_li' => '',
); ?>
<ul><?php wp_list_categories( $args ); ?></ul>

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
$args = array(
    'post_type' => 'person',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order' => 'ASC',
    'tax_query' => array(

        array(
            'taxonomy' => 'person_cat',
            'field' => 'slug',
            'terms' => 'cof-fellowship-team'
        )

    )
);

$Query = new WP_Query($args);
if($Query -> have_posts()):

    while($Query -> have_posts()):
        $Query -> the_post();
        ?>
        
        

<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); 
 }  else { ?>
<img src="<?php bloginfo('stylesheet_directory'); ?>/img/resource-person.jpg" alt="<?php the_title(); ?>" />
<?php } ?>


<h3><?php the_title(); ?></h3>
		<?php  the_content();?>
 <?php edit_post_link(); ?>
 
 
        <?php
    endwhile;

else:
    "No People Found.";
endif;
wp_reset_postdata();
?>

ACF

// oEmbed with ACF
<?php echo apply_filters('the_content', get_field('field-name')); ?>
 
// 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 ?>
 

General

// 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 } ?>

 

Last updated: January 19th, 2023

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/.