Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic value in the attributes #6

Closed
xu4wang opened this issue Nov 16, 2024 · 1 comment
Closed

dynamic value in the attributes #6

xu4wang opened this issue Nov 16, 2024 · 1 comment

Comments

@xu4wang
Copy link

xu4wang commented Nov 16, 2024

Is it possible to assign dynamic values to an attribute? for example

x-data = { href: '{{post_permalink}}' }

I am trying to use it inside a loop.

@websevendev
Copy link
Owner

Doesn't look like it, there is no filter in place. I suppose there could be one here:

So replace it with:

	return apply_filters('afb_get_attributes', $attributes, $args, $tags);

(I'll include it in the next release so it won't disappear with a plugin update)
and then you can use something like:

add_filter('afb_get_attributes', function($attributes) {
	foreach($attributes as $key => $value) {
		$attributes[$key] = str_replace('{{my_dynamic_permalink}}', esc_js(get_the_permalink()), $value);
	}
	return $attributes;
});

Alternatively you could locate the dynamic value with AlpineJS on the page, if it's available:

<!-- wp:query -->
	<div class="wp-block-query">
	<!-- wp:post-template -->
		<!-- wp:post-title {"isLink":true,"attributesForBlocks":{"x-cloak":""}} /-->
		<!-- wp:post-date {"attributesForBlocks":{"x-data":"{href: $el.previousElementSibling.querySelector('a').getAttribute('href')}","x-text":"href"}} /-->
	<!-- /wp:post-template -->
	</div>
<!-- /wp:query -->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants