WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Short Code for Author Box always displays at top of post and incorrectly (6 posts)

  1. k00ij
    Member
    Posted 4 years ago #

    Hey guys - I am working on a simple short code [authorbox] to display an author box when called at the bottom of the post. I don't want this in all posts, just when I call the short code.

    function my_author_box() { 	
    
    $author_name = the_author();
    $site_link = the_author_meta('user_url');
    $author_desc = the_author_meta('description');
    $facebook_link = the_author_meta('aim');
    $twitter_link = the_author_meta('yim');
    $author_img_link = the_author_meta('jabber');
    
    $return_text = '<div id="author-info"><div id="author-image"><a href="'.$site_link.'"><img src="'.$author_img_link.'" alt="'.$author_name.'" title="'.$author_name.'"/></a></div>
    <div id="author-bio"><h4>Written by '.$author_name.'</h4><p>'.$author_desc.'</p></div></div>';
    
    return $return_text;
    
    }
    add_shortcode('authorbox', 'my_author_box');
    ?>

    *note - I'm using the jabber, aim, and yahoo im fields to store twitter, facebook, and a link to an image. They are all in "http://www.sitelink.com" format.

    Few problems:
    1. I get the basic meta info from listed at the very top of my posts, even though the [authorbox] code is placed at the very bottom. This will not display the CSS or image etc, just straight text with no spaces in between.

    2. I ALSO get the author info at the bottom which is formatted according to my CSS. Several of the data items do not display, even though they appear correct at the top of the post.

    Any ideas? Desperately trying to get this to work.

  2. k00ij
    Member
    Posted 4 years ago #

    btw - that function is placed in functions.php

  3. k00ij
    Member
    Posted 4 years ago #

    Finally figured it out - needed to use get_the_author_meta instead of just the_author_meta

    function my_author_box() { 	
    
    $author_name = the_author();
    $site_link = get_the_author_meta('user_url');
    $author_desc = get_the_author_meta('description');
    $facebook_link = get_the_author_meta('aim');
    $twitter_link = get_the_author_meta('yim');
    $author_img_link = get_the_author_meta('jabber');
    
    $return_text = '<div id="author-info"><div id="author-image"><a href="'.$site_link.'"><img src="'.$author_img_link.'" alt="'.$author_name.'" title="'.$author_name.'"/></a></div><div id="author-bio"><h4>Written by '.$author_name.'</h4><p>'.$author_desc.'</p></div></div>';
    
    return $return_text;
    
    }
    add_shortcode('authorbox', 'my_author_box');
  4. Nio
    Member
    Posted 3 years ago #

    You seem to have the answer to my problem!

    I have a function, that it’s works, but used with a shortcode behave different... the output it's appearing at the top of the post, and not where I was placing the shortcode.

    I did try placing the return, but keep gating errors. Can you please help me with this? I’m just a beginner in php…
    The function is here: http://www.nullcore.nl/uploads/mess.txt
    Thanks!

  5. mcallaghan
    Member
    Posted 3 years ago #

    Hi

    I have been reading this and many more and I am having the same problem. My shortcode is displaying at the top.

    However, I cannot find how to change the echo to a return as it already is using return.

    It is a basic RSS reader that uses the built-in wp_rss function.

    I found it on a site and it is working, but as mentioned displaying it aty the top.

    Here is the code added to the functions.

    //This file is needed to be able to use the wp_rss() function.
    include_once(ABSPATH.WPINC.'/rss.php');
    
    function readRss($atts) {
    	extract(shortcode_atts(array(
    		"feed" => 'http://',
    		"num" => '1',
    		"echo" => '0',
    		), $atts));
    	return wp_rss($feed, $num);
    }
    
    add_shortcode('rss', 'readRss');

    as you can see, it already uses return. Any advice would be greatly appreciated.

    Thanks

    Paddy

  6. weex
    Member
    Posted 3 years ago #

    Answer to your problem:

    http://codex.wordpress.org/Shortcode_API#Output

    "The return value of a shortcode handler function is inserted into the post content output in place of the shortcode macro. Remember to use return and not echo - anything that is echoed will be output to the browser, but it won't appear in the correct place on the page."

Topic Closed

This topic has been closed to new replies.

About this Topic