Last active
March 26, 2021 13:42
-
-
Save kisabelle/8465593 to your computer and use it in GitHub Desktop.
Advanced Custom Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*----------------------------------------------*/ | |
/* Image Field | |
/*----------------------------------------------*/ | |
/* Return value: Attachment ID */ | |
<?php if(get_field('logo')): ?> | |
<?php | |
$attachment_id = get_field('logo'); | |
$size = "vendor-logo"; // (thumbnail, medium, large, full or custom size) | |
echo wp_get_attachment_image( $attachment_id, $size ); | |
?> | |
<?php endif; ?> | |
/* Add A Class to the Img tag */ | |
<?php echo wp_get_attachment_image( $attachment_id, $size, false, array( 'class' => 'my-class' ) ); ?> | |
/* Get Image Attributes */ | |
<?php if(get_field('logo')): ?> | |
<?php | |
$attachment_id = get_field('logo'); | |
$size = 'large'; | |
$image_attributes = wp_get_attachment_image_src( $attachment_id, $size ); | |
// [0] => url | |
// [1] => width | |
// [2] => height | |
// [3] => boolean: true if $url is a resized image, false if it isn't or no img avail. | |
?> | |
<?php endif; ?> | |
/*----------------------------------------------*/ | |
/* Repeater Field | |
/*----------------------------------------------*/ | |
<?php | |
// check if the repeater field has rows of data | |
if( have_rows('repeater_field_name') ): | |
// loop through the rows of data | |
while ( have_rows('repeater_field_name') ) : the_row(); | |
// display a sub field value | |
the_sub_field('sub_field_name'); | |
endwhile; | |
else : | |
// no rows found | |
endif; | |
?> | |
/* Repeater Field Alternate Syntax/Markup */ | |
<?php if( have_rows('repeater_field_name') ): ?> | |
<?php while ( have_rows('repeater_field_name') ) : the_row(); ?> | |
<?php the_sub_field('sub_field_name'); ?> | |
<?php endwhile; ?> | |
<?php else : ?> | |
<?php endif; ?> | |
/*----------------------------------------------*/ | |
/* Checkbox Field | |
/*----------------------------------------------*/ | |
/* | |
* Conditional statement (Checkbox rvalue is an array) | |
*/ | |
if( in_array( 'red', get_field('field_name') ) ) | |
{ | |
//... | |
} | |
/* | |
* Query posts for a checkbox value. | |
* This method uses the meta_query LIKE to match the string "red" to the database value a:2:{i:0;s:3:"red";i:1;s:4:"blue";} (serialized array) | |
* The above value suggests that the user selected "red" and "blue" from the checkbox choices | |
*/ | |
$posts = get_posts(array( | |
'meta_query' => array( | |
array( | |
'key' => 'field_name', // name of custom field | |
'value' => '"red"', // matches exaclty "red", not just red. This prevents a match for "acquired" | |
'compare' => 'LIKE' | |
) | |
) | |
)); | |
/*----------------------------------------------*/ | |
/* Flexible Content Field | |
/*----------------------------------------------*/ | |
<?php | |
// check if the flexible content field has rows of data | |
if( have_rows('flexible_content_field_name') ): | |
// loop through the rows of data | |
while ( have_rows('flexible_content_field_name') ) : the_row(); | |
if( get_row_layout() == 'paragraph' ): | |
the_sub_field('text'); | |
elseif( get_row_layout() == 'download' ): | |
$file = get_sub_field('file'); | |
endif; | |
endwhile; | |
else : | |
// no layouts found | |
endif; | |
/*----------------------------------------------*/ | |
/* Page Link | |
/*----------------------------------------------*/?> | |
<a href="<?php the_field('page_link'); ?>">Read this!</a> | |
<?php | |
/*----------------------------------------------*/ | |
/* User Fields | |
/*----------------------------------------------*/ | |
// The $post_id needed is a string containing “user” + the user ID in this format: “user_$UserID” | |
$variable = get_field('field_name', 'user_2'); | |
// do something with $variable | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment