-
Topfgartenwelt
Hi,
I have created a text block for affiliate links it is always an H3 followed by a short text and the affilate box (reuseable pattern) with varying links and information. Is it possible to put this in a “field” where I can decide if it is inserted or not?
I don’t think it would work with AdInserter because the text is not always the same. I think about something like a “block element” that I can insert or not and where the content can differ.
Greetings Kathrin
-
Hi there,
do you use GenerateBlocks Pro ?
And do you use custom fields eg. ACF ? -
Topfgartenwelt
I don’t have Generate Blocks Pro but I have ACF Custom Fields. I have built a Custom Field that includes a WYSIWIG Editor. But I’m not sure if only a field or an ACF Block would be a better way. And I don’t know how to style the content in this field so that it looks like it was in the Screenshot.
Greetings Kathrin
-
You can add the info to the WYSIWIG field, and use a GB headline block to pull it, post meta > the slug of WYSIWIG field. Set the headline block’s tag to
div
. -
Topfgartenwelt
Ok and how I do that? Is it possible to globally turn on and off such field?
Greetings Kathrin
-
Is it possible to globally turn on and off such field?
I don’t know what you mean by “turn on/off such field globally”.
If it’s related to ACF settings, please check ACF’s documents or contact their plugin support.
For the setting in GB, it should be pretty straightforward, let me know which specific step you have difficulty with.
-
Topfgartenwelt
For me it is necessary that I can turn of / check on of block with one click on all posts. For example I have created with Davids help an ACF field / php snippet for a shortcode. It is possible to control it with the php script. If I turn this of the code isn’t inserted anymore.
I got it work but it looks terrible: https://imgur.com/a/WBhCbAN
And it is inserted twice.
Greetings Kathrin
-
For example I have created with Davids help an ACF field / php snippet for a shortcode
What is the PHP code?
I got it work but it looks terrible:
Why it’s terrible? it should not be looking like this? How should it look like?
And based on your screenshot, you should not be pulling dynamic link from the same field. so disable that, it might fix the display twice issue.
-
Topfgartenwelt
I have created an Element – Block with 3 Heading Blocks set to div, as you can see here: https://imgur.com/a/ldsfLdQ
Than I have created 3 ACF fields affiliate_block_heading, affiliate_block_beschreibung and affiliate_block_links (I can see them in the backend under the post)
I have set the div to post meta and added the slugs where I want to be them.
In the next step I have created a shortcode like here: https://generatepress.com/forums/topic/hook/#post-1036765 with Code Snippets
function db_affiliate_hook_inside_shortcode($atts, $content = null) { ob_start(); do_action('custom_shortcode_hook'); return ob_get_clean(); } add_shortcode('hooky_affiliate_shortcode', 'db_affiliate_hook_inside_shortcode');
In the next step I have set in the block element the position to custom –> hook name affiliate_hook_inside_shortcode
I have tried to so set the display rules to posts – all posts / blank
But I can’t see the shortcode working 🙁 You can find the link to the testsite in the private section.
Greetings Kathrin
-
This seems overly complicated.
Instead you could do:1. use a GP Block Element to hook in your Affiliate Notice on all your posts.
1.1 make a note of the Elements ID.2. create a ACF boolean field for the Hide Affiliate Option.
Make a note of the ACF field name.3. Add the following PHP Snippet to the site:
add_filter( 'generate_block_element_display', function( $display, $element_id ) { $hideaffiliate = get_field('your_custom_field', get_the_ID() ); if ( 999 === $element_id && false === $hideaffiliate ) { $display = false; } return $display; }, 10, 2 );
You need to:
i)$hideaffiliate = get_field('your_custom_field', get_the_ID() );
Change theyour_custom_field
to your custom field name.ii) change the
999
to the element ID.So the GP Element will display the notice on all posts.
And on any post you want to hide it, you simply set your ACF field to true. -
Topfgartenwelt
Ok, but the content of that element always changes. The affiliate links are different for all posts and also the describing text for that links. So I thought the solution with the shortcode and the dynamic fields would be great. Following your recommendation the same content would be displayed on all posts. Am I right?
Greetings Kathrin
-
Following your recommendation the same content would be displayed on all posts. Am I right?
No, the content in the element is still dynamic, this PHP is to show or not show the element by creating an ACF boolean field.
-
Topfgartenwelt
Ok, I have created a GP Block Element, at the moment it includes 3 Header-Blocks with divs. I need a fields for header, text and the affiliate-links with text. How I get them? Or do I create only a blank block element, because that block can be filled in the Gutenberg editor with text and so on, and a boolean ACF field?
And where does that block show up? I would need a possibility that I can place it on different positions.
Greetings Kathrin
-
. I need a fields for header, text and the affiliate-links with text. How I get them?
you still don’t know how to use the dynamic data panel of the headline block?
Check this article, you should be able to enable the dynamic data, set content source to post meta, and fill the field with the custom field slug.
https://docs.generateblocks.com/article/dynamic-data-options-overview/#content-sourceIn your case, 3 headline blocks, each presenting one custom field. Let me know if you still don’t understand. and let me know which step you do not understand.
-
Topfgartenwelt
Dear Ying, I have already created a block element with three headline blocks each presenting one costum field connected to the relevant slug in the ACF custom field. Maybe there is a misunderstanding 🙁
I have created a boolean field and added the php from David to the Code Snippets tool. Now it shows up. For the moment I have set the block to appear before the comments block, but it should be display at the same position where the original text was. How can I do this? With a shortcode? Because the p-tags differ from post to post, so I can’t count them and set a custom hook.
Greetings Kathrin
-
it should be display at the same position where the original text was
Where exactly will the original text be placed?
- You must be logged in to reply to this topic.