WordPress.org

Ready to get started?Download WordPress

Forums

Advanced Custom Fields
[resolved] Page Link field doesn't work (3 posts)

  1. Treebeard
    Member
    Posted 2 years ago #

    I followed the instructions for adding a custom field, and searched for the code to add it to my page template, but it's either broken, or I have the wrong code.

    I copied the code from the Documentation, changing the field name of course:

    <?php
    
    if(get_field('get_started'))
    {
    	echo '<div class="getstarted"><a href="<?php get_started('page_link'); ?>">' . $get_started . '</a></div>';
    }
    
    ?>

    I can see the link on the frontend if I use 'the_field' instead, but it's not a link, it's plain text - and the style class "getstarted" is not applied at all, it's ignored. I tried changing the 'get_field' to 'the_field' because 'get_field' shows nothing at all.

    Since this is not a required field, I don't want to show the 'getstarted' class all the time. I'm using this as an option for the admin to be able to add a custom button (an image button, using CSS) so when they choose a page from the dropdown menu, then the button should appear on the page as a link.

    Help? Does anyone know how to use the Page Links field? There is no documentation on the ACF site that shows an example, and nothing explaining how to use this.

    Thanks!

    http://wordpress.org/extend/plugins/advanced-custom-fields/

  2. Treebeard
    Member
    Posted 2 years ago #

    I've been trying to set up a "Page Link" field that uses CSS to create an image rollover for a link that's supposed to be added to the Page Link field. Documentation says it returns that link, but I can't get anything to work. I followed the documentation for the installation process, and there's nothing there that says anything about adding code to the functions.php file so I don't know if that's a Requirement or not (not listed there either).

    I added the Field Group.
    Field Name: 'get_started'
    Field Type: Page Link
    Required: No
    Post Type: All
    All Null? : Yes
    Select Multiple Values: No

    In my template I have this:

    <?php
    
    if(get_field('get_started'))
    {
    	echo '<div class="getstarted">' . $get_started . '</div>';
    }
    
    ?>

    I tried adding the link using the documentation example as a guide:

    <?php
    
    if(get_field('get_started'))
    {
    	echo '<div class="getstarted"><a href="get_started('page_link');">' . $get_started . '</a></div>';
    }
    
    ?>

    That doesn't work at all..

    The Documentation doesn't show an example with detection to make sure the field is used first.

    Does anyone have an example of a Page Link?

    Thanks~

  3. Treebeard
    Member
    Posted 2 years ago #

    Figured it out following instructions for other custom fields plugins and the codex.

    Didn't need to add anything to the functions.php file, just to the template. The only problem I had was that I couldn't figure out how to let a client input their own url for the button, so I had to code it in.

    Code examples:

    To add a custom field named 'my_custom_button' with css linking to a contact page:

    <?php if ( get_post_meta($post->ID, 'my_custom_button', true) ) : ?>
    <div class="mycss"><a href="<?php echo get_option('home'); ?>/contact"><?php echo get_post_meta($post->ID, 'my_custom_button', true) ?></a></div>
    <?php endif; ?>

    I also created a wysiwyg field named 'additional_content' and used this in my theme:

    <?php if ( get_post_meta($post->ID, 'additional_content', true) ) : ?>
    <?php echo get_post_meta($post->ID, 'additional_content', true) ?>
    <?php endif; ?>

    Hope this is helpful to anyone having trouble.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic