简体   繁体   中英

Return ACF field as a Shortcode using Wordpress Functions.php file

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' );

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);
    });
});

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' );

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' );

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM