WAIT: Featured image is not showing in archive template - {featured_image}

The featured image worked fine at first.
I created a custom dd tag in my functions.php following the tutorials (Create Your Own Dynamic Data Tag – Bricks Academy). Below is my code.

add_filter('bricks/dynamic_tags_list', 'add_my_tag_to_builder');
function add_my_tag_to_builder($tags) {
	// Ensure your tag is unique (best to prefix it)
	$tags[] = [
		'name'  => '{custom_dd_tag}',
		'label' => 'My Dynamic Data',
		'group' => 'My Dynamic Data Group',
	];

	return $tags;
}

add_filter('bricks/dynamic_data/render_tag', 'get_my_tag_value', 10, 3);
function get_my_tag_value($tag, $post, $context = 'text') {
	// $tag is the tag name without the curly braces
	// Only look for dynamic tag starts with my_dd_tag:

	//if (gettype($tag) == 'array') return;

	if (strpos($tag, 'custom_dd_tag:') === false) {
		return $tag;
	}

	// Get argument
	$argument = str_replace('custom_dd_tag:', '', $tag);

	// Do your custom logic here, you should define run_my_dd_tag_logic() function
	$value = run_my_dd_tag_logic($argument, $post);

	return $value;
}

function run_my_dd_tag_logic($argument, $post) {
	// Do your custom logic here

	$price = "";
	$dates = explode('--', $argument);

	$pick_date = new DateTime($dates[0]);
	$return_date = new DateTime($dates[1]);

	$interval = $pick_date->diff($return_date);

	$days = $interval->days;

	if ($days <  7) {
		$price = get_field('price_rate_for_1_weeks', $post->ID);
	}
	if ($days <= 14 && $days > 7) {
		$price = get_field('price_rate_for_2_weeks', $post->ID);
	}
	if ($days <= 21 && $days > 14) {
		$price = get_field('price_rate_for_3_weeks', $post->ID);
	}
	if ($days <= 30 && $days > 21) {
		$price = get_field('price_rate_for_1_month', $post->ID);
	}
	if ($days <= 90 && $days > 30) {
		$price = get_field('price_rate_for__3_months', $post->ID);
	}
	if ($days <= 180 && $days > 90) {
		$price = get_field('price_rate_for__6_months', $post->ID);
	}


	return $price;
}

add_filter('bricks/dynamic_data/render_content', 'render_my_tag', 10, 3);
add_filter('bricks/frontend/render_data', 'render_my_tag', 10, 2);
function render_my_tag($content, $post, $context = 'text') {

	// $content is the content of the element, including other dynamic tags, HTML, etc.
	// Only look for content starts with {my_dd_tag:
	if (strpos($content, '{custom_dd_tag:') === false) {
		return $content;
	}

	// Regex to match my_dd_tag: tag
	preg_match_all('/{(custom_dd_tag:[^}]+)}/', $content, $matches);

	// Nothing grouped in the regex, return the original content
	if (empty($matches[0])) {
		return $content;
	}

	foreach ($matches[1] as $key => $match) {
		$tag = $matches[0][$key];

		// Get the dynamic data value, $match is the tag name without the curly brackets
		// Can reuse the get_my_tag_value function created earlier
		$value   = get_my_tag_value($match, $post, $context);

		// Replace the tag with the transformed value
		$content = str_replace($tag, $value, $content);
	}

	return $content;
}

This code worked in my search results template (I use posts widget and featured image worked) and I achieved what I needed by this code. but when I go to archive template (**I am using query loop **), an error is showing (see below).

Uncaught TypeError: strpos(): Argument #1 ($haystack) must be of type string, array given

I managed to fix the error by adding ’ if (gettype($tag) == ‘array’) return; ’ in ‘get_my_tag_value’ function (see above code). But the featured image is not showing as shown in the first image.

I am uisng PHP 8.1.23
Could you help me to solve this issue?

Hi @kalpana_99 ,

I just moved your thread to “Developers” category as the issue arose when you added your own custom code.

I tried your code locally but was unable to replicate the issue.

Could it be other plugin’s code causing that? $tag should be string but not sure why it’s an Array in your case.

Alternatively, please send your admin credentials to help@bricksbuilder.io and include this thread URL as a reference.

Regards,
Jenn.

Hi, unfortunately Its in localhost.
But if you try the code in your tutorial, you will able to replicate the issue. Create Your Own Dynamic Data Tag – Bricks Academy.
try with passing arguments method mentioned in the tutorial . the code in the tutorial itself is not working its not just my code.

Hi @kalpana_99 ,

I tried the code in our tutorial article.
The dynamic tag outputs the correct result.
Example: {my_dynamic_tag:apple}

Using {featured_image} inside an archive template (query loop) is also working.

Kindly place clone your site to a publicly accessible domain and send your admin credentials to help@bricksbuilder.io + include this thread URL as a reference.
So I can investigate the problem.

Regards,
Jenn.