I'm using the advanced custom fields to add some call

I'm using the advanced custom fields to add some call to action options for some of my posts. Specifically, I'm trying to use the plugin to make a link and then use that link as the href for a button on a post. The following code is getting me close but I had to manually add the href to my button tag. I left in the code "the_field('call_to_action_link') as it currently displays the actual link text which is what I want my href to equal. What I'm trying to do is us that function to replace the manually input href. How would I use the php function within my html to dynamically populate the href based off of the custom field for that particular post?

`<?php
if(get_field('call_to_action_text')) {
echo '<div class = "call-to-action">';

echo'<p>' . get_field('call_to_action_text') . '</p>';
the_field('call_to_action_link');
echo '<button href = "http://localhost/cpt_class/?p=1175">text</button>';

echo '</div>';
}
?>`

  • Michelle Shull

    Hi there, James!

    I think this may end up being a case for our next level of support, they handle a lot of the more complex and code-related questions we get, but I want to make sure I understand what you're trying to do here, first.

    If I'm reading you correctly, you've got an advanced custom field on your posts where authors enter a URL. You'd like to dynamically replace this author entered URL with a different URL? Or are you trying to create a text link with dynamic text? Is this text entered along with the URL?

    Thanks james!

  • james

    Hello!

    As usual, asking the question has brought me inspiration to the answer. Thanks for your time but I've got a solution. Sometimes it's hard to explain exactly what one is trying to do with web development, especially someone like me who isn't all that experienced, but here goes...

    I'm making a landing page with a call-to-action section that will funnel people to another, more in-depth page. I'm trying to make the page responsive so I decided to use advanced custom fields for the call to action link and text as they will be styled to make them stand out on the page and will have to move around for different screen sizes so they can be visible at all times without scrolling. Also, I wanted to give the author the option to have the call to action go to any page he or she desired.

    If the page is a landing page with a call to action section, the author would enter info into the custom fields for the call to action section on the page. The two custom fields the author would enter would be the call to action text and a URL for the button to go to if a user wanted to follow the link.

    The actual link was to be in the form of a button on the screen so I could style it and make it tempting for people to click on. My issue was making it so that the URL the author entered in the advanced custom field while making the post was where the user would be taken to upon clicking the button.

    This is my first attempt at the advanced custom fields plugin and it's a bit intimidating. However, the problem I was having was forgetting a little bit of PHP101 and the ability for going in and out of PHP at will. I started my php statement with an if statement and was assuming that I couldn't get out of the php statement, write some html then embed a separate php function within that html... Anyway, long story short, here is the code I ended up with.

    <?php
                    if(get_field('call_to_action_text')) {
                        echo '<div class = "call-to-action">';
    
                            echo'<p>' . get_field('call_to_action_text') . '</p>';
    
                ?>
                            <input type ="button" class="cta-button" value="text"
                                    onclick= "window.open('<?php the_field('call_to_action_link'); ?>')">
    
                <?php
    
                        echo '</div>';
                    }
                ?>

    I left my php statement after displaying the advanced custom field "call_to_action_text" and wrote the html for the button. I then reentered php when calling the href from the advanced custom field "call_to_action_link" so that whatever link the author entered for that particular page would be the page the user would go to upon clicking the button. I then jumped back into php to close the div and end the conditional statement...

    I hope that makes sense. I know enough about this stuff to confuse myself and others but I'm getting better.

    Thanks again!

    James

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.