Archived topic
Custom Field: Where does php go in Generate Press tools?
17 replies · Started by Tom on February 8, 2022
Hi,
I am trying Advanced Custom Fields in my theme and have a question.
I have code:
<?php if ( get_field('position_open') ): ?>
Add button code here
<?php endif; ?>
But am not sure where it goes. Does it go:
1 - On the single.php page
2 - Or some area within the Generate Press tools, ON the page in question (which would be a page for a Custom Post Type - "Positions"?)
Any help would be appreciated.
Thank you.
Hi Tom,
You can use one of the methods introduced here to add PHP:
https://docs.generatepress.com/article/adding-php/
Thank you Ying!
One follow-up question: the code I listed above - is that an example of code that can appear in the functions.php page? To me that would be easy to do, but the examples I saw had a person adding the code to single.php. Which looked far more complicated once I opened the single.php generate press page. Let me know.
And thank you!
What does the instruction say?
I misread it.
1 - added to functions.php - but broke site - so it obviously needs something else (hook or such).
2 - Then tried code snippets plugin
3 - Which also broke the site.
Can't easily undo that, so I guess I have to FTP in and disable the plugin folder, then do more reading.
You can activate Code snippet's safe mode:
https://help.codesnippets.pro/article/12-safe-mode
Can you show me the Advanced Custom Fields instruction of its PHP code?
Thank you very much Ying for the Safe Mode!
1 - I have a custom post type “Position” and am using it to list job descriptions.
If a Position is OPEN (a true false in the back end with Label "Position Open or Closed" and the Field Name "position_open") – I would like a URL Link to appear in the back end.
2 - I then have another field with the label "Apply Link" with the field name "apply_link" - which should appear with the rule I applied: Show field if Position Open or Closed has a value equal to "checked."
3 - I then wanted the link to appear in the front end using the code:
<?php if ( get_field('position_open') ): ?>
Add button code here <- this would be link code for now.
<?php endif; ?>
I obviously am doing something wrong. Just don't know what. :^) But I appreciate your help!
Ah I see, thanks for the explanation :)
You can use a hook element to add the code, remember to check the execute PHP box.
I will read that - I also am missing the code in the php statement to show the link (echo) - but one step at a time. :^)
I'm sure I will have more questions - but will go learn about hooks now.
Thank you Ying!
Awesome! Got the basic test to work: Simple text I have used for now appears and disappears depending on whether the Position is true! Very cool!
However - the placement 'generate_after_content' sort of throws the text to the far right since I have three columns.
Is there a way to capture the text I generated into a block somehow? I want it in my center column.
Thoughts?
So close... I have this code:
<?php
$apply_link = get_field('position_open');
if( $apply_link ): ?>
<a class="button" href="<?php echo esc_url( $apply_link ); ?>">Click to Apply</a>
<?php endif; ?>
However - when the button appears, the link is NOT the link specified by apply_link. The link, for whatever reason, is 0.0.0.1 not the desired link.
Do you just want to pull the url from the custom field to the button?
If so, using a block element with dynamic feature seems easier for you.
https://www.screencast.com/t/uHUSkbWiQri
Please be noted, in order to use block element dynamic feature, GenerateBlocks plugin needs to be installed and activated.
I got the block element working, but don't like that method.
1 - Yes - it accomplishes what I want - but I have to create that separate block for every landing page.
2 - I would rather get help with the code above - to get that to work.
3 - Why? Then when other people are creating positions, they just fill in the link that is generated when the position is created.
So - I'd rather figure out what is wrong in this code below -because it is not generating the link it needs too (I'm not a PHP writer, so do not know if what I did below is correct):
<?php
$apply_link = get_field('position_open');
if( $apply_link ): ?>
<a class="button" href="<?php echo esc_url( $apply_link ); ?>">Click to Apply</a>
<?php endif; ?>
but I have to create that separate block for every landing page.
Why? You should not. What's the location you set for the block element?
But if you insist to use code, try this:
<?php
$keywords = get_field( 'position_open' );
if ( $apply_link ) {
echo '<a class=button'.' href=' .$apply_link. '>Click to Apply</a>';
}
?>
Hi Ying,
1 - The reason I wanted to stick with my code method as opposed to the block is because it will be easier from a maintenance standpoint when landing pages and URL links become different and added to the site by people without the knowledge to build the generateblocks.
2 - I spoke to ACF support and they asked me to try similar code - and it worked (below):
<?php
$apply_link = get_field('apply_link');
if( $apply_link ): ?>
<a class="button" href="<?php echo esc_url( $apply_link ); ?>">Click to Apply</a>
<?php endif; ?>
3 - The button is large still - but I guess I can work on that.
4 - The location will still be an issue. Is there away to take this code, and make it shortcode to put in an exact position within content?
Thank you for all your help - it is appreciated!