Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mnestorov/wp-acf-snippets

A compilation of WordPress ACF plugin snippets and functions for developers who wants to create their own themes.
https://github.com/mnestorov/wp-acf-snippets

acf advanced-custom-fields snippets wordpress wordpress-snippets wp wp-snippets

Last synced: 3 months ago
JSON representation

A compilation of WordPress ACF plugin snippets and functions for developers who wants to create their own themes.

Awesome Lists containing this project

README

        

Laravel Logo

# WordPress - ACF Snippets

![Licence](https://img.shields.io/badge/Unlicense-red)

## Overview

A compilation of WordPress ACF plugin snippets and functions for developers who wants to create their own themes.

**Note:** Please be careful and make backups!

## Basic Fields

- [Display A Field](#display-a-field)
- [Retrieving A Field As A Variable](#retrieving-a-field-as-a-variable)
- [Get A Field By Name](#get-a-field-by-name)
- [Get And Format A Date Field](#get-and-format-a-date-field)
- [Field Conditional Also Used for True or False Fields](#field-conditional-also-used-for-true-or-false-fields)
- [Get A Field By Name Within Repeater (Flexible)](#get-a-field-by-name-within-repeater-flexible)
- [Using Conditional Statements](#using-conditional-statements)

## Color Picker Field

- [Display a color picker field value](#display-a-color-picker-field-value)

## Image Fields

- [Image Field With A Return Value Of "Image URL"](#image-field-with-a-return-value-of-image-url)
- [Image Field With A Return Value Of "Image ID"](#image-field-with-a-return-value-of-image-id)
- [Image Field With A Return Value Of "Image Object"](#image-field-with-a-return-value-of-image-object)

## Gallery Field

- [Loop through a gallery field and display images](#loop-through-a-gallery-field-and-display-images)

## File Fields

- [File Field With A Return Value Of "File URL"](#file-field-with-a-return-value-of-file-url)
- [File Field With A Return Value Of "File ID"](#file-field-with-a-return-value-of-file-id)
- [File Field With A Return Value Of "File Object"](#file-field-with-a-return-value-of-file-object)
- [Flexible Content Basic Field Returns 1 Row Deep](#flexible-content-basic-field-returns-1-row-deep)
- [Flexible Content Nested Field Returns](#flexible-content-nested-field-returns)
- [Flexible Content Basic Field Returns 1 Row Deep](#flexible-content-basic-field-returns-1-row-deep)

## Embed Field

- [Display an embed field](#display-an-embed-field)

## Relationship Field

- [Get A Relationship Field And Loop Over All Returned Posts](#get-a-relationship-field-and-loop-over-all-returned-posts)

## Location Fields

- [Get The Street Address From A Location Field](#get-the-street-address-from-a-location-field)
- [Get A Location Field And Convert It To A Static Google Map](#get-a-location-field-and-convert-it-to-a-static-google-map)
- [Get A Location Field And Convert It To An Interactive Google Map](#get-a-location-field-and-convert-it-to-an-interactive-google-map)

## Gravity Form Field

- [Display A Gravity Form](#display-a-gravity-form)

## Repeater Field

- [Get And Loop Over A Repeater Field](#get-and-loop-over-a-repeater-field)
- [Get and loop over a repeater field with subfields](#get-and-loop-over-a-repeater-field-with-subfields)
- [Loop Over A Repeater Filed And Seperate Results Into Rows](#loop-over-a-repeater-filed-and-seperate-results-into-rows)

## Taxonomy Fields

- [Get taxonomy field as a list of terms](#get-taxonomy-field-as-a-list-of-terms)
- [Get taxonomy field as a comma-separated list of terms](#get-taxonomy-field-as-a-comma-separated-list-of-terms)

## Queries

- [Query A Post Type On A Field Value And Loop Over Posts](#query-a-post-type-on-a-field-value-and-loop-over-posts)

## WYSIWYG Editor Field

- [Display the content of a WYSIWYG editor field](#display-the-content-of-a-WYSIWYG-editor-field)
- [Get and format a date field with a custom format](#get-and-format-a-date-field-with-a-custom-format)

## Misc

- var_dump The Field Contents Wrapped In 'pre' tags

---

## Basic Fields

### Display A Field

```php


```

### Retrieving A Field As A Variable

```php

```

### Get A Field By Name

```php
/**
* Get A Field By Name
*/

```

### Get And Format A Date Field

```php
/**
* Get And Format A Date Field
*/

format('d-m-Y'); ?>

```

### Field Conditional Also Used for True or False Fields

```php
/**
* Field Conditional / Also Used for True/False Fields
*/

// some code

```

### Get A Field By Name Within Repeater (Flexible)

```php
/**
* Get A Field By Name Within Repeater/Flexible
*/

```

### Using Conditional Statements

```php
/**
* Using Conditional Statements
* get_field returns false if (value == “” || value == null || value == false)
*/
' . get_field('field_name') . '';
}
?>
```

## Color Picker Field

### Display a color picker field value

```php
/**
* Display a color picker field value
*/
$color = get_field('color_picker_field_name');
if ($color) {
echo '

Some content
';
}
```

## Image Fields

### Image Field With A Return Value Of "Image URL"

```php
/**
* Image Field With A Return Value Of "Image URL"
*/

<?php the_field(''); ?>

```

## Image Field With A Return Value Of "Image ID"

```php
/**
* Image Field With A Return Value Of "Image ID"
*/

```

## Image Field With A Return Value Of "Image Object"

```php
/**
* Image Field With A Return Value Of "Image Object"
*/


<?php echo $image['alt']; ?>


<?php echo $image['alt']; ?>

```

## Gallery Field

### Loop through a gallery field and display images

```php
/**
* Loop through a gallery field and display images
*/
$images = get_field('gallery_field_name');
if ($images) {
echo '

';
}
```

## File Fields

### File Field With A Return Value Of "File URL"

```php
/**
* File Field With A Return Value Of "File URL"
*/

Download File

```

### File Field With A Return Value Of "File ID"

```php
/**
* File Field With A Return Value Of "File ID"
*/

```

### File Field With A Return Value Of "File Object"

```php
/**
* File Field With A Return Value Of "File Object"
*/

```

### Flexible Content Basic Field Returns 1 Row Deep

```php
/**
* Flexible Content Basic Field Returns 1 Row Deep
*/






```

### Flexible Content Nested Field Returns

```php
/**
* Flexible Content Nested Field Returns
*/






  • <?php echo $image['alt']; ?>





```

### Flexible Content Basic Field Returns 1 Row Deep

```php
/**
* Flexible Content Basic Field Returns 1 Row Deep
*/






```

## Embed Field

### Display an embed field

```php
/**
* Display an embed field (e.g. video or audio)
*/
$embed = get_field('embed_field_name');
if ($embed) {
echo $embed;
}
```

## Relationship Field

### Get A Relationship Field And Loop Over All Returned Posts

```php
/**
* Get A Relationship Field And Loop Over All Returned Posts
*/







```

## Location Fields

### Get The Street Address From A Location Field

```php
/**
* Get The Street Address From A Location Field
*/

```

### Get A Location Field And Convert It To A Static Google Map

```php
/**
* Get A Location Field And Convert It To A Static Google Map
*/

```

### Get A Location Field And Convert It To An Interactive Google Map

```php
/**
* Get a location field and convert it to an interactive Google Map.
* Also adds a marker to the location. The CSS is used to prevent rendering
* issues with map controls caused by most responsive CSS grids.
*/



google.maps.event.addDomListener(window, 'load', function() {
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 16,
center: new google.maps.LatLng(<?php echo $coordinates; ?>),
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
});

new google.maps.Marker({
position: new google.maps.LatLng(<?php echo $coordinates; ?>),
map: map
});
});


#map-canvas img {
max-width: inherit;
}

```

## Gravity Form Field

### Display A Gravity Form

```php
/**
* Display a Gravity form
* The parameters for gravity_form() are outlined in the Gravity Forms documentation
*/

```

## Repeater Field

### Get And Loop Over A Repeater Field

```php
/**
* Get And Loop Over A Repeater Field
*/

```

### Get and loop over a repeater field with subfields

```php
/**
* Get and loop over a repeater field with subfields
*/
if (have_rows('repeater_field_name')) {
while (have_rows('repeater_field_name')) {
the_row();
$subfield_1 = get_sub_field('subfield_1');
$subfield_2 = get_sub_field('subfield_2');
// Output or use subfields as needed
}
}
```

### Loop Over A Repeater Filed And Seperate Results Into Rows

```php
/**
* Loop Over A Рepeater Filed And Seperate Results Into Rows.
* The second tabstop is the row length.
*/




$5



```

## Taxonomy Fields

### Get taxonomy field as a list of terms

```php
/**
* Get taxonomy field as a list of terms
*/
$taxonomy_terms = get_field('taxonomy_field_name');
if ($taxonomy_terms) {
echo '

';
}
```

### Get taxonomy field as a comma-separated list of terms

```php
/**
* Get taxonomy field as a comma-separated list of terms
*/
$taxonomy_terms = get_field('taxonomy_field_name');
if ($taxonomy_terms) {
$term_names = array();
foreach ($taxonomy_terms as $term) {
$term_names[] = $term->name;
}
echo implode(', ', $term_names);
}
```

## Queries

### Query A Post Type On A Field Value And Loop Over Posts

```php
/**
* Query A Post Type On A Field Value And Loop Over Posts
*/
10,
'post_type' => 'post',
'meta_key' => 'field_name',
'meta_value' => 'field_value'
);

$query = new WP_Query( $args );

?>

have_posts() ) : ?>


    have_posts() ) : $query->the_post(); ?>




```

## WYSIWYG Editor Field

### Display the content of a WYSIWYG editor field

```php
/**
* Display the content of a WYSIWYG editor field
*/
$wysiwyg_content = get_field('wysiwyg_field_name');
if ($wysiwyg_content) {
echo $wysiwyg_content;
}
```

### Get and format a date field with a custom format

```php
/**
* Get and format a date field with a custom format
*/
$date_field = get_field('date_field_name', false, false);
if ($date_field) {
$date = new DateTime($date_field);
echo $date->format('F j, Y');
}
```

## Misc

### var_dump The Field Contents Wrapped In 'pre' tags

```php




```

---

## License

This repository is unlicense[d], so feel free to fork.