Skip to content

Instantly share code, notes, and snippets.

@donaldallen
Last active April 16, 2017 04:02
Show Gist options
  • Save donaldallen/f828af52bed64f7d3fe81b4d2f49eb0a to your computer and use it in GitHub Desktop.
Save donaldallen/f828af52bed64f7d3fe81b4d2f49eb0a to your computer and use it in GitHub Desktop.
<?php
function acf_json_save_point ($path) {
return __DIR__.'/data/acf';
}
function acf_json_load_point ($path) {
return [__DIR__.'/data/acf'];
}
function acf_load_template_field_choices($field)
{
$field['choices'] = [];
$include_paths = [
get_template_directory() . '/templates',
];
$templates = [];
foreach ($include_paths as $include_path) {
foreach (glob($include_path . '/*.php') as $template) {
if (!in_array($template, $templates)) {
$templates[] = [
'path' => $template,
// We get the name of the template by reading the File Header.
'name' => get_file_data($template, ['name' => 'Template Name'])['name'],
];
}
}
}
foreach ($templates as $template) {
$path = pathinfo($template['path'])['filename'];
$name = $template['name'];
$field['choices'][$path] = $name;
}
return $field;
}
add_filter('acf/settings/save_json', 'acf_json_save_point');
add_filter('acf/settings/load_json', 'acf_json_load_point');
add_filter('acf/load_field/name=template', 'acf_load_template_field_choices');
<?php
/**
* ACF Partials
*
* When you define a new content block in ACF, you'll need to create a partial file called partial-*.php
* within your theme's partials/ folder. The * being the slug of the content block. It must be exactly the same,
* otherwise it will not load.
*
* @param null $post_id
*/
function templatePartials($post_id = null)
{
// Create an array to hold the content blocks. This keeps them in order.
$results = [];
// Can optionally pass a post ID. If not set, get the one within the loop.
if (!$post_id) {
$post_id = get_the_ID();
}
// Check to see if this post has any Flexible Content layouts set.
// The Flexible Content field should be named 'layout'
if (have_rows('layout', $post_id)) {
while (have_rows('layout', $post_id)) {
the_row();
// I usually create a Layout called Template Picker with a Select field called 'Template'.
// I populate this select field with files that are located inside my theme's templates/ folder.
// Sometimes we'll build templates that don't need to be configured at all, like a recent posts carousel.
// This is totally optional, but lets you know you can do different things with different layouts.
if (get_row_layout() === 'template_picker') {
$file_name = basename(get_sub_field('template'), '.php');
$template = substr($file_name, strpos($file_name, '-') + 1);
ob_start();
get_template_part('templates/template', $template);
$results[] = ob_get_clean();
} else {
ob_start();
get_template_part('partials/partial', get_row_layout());
$results[] = ob_get_clean();
}
}
}
foreach ($results as $result) {
echo $result;
}
}
themes/
|── my-theme/
│ ├── includes/
│ | ├── data/
│ | | └── acf/ [bunch of .json files in here]
│ | └── acf.php
│ ├── partials/
│ | ├── partial-example.php
│ | └── partial-another-example.php
│ ├── templates/
│ | ├── template-example.php
│ | └── template-another-example.php
│ └── functions.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment