-
newpress
I do not see a way to add a class to to a basic anchor tag (link) without directly editing the HTML
Is there a better/easier way? -
Hi there,
Do you mean adding a CSS class to an
<a>
element?Is the
<a>
element inside a<p>
element? If so, yes, you will need to manually edit the<p>
to add class to the<a>
. -
newpress
Yes, adding a CSS class to an element
So edit the HTML … -
Alvind
Hi there,
The easiest way is to directly edit the block HTML to add the class. An alternative way is to use a PHP filter, but it’s a bit more complicated.
-
newpress
Yes, currently doing a php work around…odd that it is not easy to just add a class to any element without editing html directly.
So here we wrap a link in a container and assign a class to the container then this snippet will add a class to the tag found within the container.
—————————————–
<?php
// add colorbox classes to tag when the parent block has a class of “add-colorbox”
add_filter( ‘render_block’, function( $block_content, $block ) {
if (
!is_admin()
&& ! empty( $block[‘attrs’][‘className’] )
&& ‘add-colorbox’ === $block[‘attrs’][‘className’]
){
$block_content = str_replace( ‘<a ‘, ‘<a class=”wp-colorbox-youtube cboxElement” ‘ , $block_content );
}return $block_content;
}, 10, 2 );
-
Or you can add a text block (GB 2.0), and set its tag to
<a>
,display
toinline
, then you can add class to it. -
newpress
Ok I installed the GB pro 2.0 beta but do not see a “text” block
-
if you have GB v1 installed on the site previously, then you need to manually activate v2 blocks at dashboard > generateblocks > settings.
-
newpress
GB Version 1.9.1
GB Pro Version 2.0.0-beta.6Not seeing anything in GB settings on activating any blocks
-
It’s because the 2 versions are not matching.
You need to update GB to 2.0 beta 6 as well, you can download it here:
https://generatepress.com/generateblocks-2-0-a-new-era/ -
newpress
Right!
Ok that works well adding a class toHowever
After adding an image and making the image a link, I would like to add a class to that tag
but I can not. Message says that “Class attribute is not allowed.”
Suggestion? -
Are you trying to add a class to the image block’s
<a>
tag?Don’t think that would work, you will need to use
render_block
filter. -
newpress
Yes that’s what I need, thanks.
-
No Problem 🙂
- You must be logged in to reply to this topic.