- This topic has 3 replies, 2 voices, and was last updated 3 years, 1 month ago by Elvin.
-
AuthorPosts
-
February 8, 2021 at 10:51 am #1650739robert
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! * * *February 8, 2021 at 3:42 pm #1650993ElvinStaffCustomer SupportHi 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.February 9, 2021 at 12:58 am #1651293robertHi 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!
February 9, 2021 at 3:11 am #1651417ElvinStaffCustomer SupportNice catch! Glad you got it sorted. No problem. π
-
AuthorPosts
- You must be logged in to reply to this topic.