WordPress.org

Ready to get started?Download WordPress

Forums

shortcode output always appearing at top of page content (15 posts)

  1. wkor
    Member
    Posted 4 years ago #

    Just like srshakya over here, my shortcode output won't appear where I put it, but rather at the top of the content (top of the post/page content).

    Here is the page: http://img29.imageshack.us/img29/7155/bloguserspage.jpg

    Here it is in the editor: http://img32.imageshack.us/img32/2594/bloguserspageediting.jpg

    And here is the relevant code from functions.php:

    function abm_blog_users() {
    $blogusers = get_users_of_blog(); //gets registered users
    if ($blogusers) {
    foreach ($blogusers as $bloguser) {
    $user = get_userdata($bloguser->user_id); //gets the actual data about each user
    echo "<a href=\"".$user->user_url."\">".$user->display_name."</a> (<a href=\"";
    bloginfo('url');
    echo "/author/".$user->user_nicename."\">";
    echo "#userposts</a>)<br />";
    }
    }
    }
    add_shortcode('blogusers', 'abm_blog_users');

    Desperately hope you have some insight. And for those wondering about #userposts, the function isn't done yet. =)

  2. wkor
    Member
    Posted 4 years ago #

    It appears the thread [resolved] Plugin called via shortcode appears at the wrong place on post found a solution to the problem.

    I'm not quite sure how to implement that into my code, though. Any help?

  3. 610m
    Member
    Posted 4 years ago #

    I have a similar problem and can't find an answer but in my travels I think I found the answer to yours... You cannot use "echo" in a shortcode fucntion, you must use "return". Everyone else that I've seen use this has their problem resolved when they stop echoing and start returning.

  4. daricedotorg
    Member
    Posted 4 years ago #

    I have the same problem and the solution mentioned in the other post does not work for me.

    I have a very simple shortcode function:

    function werklinks() {
         return wp_list_pages("title_li=&child_of=8d&depth=1");
    }
    add_shortcode('werk', 'werklinks');

    The list of pages links appears on top of the content. Any idea what is wrong with my code?

  5. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    @daricedotorg
    in your function you are calling wp_list_pages, which will be executed immedeately when your functions.php gets processed, before the content and will echo its list.

    you probably want to have your function return the page list as a string -
    look into the 'echo' variable of
    http://codex.wordpress.org/Template_Tags/wp_list_pages

    function werklinks() {
         return wp_list_pages("title_li=&child_of=8d&depth=1&echo=0");
    }
    add_shortcode('werk', 'werklinks');

    then where you want to use this funtion, you could use:
    $pageslinks = werklings(); echo $pageslinks;

  6. daricedotorg
    Member
    Posted 4 years ago #

    Thanks @alchymyth but it needs to be a shortcode. It needs to be added in the content and using a php plugin is no an option. The website needs to be as dynamic and simple as possible for the client.

    I'm thinking the error could lie in the fact that wp_list_pages() has an echo in it so that's while when used inside a function for shortocde it will output before the content. Not sure tho...

  7. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    who is talking about a plugin?

    as i said - the 'echo' parameter needs to be set to '0' for the list not to echo, but to be returned as string.
    but the point is that your shortcode function needs to do something with this string.

    what is your shortcode function like?

    EDIT:
    forget the above, and what i said about using the function.
    i did not pay enough attention that you are trying to make a shortcode:
    this should really work as it is:
    (remove the typo - the 8d in 'child_of=8d')

    function werklinks() {
         return wp_list_pages("title_li=&child_of=8&depth=1&echo=0");
    }
    add_shortcode('werk', 'werklinks');
  8. daricedotorg
    Member
    Posted 4 years ago #

    Thanks! echo=0 does it indeed! And then putting put it in a variable first. Without variable it won't work.

    Working code:

    function werklinks() {
    	$list = wp_list_pages("title_li=&child_of=8d&depth=1&echo=0");
            return $list;
    }
    add_shortcode('werk', 'werklinks');
    ?>
  9. AndyiBM
    Member
    Posted 3 years ago #

    Hi,

    I have a similar problem which doesn't seem to get resolved by using 'return'...

    I just want to include some code in a php file, but again it always appears before the post content, rather than in the middle where I put the shortcode.

    Here's my code in the functions file:

    function get_file_content() {
        include( TEMPLATEPATH . '/my_file.php' );
        return;
    }
    add_shortcode('show-content', 'get_file_content');

    Any help appreciated - cheers.

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

    what is the code in my_file.php?

  11. AndyiBM
    Member
    Posted 3 years ago #

    Hi alchymyth,

    It's just some regular HTML lists with some php calls to import tables from a table plugin (do you need me to paste?)

    The content displays correctly, just in the wrong position.

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

    that is what i thought - the html outputs immmediately; try to put all the html output into a string and return that string as the result of your included file.

  13. AndyiBM
    Member
    Posted 3 years ago #

    Hi Alchymyth,

    This is my code now:

    function get_file_content() {
        $filestring = (include TEMPLATEPATH . '/my_file.php') ;
        return $filestring;
    }
    add_shortcode('show-content', 'get_file_content');

    but it still makes no difference to the order (apart from outputting an additional '1' character).

    Sorry - my PHP ain't great - I'm assuming my syntax is ok...

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

    you would also need to make changes in my_file.php

  15. AndyiBM
    Member
    Posted 3 years ago #

    Er... not sure I follow. Change my_file.php how?

    You may need to spoonfeed me a little more... sorry - this might be getting too technical for me now :(

Topic Closed

This topic has been closed to new replies.

About this Topic