1

Let me start off by saying that I usually make alot of research before asking publicly for help - due to likelyhood of information already being on the web - altho I've done several days of reseach etc I'm still in agony...

I'm using Wordpress with Advanced Custom Fields plugin - I've been able to make it show the image on my front page using a simple HTML code

<img src="[acf field='image' post_id=''. $post_id .'']" />

Where I created a custom field called 'image' . Now I want to create a shortcode to display this link if possible? I've gone through alot of possiblities such as;

Tried this too;

<?php
function my_shortcode() {
    $output = '';
    $output.= '<img src="[acf field='image' post_id=''. $post_id .'']" />';
    return $output;
}

Didnt take... then I went for the last one I could find on the web;

    function my_shortcode() {
    ob_start();
    ?> <HTML> <img src="[acf field='image' post_id=''. $post_id .'']" /> <?php
    return ob_get_clean();
}

Which ended into something like this on my website - outcome

I'm really out of my depth here guys... I'd be really glad if anyone would be willing to help me out a little!

Thanks in advance!

2 Answers 2

1

You have to use do_shortcode() to execute shortcodes in your string

Full code should be like that

<?php
function my_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'post_id' => '', // Default value.
    ), $atts );

    $output = '[acf field="image" post_id="' . $atts['post_id'] . '"]';
    $output = do_shortcode( $output );
    $output = '<img src="' . $output . '" />';
    return $output;
}

add_shortcode('my_link', 'my_shortcode');

Usage:

[my_link post_id="xxx"]

where xxx is id of the requered post.

8
  • Hey there! Thanks for a really fast answer! Did you mean to change the $output in here prntscr.com/i2kmu7 ? Thanks a lot again
    – Pr0metheus
    Jan 19, 2018 at 9:39
  • Exactly. do_shortcode() executes shortcodes in your string. Jan 19, 2018 at 9:41
  • I have extended the answer Jan 19, 2018 at 9:43
  • And I hope you understand that $post_id must be send to my_shortcode function Jan 19, 2018 at 9:45
  • Hey, thanks for the edits and for the effort - prntscr.com/i2kto6 leads to prntscr.com/i2ku0y (empty site) - should I look into something different rather than the code itself ? Maybe some settings I should modify to enable the function.php file?
    – Pr0metheus
    Jan 19, 2018 at 9:54
0

enter image description here

function my_shortcode(){
return '<img src="[acf field='image' post_id=''. $post_id .'']" />';
}
add_shortcode('image','my_shortcode');
4
  • All looks good.
    – melvin
    Jan 19, 2018 at 9:42
  • prntscr.com/i2ks7y ends up into prntscr.com/i2ksk1 I'm not really sure - should I edit some other document to enable the function.php - if I comment the whole php code - the site's working again
    – Pr0metheus
    Jan 19, 2018 at 9:50
  • For the last time: Please stop posting plain code snippets; edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Jan 19, 2018 at 9:57
  • I'm sorry fuxia - I'll learn from here and try better next time!
    – Pr0metheus
    Jan 19, 2018 at 10:04

Your Answer

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

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