I’ve been trying dozens of different things, but can’t quite figure this out. I have four post categories: art, cinema, literature, music. Posts typically only have one category, but sometimes they have multiple categories. Using ACF, I created a Repeater field, “list_post_name,” a text subfield with the field name, “postname,” and a “Select” subfield with the field name, “category.” Each post has at least one subject name (“postname”), but sometimes multiple names that each belong to different categories. (e.g. postname: Charles Mingus; category: music). I created dynamic tags that output a postname and its associated category: {post_name} and {post_name_category}. The {post_name_category} has the same values as the post categories: art, cinema, literature, music.
My issue is that I can’t figure out how to only output the post name and post name category if it matches the category called by the query loop. Right now, when my query loop calls for the “music” category and a post also has a post name in a different category, it’ll show the name that isn’t being called in the query loop. I don’t know if my approach is flawed or if there’s a much more simple way of doing this. I’ve tried so many different methods and refining in different ways and I feel like I can’t see the forest for the trees anymore. I feel like there must be some incredibly simple solution to this, but I’m exhausted at this point.
This is my dynamic tag code:
/** Code to get post name from ACF */
// Register the custom dynamic tags
add_filter('bricks/dynamic_tags_list', 'add_custom_dynamic_tags');
function add_custom_dynamic_tags($tags) {
$custom_tags = [
[
'name' => '{post_name}',
'label' => 'Post Name',
'group' => 'Custom',
],
[
'name' => '{post_name_category}',
'label' => 'Post Name Category',
'group' => 'Custom',
],
];
return array_merge($tags, $custom_tags);
}
// Render the custom dynamic tags
add_filter('bricks/dynamic_data/render_tag', 'render_custom_dynamic_tags', 10, 3);
function render_custom_dynamic_tags($tag, $post, $context) {
switch ($tag) {
case 'post_name':
return render_post_name_tag($post);
case 'post_name_category':
return render_post_name_category_tag();
default:
return $tag;
}
}
function render_post_name_tag($post) {
$post_id = $post->ID;
$post_names = get_field('list_post_name', $post_id);
if (!$post_names || !is_array($post_names) || empty($post_names)) {
return 'No names available';
}
global $entry;
$entry = $post_names[array_rand($post_names)];
return $entry['postname'] ?? 'Name not found';
}
function render_post_name_category_tag() {
global $entry;
return $entry['category'] ?? 'Category not found';
}
// Handle the custom dynamic tags in content
add_filter('bricks/dynamic_data/render_content', 'render_custom_content', 10, 3);
add_filter('bricks/frontend/render_data', 'render_custom_content', 10, 2);
function render_custom_content($content, $post, $context = 'text') {
$tags = [
'{post_name}' => function() use ($post) {
$name = render_post_name_tag($post);
$category = render_post_name_category_tag();
return "<span class=\"post-name {$category}\">{$name}</span>";
},
'{post_name_category}' => function() {
$category = render_post_name_category_tag();
return "<span class=\"post-name-category {$category}\">{$category}</span>";
},
];
foreach ($tags as $tag => $replacement) {
if (strpos($content, $tag) !== false) {
$content = str_replace($tag, $replacement(), $content);
}
}
return $content;
}