WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Complex do_shortcode statements (4 posts)

  1. johnsalomon
    Member
    Posted 3 years ago #

    Hi there,

    I am trying to construct a do_shortcode statement in a PHP code widget, using a custom field. The idea is to get a shortcode evaluated inside a widget.

    The shortcode I'm trying to create is (this is an example)

    [flickrgallery setid="72157626892022672"]

    I want to use a custom value (called per_post_widget_content) on a per-post basis that lets me enter 72157626892022672 in a field.

    I figure I have to have statements that echo '[flickrgallery setid="', then get_post_meta for the custom field value (this part works), then echo '"]'

    The do_shortcode statement I have so far is

    <?php global $post do_shortcode(echo '[flickrgallery setid="'; echo get_post_meta($post->ID, 'per_post_widget_content', true); echo '"]';); ?>

    This does not work.

    I've tried variations on this, but I can't get the syntax right - can someone give me a pointer on what I'm doing wrong?

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    basics:
    http://php.net/manual/en/function.echo.php
    http://php.net/manual/en/function.return.php
    http://php.net/manual/en/language.operators.string.php

    do_shortcode() needs a string to work on;
    you cannot use echo in this situation within the brackets, rather use string concatenation;
    (not to confuse with the fact that you need to use echo do_shortcode() because do_shortcode() does return the result)

    example:

    <?php global $post; echo do_shortcode('[flickrgallery setid="' . get_post_meta($post->ID, 'per_post_widget_content', true) . '"]'); ?>
  3. johnsalomon
    Member
    Posted 3 years ago #

    I'll give that a shot, thank you. Still learning :)

    (That gives me an error

    Invalid argument supplied for foreach()

    but I think that's related to the theme I'm using. Thanks!

  4. johnsalomon
    Member
    Posted 3 years ago #

    This works, brilliant, thank you so much.

Topic Closed

This topic has been closed to new replies.

About this Topic