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' => 'activity',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'activity-type',
            'field' => 'slug',
            'terms' => 'campaign'
        )));
$Query = new WP_Query($args); if($Query -> have_posts()):
 while($Query -> have_posts()): $Query -> the_post(); ?>
   
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail();
 }  else { ?>
<img src="<?php bloginfo('stylesheet_directory'); ?>/img/resource-person.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
</a>
      
<h3><?php the_title(); ?></h3>
<?php  the_excerpt();?>
  
<?php endwhile; else: "No Campaigns 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 ?>
 
// ACF file download
   <?php if (get_field('document') ) { ?> 
    <?php
$file = get_field('document');
$attachment_id  = get_field('document');
$attachment_meta = wp_prepare_attachment_for_js($attachment_id);

if( $file ):
    $url = wp_get_attachment_url( $file ); ?>
<div class="et_pb_button_module_wrapper et_pb_button_0_tb_body_wrapper  et_pb_module ">
				<a class="et_pb_button et_pb_custom_button_icon et_pb_button_0_tb_body et_pb_bg_layout_light" href="<?php echo esc_html($url); ?>" data-icon="&#xe092;">Download File</a>
			</div>
			
	<p>File Size:	<?php	echo $attachment_meta['filesizeHumanReadable']; ?></p>
<?php endif; ?>
<?php } ?>

// ACF copyright selector

Field Name: 
licence

Choices:
All Rights Reserved
Attribution
Attribution-ShareAlike
Attribution-NoDerivs
Attribution-NonCommercial
Attribution-NonCommercial-ShareAlike
Attribution-NonCommercial-NoDerivs
Public Domain

<p>
<?php if( get_field('licence') == 'All Rights Reserved' ): ?>
© All Rights Reserved
<?php elseif( get_field('licence') == 'Attribution' ): ?>	
<a href="https://creativecommons.org/licenses/by/4.0/" target="_blank">
<img  src="https://i.creativecommons.org/l/by/4.0/88x31.png" alt="image" width="88" height="31" > Attribution CC BY</a>

<?php elseif( get_field('licence') == 'Attribution-ShareAlike' ): ?>	
<img  src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png" alt="image" width="88" height="31"><a href="https://creativecommons.org/licenses/by-sa/4.0" target="_blank">
Attribution ShareAlike CC BY-SA</a>

<?php elseif( get_field('licence') == 'Attribution-NoDerivs' ): ?>	
<img src="https://i.creativecommons.org/l/by-nd/4.0/88x31.png" alt="image" width="88" height="31"><a href="https://creativecommons.org/licenses/by-nd/4.0" target="_blank">
Attribution-NoDerivs CC BY-ND</a>

<?php elseif( get_field('licence') == 'Attribution-NonCommercial' ): ?>	
<img src="https://i.creativecommons.org/l/by-nc/4.0/88x31.png" alt="image" width="88" height="31"><a href="https://creativecommons.org/licenses/by-nc/4.0" target="_blank">
Attribution-NonCommercial CC BY-NC</a>

<?php elseif( get_field('licence') == 'Attribution-NonCommercial-ShareAlike' ): ?>	
<a href="https://creativecommons.org/licenses/by-nc-sa/4.0" target="_blank">
<img src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" alt="image" width="88" height="31"> Attribution-NonCommercial-ShareAlike CC BY-NC-SA</a>

<?php elseif( get_field('licence') == 'Attribution-NonCommercial-NoDerivs' ): ?>	
<a href="https://creativecommons.org/licenses/by-nc-nd/4.0" target="_blank">
<img src="https://i.creativecommons.org/l/by-nc-nd/4.0/88x31.png" alt="image" width="88" height="31"> Attribution-NonCommercial-NoDerivs CC BY-NC-ND</a>

<?php elseif( get_field('licence') == 'Public Domain' ): ?>	
<a href="https://creativecommons.org/publicdomain/zero/1.0/" target="_blank">
<img src="https://licensebuttons.net/l/zero/1.0/88x31.png" alt="image" width="88" height="31"> Public Domain - No rights reserved</a>
<?php endif; ?>
</p>



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

 

Feed to copy into email newsletters


<table style="border-collapse: collapse;" cellpadding="12"><tbody> <?php $args = array(

'posts_per_page' => '3',





'post_type' => 'post'
);
 
$Query = new WP_Query($args);
if($Query -> have_posts()):
 
    while($Query -> have_posts()):
        $Query -> the_post();
        ?>
         
    
<tr>
 <td>

<a href="<?php the_permalink(); ?>">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<img src="<?php echo $url ?>" width="150" height="150"/>
 </a>
</td>
 <td>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <?php  the_excerpt();?>
</td>
  </tr>

  
        <?php    endwhile; endif;
wp_reset_postdata();
?>
</tbody>
</table>

Last updated: November 23rd, 2023

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