WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [closed] shortcode displaying where it pleases not where it is put (7 posts)

  1. Rubyfire
    Member
    Posted 2 years ago #

    I created a VERY simple little shortcode so far using the codex example as I'm trying to create it more elegantly but have hit one serious problem. The damn thing just won't show up where I place it. Instead of rendering inside the post content wrapper, it displays above it. Every time, no matter the post.

    Heres what I got
    in functions PHP:

    function wptuts_recentposts($atts, $content = null){  
    
    $recent_posts = wp_get_recent_posts();
    foreach( $recent_posts as $recent ){
    		echo '
    <li><a>' .   $recent["post_title"].'</a> </li>
     ';
    	}
    
    }
    add_shortcode('recent_posts', 'wptuts_recentposts');

    in content area in page editor:
    [recent_posts]

    anybody got a clue in heck what could be going on? I'm using a template called Karma for this site.

  2. Rubyfire
    Member
    Posted 2 years ago #

    Here is the link for reference.. You can check out exactly whats going on..

    http://underyourskintattoos.com/contact

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

    shortcode must not contain echo but concatenate the output and return it at the end.

    example:

    function wptuts_recentposts($atts, $content = null){
    $output = '';
    $recent_posts = wp_get_recent_posts();
    foreach( $recent_posts as $recent ){
    		$output .= '
    <li><a>' .   $recent["post_title"].'</a> </li>
     ';
    	}
    return $output;
    }
    add_shortcode('recent_posts', 'wptuts_recentposts');

    besides - are you adding the <ul> tags outside of the code?

  4. Rubyfire
    Member
    Posted 2 years ago #

    well I was just making sure it worked correctly... and then was like what? But I did... I put ul's div's... w/e to see if that was the deal.

  5. Rubyfire
    Member
    Posted 2 years ago #

    thank you very much man... very very much...

  6. Rubyfire
    Member
    Posted 2 years ago #

    Correct me if I'm wrong but shouldn't this be outputting the excerpts to my posts?

    function wptuts_recentposts($atts, $content = null){
    global $post;
    $list_start = '<ul class="recent_posts">';
    $list_end = '</ul>';
    $output = '';
    $recent_posts = wp_get_recent_posts();
    foreach( $recent_posts as $current_post ){
    get_post($current_post["ID"]);
    $excerpt = $postid->post_excerpt;
    		$output .= '
    <li>
    	<div class="recent_wrapper">
    		<h4><a href=' . get_permalink($current_post["ID"]) .  '>' .   $current_post["post_title"].'</a></h4>'. get_the_post_thumbnail($current_post["ID"], array(75,75)) . $excerpt . '
    	</div>
    </li>';
    	}
    return $list_start . $output . $list_end;
    
    }
    add_shortcode('recent_posts', 'wptuts_recentposts');
  7. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic