Support » Developing with WordPress » Need to add a field to page template that changes button URL

  • Resolved thewaythecococrumbles

    (@thewaythecococrumbles)


    Dear WordPress community,

    I’m totally new at WordPress, and ran into a problem. I created a page template, that includes a button. For each page I make, i want the button URL to change through manual input. How do I accomplish this?

    The buttons I want to change the URL for are the red buttons at the bottom of this page: http://www.thewaythecococrumbles.com/united-kingdom/ (called ‘See all posts’)

    Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,
    As much as I understood your problem is, you want an input field in which whatever url you fill should be applied to that red button of that page right?
    If so then you just need to add a custom metabox for url field(an input field) related to pages.
    Read this for adding the custom meta box field.
    You’ll be able to find most of the bits of custom metabox.
    Hope this is what you want and it can help you.

    thewaythecococrumbles

    (@thewaythecococrumbles)

    Dear simplerthansimplest,

    I have read through the article, and find it slightly confusing. I’m a complete beginner and trying to get my head around it. There are two separate red boxes. Do I add that directly into the page template file? What exactly would I add? Which function?

    Thank you!!

    thewaythecococrumbles

    (@thewaythecococrumbles)

    Below is the code of the page template.

    [Large code excerpt removed by moderator per forum rules. Please use Pastebin or a Gist for all large code excerpts, they work better anyway.]

    • This reply was modified 2 years, 3 months ago by  bcworkz. Reason: excessive code

    Put your code in code tags, we can’t see the a tags like this which is what you need to edit.

    Hi @thewaythecococrumbles,

    I was looking at your template code and also looked at your website. I have a few suggestions as to how solve for this.

    The problem:

    If I understand correctly, you basically want to create “Country” pages that use this template and then each page would have “Recent Recipes” and “Recent Restaurant Recommendations”. Then have just a few of those displayed in the page and be able to look at all recipes or all recommendations.

    By looking at the code, it looks like you already have a few custom fields that store the posts to recipes and recommendations. You also have some custom fields that have the “map”, “key_facts” and “full_country_text”, etc.

    You may be using Advanced Custom Fields because (in your code) I see you are already pulling custom fields using the get_field() function like:

    <div class="full-width-content">
       <?php echo get_field('full_contry_text'); ?>
    </div>

    So this means that what you need to do is create two more custom fields that would contain the links to the archive pages for all posts with “Recipes” and “Recommendations”. But these should be by filtered by Country.

    I think you also have some custom post types like “Recipes” that have some information like in the case of Berry Coco Crumble

    Suggestions:

    • Create either a custom taxonomy or a category (Countries) and then use that when you create recipes, this way you are able automatically have an archive page that contains all recipes for a specific country.
    • Use that same taxonomy to apply it to the restaurant recommendations posts, perhaps you also have a custom post type for recommendations.
    • Create two new custom fields that would store the direct link to the archive pages for recipes and recommendations. Custom field names could something like ‘all_recipes_link’ and ‘all_restaurant_recommendations_link’.
    • Finally: pull the links from the corresponding custom fields into your template. These would go just before closing corresponding <div>s that you have with the classes ‘recent-recipes’ and ‘recent-restaurant-recommendations’. Like this:
      
      <div class="recent-recipes">
         (other php and HTML code goes here)
         .
         .
         <div class="readmore_button">
            <a href="<?php echo get_field('all_recipes_link'); ?>">See All Recipes</a>
         </div>
      </div>
      <div class="recent-restaurant-recommendations">
         (other php and HTML code goes here)
         .
         .
         <div class="readmore_button">
            <a href="<?php echo get_field('all_restaurant_recommendations_link'); ?>">See All Recipes</a>
         </div>
      </div>
      
    • I don’t know more details about the theme you are using but I hope this helps.
      -Armando

    • This reply was modified 2 years, 3 months ago by  Armando Duran.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Need to add a field to page template that changes button URL’ is closed to new replies.