0

I created the following shortcode which works well in returning the featured image of any given post I am on with a size...

function mfeaturedimage( $atts ) {
    $a = shortcode_atts( array(
        'size' => 'medium',
    ), $atts );
    return the_post_thumbnail("{$a['size']}");
}
add_shortcode( 'feature', 'mfeaturedimage' );

...But how can I evolve this to "also" be called directly from my content-single.php file, (i.e. feature();)

To sum it up, I want a single function that is flexible enough to work as a shortcode and also as a function declaration statement, but I'm not sure how to structure it...

1

I'm not sure I fully understand, but you can call

echo do_shortcode("[feature site='your-choice']");

within the content-single.php file. If that isn't enough, I suppose a function like this could be made:

function feature($size){
    echo do_shortcode("[feature size='{$size}']");
} 
  • This is cool, I didn't know do_shortcode() could reference my original shortcode function and display it on a custom php page with echo. That is exactly what I needed. Thanks – klewis Oct 26 '16 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.