2

How do I use an Advanced Custom Field as a Shortcode. Ive used the following code in the Wordpress functions.php file but no luck.

Here is my Code:

function location_date_func( $atts ){

    return "<?php the_field('location_date', 658); ?>";
}
add_shortcode( 'location_date', 'location_date_func' );

4 Answers 4

1

You need to register the shortcode properly, and make it return the data to display, not return a string with php code in it:

function location_date_func( $atts ){
    //return string, dont echo it, so use get_field, not the_field
    return get_field('location_date', 658);
}
//create function to register shortcode
function register_shortcodes(){
   add_shortcode( 'location_date', 'location_date_func' );
}
// hook register function into wordpress init
add_action( 'init', 'register_shortcodes');

Or if you are using php 5.3+, you can use anonomous functions to acheive the same result:

add_action('init', function(){
    add_shortcode('location_date', function(){
        return get_field('location_date', 658);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

@johnnyr209 how exactly are you trying to use this shortcode? The built in shortcode doublesharp provided should work fine, as should this
The built in shortcode only works on ACF fields it does NOT work on default Wordpress fields.
1

Got it to work!

function location_date_func( $atts ){
    return apply_filters( 'the_content', get_post_field( 'location_details', 658 ) );
        }
add_shortcode( 'location_date_sc', 'location_date_func' );

Comments

0

If you want to return the value of an ACF field using the_field(), there is already a built in shortcode to do that.

[acf field="location_date" post_id="658"]

If you would like to reproduce it using the [location_date] shortcode, you need to use get_field() to return rather than echo the value. Syntax-wise, the only problem with your code is that you do not need the double quotes or <?php tags, since it should already be inside a PHP block. It will be functionally the same as the [acf] shortcode, but does not accept the post_id argument. This example will be hard coded to post ID 658 unless you modify it to accept an ID as part of the $atts or use the global $post;

function location_date_func( $atts ){
    return get_field( 'location_date', 658 );
}
add_shortcode( 'location_date', 'location_date_func' );

4 Comments

The default ACF shortcode only works within ACF fields it does not work on any default Wordpress fields. The function code provided does not work. Im thinking it may have to do something with the ACF Post ID
Maybe I don't understand the issue then - if you want to use regular WordPress custom fields then it has nothing to do with ACF. You need to use the standard get_post_meta( post_id, 'meta_key', single )
you loose the WYSIWYG editor if you use the WordPress custom fields
I don't think you are asking the right question then... all this does is get the values, if you want to use the ACF fields on the front end that is something else entirely.
0

add_shortcode('location_start_your_application_group', 'start_your_application_group');

function start_your_application_group() {
    $start_your_application_group = '';
    $start_your_application_group .= '<section class="start-your-application">';
     if ( have_rows( 'start_your_application_group', 'option' ) ) : 
         while ( have_rows( 'start_your_application_group', 'option' ) ) : the_row(); 
           $heading = get_sub_field( 'heading' );
             $content = get_sub_field( 'content' );

             if ( $heading !== '' ) {
                    $start_your_application_group .= '<h3 class="start-your-application__heading">' . $heading . '</h3>';
             }
             if ( $content !== '' ) {
                $start_your_application_group .= '<div class="start-your-application__content">' . $content . '</div>';
             }

             $image = get_sub_field( 'image' );
             if ( $image ) { 
                $start_your_application_group .= '<div class="start-your-application__image-container"><img class="start-your-application__image" src="' . $image['url'] .'" alt="' . $image['alt'] . '" /></div>';
            } 
        endwhile;
    endif;
    $start_your_application_group .= '</section>';

    return $start_your_application_group;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.