• Resolved Rob

    (@roballred)


    i have a simple shortcode that i want to return a permalink with some h2 tags around it. below you can see my two samples the first one just returns a string with and the other gets the permalink with <h2>’s around it. I put the <h2>’s around the test case to make sure they werent getting filtered out. any suggestions?

    function demolistposts_handler() {
    $retVal = ‘<h2>hello earthlings</h2>;
    return $retVal;
    }

    this returns what i would expect but the following returns
    the link with no tags
    function demolistposts_handler() {
    $temp_link = the_permalink();
    $retVal = ‘<h2>$temp_link</h2>;
    return $retVal;
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • http://php.net/manual/en/language.types.string.php

    try either:

    $retVal = '<h2>'.$temp_link.'</h2>';

    or:

    $retVal = "<h2>$temp_link</h2>";

    also, you need to use get_permalink(), possibly like so get_permalink($post->ID), for which you might need to set $post as a global variable: global $post;

    i.e. the full code:

    function demolistposts_handler() {
    global $post;
    $temp_link = get_permalink($post->ID);
    $retVal = '<h2>'.$temp_link.'</h2>';
    return $retVal;
    }
    Thread Starter Rob

    (@roballred)

    ok that worked, seems like problem was where i was getting the permalink from. the value from get_permalink($post->ID) works and the value from the_permalink() doesn’t. I don’t really understand the difference in the return strings, but know i have a way that works. thank you.

    the value from get_permalink($post->ID) works and the value from the_permalink() doesn’t

    the_permalink echos the value (out to the screen), while get_permalink returns the value for further use in strings, for instance.
    example:
    $link = get_permalink($post->ID);

    http://codex.wordpress.org/Function_Reference/the_permalink
    http://codex.wordpress.org/Function_Reference/get_permalink

    there are many functiosn in wordpress where the function name starts with the_ which echo the result (direct output); and equivalent functions starting with get_ which return the result (to use in strings, for instance).

    Thread Starter Rob

    (@roballred)

    thanks again.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘shortcode return string problems’ is closed to new replies.