WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Placing a php call inside a shortcode in the page template (4 posts)

  1. PTM Groups
    Member
    Posted 2 years ago #

    Hi, I've looked here already: http://codex.wordpress.org/Function_Reference/do_shortcode

    But I don't believe the answer is there.

    What I’m trying to do is this:

    I’ve created a custom field called 'site_inspection'. The value of this field is a url.

    I want to insert the value of this field (the url) into a specific button shortcode in the page template so that the admin can enter the url into the field and the code will automatically insert that url into the button shortcode and display the completed button on the page.

    I can get the shortcode php to work if I hardcode the url, and the site_inspection field also works fine separately, but when I try to replace the hardcoded url with the php for the site_inspection field it doesn’t work.

    See Below

    <?php echo do_shortcode('[button link="<?php if(get_field('site_inspection')){echo get_field('site_inspection') ;}?>" type="icon" icon="search" newwindow="yes"] Site InSpection[/button]'); ?>

    I have also tried:
    <?php echo do_shortcode('[button link=" . (get_field('site_inspection')) ? (get_field('site_inspection') : ('') . ' type="icon" icon="search" newwindow="yes"] Site InSpection[/button]'); ?>

    With no success.

    I feel like I'm missing something simple in my syntax, but I can't spot it. Any help would be appreciated.

    Thanks.

  2. codingpet
    Member
    Posted 2 years ago #

    Hi,

    For the first one, you added <?php ?> twice. Also, is get_field a custom function you defined? The custom fields, I can use the code below to get the URL. You may take a look at the difference from yours. I used get_post_meta to get the value and then add it to shortcode. You can add the necessary conditional operators.

    <?php $site_inspection = get_post_meta($post->ID, 'site_inspection', true); ?>
    <?php echo do_shortcode('[button link="'.$site_inspection.'" type="icon" icon="search" newwindow="yes"] Site InSpection[/button]'); ?>

    codingpet.com

  3. PTM Groups
    Member
    Posted 2 years ago #

    That worked perfectly! You're a life saver, thank you :)

  4. codingpet
    Member
    Posted 2 years ago #

    You are welcome. :)

Topic Closed

This topic has been closed to new replies.

About this Topic