[Resolved] Adding ACF link (button) in page header, below intro text

Home Forums Support [Resolved] Adding ACF link (button) in page header, below intro text

Home Forums Support Adding ACF link (button) in page header, below intro text

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1650739
    robert

    Hi Generatepress support team,

    Fist of all, thanks for Generatepress! I’ve been using the theme for 2 years now, and it’s the best theme out there! Because of your excellent support, I’ve been able to find any answer or solution I needed for any questions or problems during my projects. Until now πŸ™‚

    My issue:

    I’m using elements to make headers for pages and hooks for other custom elements.

    In a page header I placed the title, and on a homepage I’ve added a custom title and some intro text using Advanced Custom Field.

    Now I want to place a button using a custom field, just below the intro tekst (or just below the page title (on other pages than the homepage).

    On the homepage, I use this code (and i want to stick the button just below the home_intro_tekst)

    <h1>{{custom_field.home_title}}</h1>
    <p>{{custom_field.home_intro_tekst}}</p>

    I can use an ACF link/button in a hook, using this code:

    <?php
    $link = get_field(‘page_button’);
    if( $link ):
    $link_url = $link[‘url’];
    $link_title = $link[‘title’];
    $link_target = $link[‘target’] ? $link[‘target’] : ‘_self’;
    ?>
    ” target=”<?php echo esc_attr( $link_target ); ?>”><?php echo esc_html( $link_title ); ?>
    <?php endif; ?>

    This works great, but the button ends up somewhere else (‘generate_before_content’ is the closest I can get the button). If I stick the php code below the intro-text in the header, all I get is Array (which makes sense, as it’s not just a textfield)

    In addition, I’ve tried to create a short-code of the button, so i could use it like the first example, but that didn’t work.

    Is there some way to make this work? (other than just creating an HTML button in the custom intro – I want the client to change the button if needed, also I will use this method on new projects in the future)

    If it cannot be done, i’ll still be very happy with Generatepress πŸ™‚ πŸ™‚
    Greatings from the cold and snowy Netherlands * * *
    * * *
    Cheers and thanks in advance! * * *

    #1650993
    Elvin
    Staff
    Customer Support

    Hi there,

    I believe PHP snippets won’t work on Header Elements.

    But you can use shortcodes there so we can turn your PHP snippet into a shortcode so we could use it on the Header element.

    Here’s an example PHP snippet based from the code you’re provided.

    add_shortcode( 'page_button', function() {
        ob_start();
    
        $link = get_field('page_button');
        if( $link ){
            $link_url = $link['url'];
            $link_title = $link['title'];
            $link_target = $link['target'] ? $link['target'] : '_self';
    
            $button = '<a target="'.esc_attr( $link_target ).'" href="'.$link_url.'">.'esc_html( $link_title )'.</a>';
    
        echo $button;
        }
    
        return ob_get_clean();
    } );

    Note: I’ve removed the PHP delimiters. You can add them back if needed.

    Once you’ve applied this to your site, you can use the shortcode [page_button] to display your button.

    #1651293
    robert

    Hi Elvin,
    Thanks!

    I got an error: syntax error, unexpected ‘esc_html’ (T_STRING)

    Which was in this line:

    $button = ‘.’esc_html( $link_title )’.‘;

    I’ve managed to fix it by adding a dot before the esc_html. In addition, I’ve added the button class to the url to complete it, and it’s working fine now. The working code is:

    add_shortcode( ‘page_button’, function() {
    ob_start();

    $link = get_field(‘page_button’);
    if( $link ){
    $link_url = $link[‘url’];
    $link_title = $link[‘title’];
    $link_target = $link[‘target’] ? $link[‘target’] : ‘_self’;
    $button = ‘‘.esc_html( $link_title ).’‘;
    echo $button;
    }
    return ob_get_clean();
    } );

    Thanks for helping me out! Really appreciate it!

    #1651417
    Elvin
    Staff
    Customer Support

    Nice catch! Glad you got it sorted. No problem. πŸ™‚

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