I am creating a GB Block for a Card. The Card should be optionally linked.
If a link is set, then inside of the h3 i want to have a-tag wrapping the headline text.
If no link is set, then no a-tag should be there.
Can’t figure out how to achieve this.
Can anyone help me out?
@MichelyWeb Hey Marco, this is a copy of the Inner Circle DM that I just sent you. Let’s pick up this conversation in this Pinegrow forum so others can benefit from it:
Question: Will you add the card title and link in the block as a block attribute, use post data through a wp_query, or use custom fields (ACF/Metabox/etc.)? The project I was thinking of doesn’t do what you are looking for, so I’m noodling on this one a bit and realized that we might need to tackle it slightly differently, depending on the situation. For example, if you are just adding block content, it might be as simple as making the title an inner block and using Gutenberg to tackle the link issue. If it’s dynamic data, we can use some conditionals to show or hide the appropriate element based on the presence of the field/data.
It might also help to know a bit more about the use case for this card so we can suggest alternatives. For example, if it’s a biography card, you might consider making it a CPT rather than using block attributes since you’ll probably want to access the associated information in more than one place. See this video (Timestamp 24:49, Section: Block Type 3 - Dynamic Block) Create a Native WordPress Block Plugin Using Pinegrow Web Editor - YouTube for a demonstration.
The card can be used in different use cases. If it is linked, then it gets an hover effect and is clickable in a whole. In this case it is not a CPT or something so the content is added freely: image, title (h3), short text (p)
Your idea of just using the Core/Heading block that can be linked would work but it needs more knowledge from the user. So i would prefer to have more control in PG.
Here is in a screenshot all logic i would need for that:
Because Attribute id’s can be used for referencing Block attributes already defined in this case card_link would be the link field in the block.
It’s possible to get the URL from link field in the expression, but its not possible to use the same statement in Attribute id (which is understandable from coding perspective).
Whats strage: when card_link is used as referenced attribute, it is not handled as empty even if no link is set in WP Block for that field. Therefore the Block Attribute “If empty” does not work. Seems to be a bug. @Emmanuel ?
I don’t have anything to add here yet, other than I think we are both traveling down the same paths based on your bug reports regarding the toggle control
Can I assume you are using K. Geary’s card structure in your project? That’s what I’m using in my tests as well.
I’m having good luck getting the linked title to work as expected, but struggling to make the unlinked title appear and disappear based on the presence of the link or the state of the toggle. I’m going to keep plugging at this since I have other tasks that I’m actively avoiding this morning…
That presents the issue of not having a great way to hide the unlinked heading when the toggle is off as you can see by the duplicated “Jane Dewey Linked” titles in the screenshot I posted previously.
@MichelyWeb@adamslowe yes, it makes sense to have more control over when the block elements are displayed. So, I added the Block Condition action that makes it very easy to support your use case.
Process:
Add 2 H3 elements, one without the link and the second one with the link.
On the first H3 add Block Condition to show it if card_link is NOT set:
In my original Project i was typing the attribute ID manually which was not working. In the demo-project i can select the attribute from the list and it’s working.
Same behavior on a new Block on the same index.html
First the attributes are not selectable. Only after removing the Block completely and adding it again (copy&paste) it’s working.