Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Use ACF input to create link for Headline block

  • Hi, I’m using GP Pro, GB Pro and ACF to create a Page Hero element for my custom post type. I would like to have a category specified in ACF and then use that to build several links that will be used for Headline blocks in the Page Hero. Is this possible?

    Thank you!

  • Hi there,

    You may move the ‘upper 3-across grid’ inside the Page Hero element instead of on individual pages. Then you can set all the dynamic parts of it just like you did with the existing Page Hero.

  • How can I turn the user’s category selection into a full URL?

  • Hi there,

    what is a) the user’s category selection and b) what is the expect full URL ?

    Kinda need to know where the parts of the URL come from.

  • Hi David,

    An editor would select the category in an ACF field when setting up each of the custom posts.

    Each of the custom posts needs three URLs. The URLs would look like:
    myurl.com/category/[yourcategoryhere]/?tag=tag1
    myurl.com/category/[yourcategoryhere]/?tag=tag2
    myurl.com/category/[yourcategoryhere]/?tag=tag3

    where tag1, tag2 and tag3 are always the same.

    Thank you!

  • Where did you set up the [yourcategoryhere]? For the local magazine post type? What is the ACF field called? Where do you want to pull the field data?

  • Hi Ying,

    Yes, in the local magazine post type. I have added it back and it’s called “location”. I want to use it on the element called “local magazine template”.

    I have added some clarifying information in the private information as well.

    Thank you!

  • I see, try this:

    1. Add this PHP code:

    add_filter('render_block_generateblocks/headline', function ($block_content, $block) {
        $allowed_classes = array('local_magazine_location_tag_1', 'local_magazine_location_tag_2', 'local_magazine_location_tag_3');
    
        if (
            !empty($block['attrs']['className'])
            && in_array($block['attrs']['className'], $allowed_classes, true)
        ) {
            $location_ids = get_post_meta(get_the_ID(), 'location', true);
    
            // Ensure $location_ids is not empty and is an array
            if (!empty($location_ids) && is_array($location_ids)) {
                foreach ($location_ids as $location_id) {
                    // Check if the term exists by ID
                    $term = get_term($location_id);
    
                    if ($term && !is_wp_error($term)) {
                        // Get the term slug
                        $term_slug = $term->slug;
    
                        $site_url = home_url();
    
                        // Customize the dynamic_url based on the CSS class
                        switch ($block['attrs']['className']) {
                            case 'local_magazine_location_tag_1':
                                $dynamic_url = $site_url . '/category/' . $term_slug . '/?tag=tag1';
                                break;
                            case 'local_magazine_location_tag_2':
                                $dynamic_url = $site_url . '/category/' . $term_slug . '/?tag=tag2';
                                break;
                            case 'local_magazine_location_tag_3':
                                $dynamic_url = $site_url . '/category/' . $term_slug . '/?tag=tag3';
                                break;
                        }
    
                        // Replace # with the dynamic_url in the block content
                        $block_content = str_replace('#', $dynamic_url, $block_content);
                    }
                }
            }
        }
    
        return $block_content;
    }, 10, 2);
    

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    2. Add CSS class to the 3 headline blocks local_magazine_location_tag_1, local_magazine_location_tag_2, local_magazine_location_tag_3.

    Adding CSS class(es): https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    3. Set the headline blocks’ link to #, do NOT enable dynamic link.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.