Skip to content

Instantly share code, notes, and snippets.

@sergiohidalgo
Forked from dstollie/acf.php
Last active September 11, 2017 14:27
Show Gist options
  • Save sergiohidalgo/b42db9a78065c23ed845c6584c869885 to your computer and use it in GitHub Desktop.
Save sergiohidalgo/b42db9a78065c23ed845c6584c869885 to your computer and use it in GitHub Desktop.
Using Themosis templates in ACF
<?php
/**
* ACF has this fancy option to show ACF fields based on pages templates.
* One problem: Themosis templates are not registered as "default" WordPress templates.
* This file hooks into the acf filters and allows ACF to use the Themosis templates.
*
* Compatible Themosis version: 1.2.3
*/
// add themosis templates
add_filter('acf/location/rule_values/page_template', 'add_themosis_templates_to_acf_rules');
function add_themosis_templates_to_acf_rules($choices)
{
$key = 'theme';
$configFile = 'templates.config.php';
$configTemplates = include(themosis_path($key) . 'config' . DS . $configFile );
$templates = array();
foreach ($configTemplates as $configTemplate) {
$prettyTemplateName = str_replace(array('-', '_'), ' ', ucfirst(trim($configTemplate)));
$templates[$configTemplate] = $prettyTemplateName;
}
return array_merge(array('none' => __('- None -')), $templates);
}
// get themosis templates
add_filter('acf/location/rule_match/page_template', 'get_themosis_templates_from_acf_rules', 11, 3);
function get_themosis_templates_from_acf_rules($match, $rule, $options)
{
// vars
$page_template = $options['page_template'];
// get page template
if (!$page_template && $options['post_id']) {
$page_template = get_post_meta($options['post_id'], '_themosisPageTemplate', true);
}
// compare
if ($rule['operator'] == "==") {
$match = ($page_template === $rule['value']);
} elseif ($rule['operator'] == "!=") {
$match = ($page_template !== $rule['value']);
}
// return
return $match;
}
@basbitfactory
Copy link

Hi Sergio, above is failing again after last ACF update (5.6)
Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment