• Resolved Rubyfire

    (@rubyfire)


    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Rubyfire

    (@rubyfire)

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

    https://underyourskintattoos.com/contact

    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?

    Thread Starter Rubyfire

    (@rubyfire)

    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.

    Thread Starter Rubyfire

    (@rubyfire)

    thank you very much man… very very much…

    Thread Starter Rubyfire

    (@rubyfire)

    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');

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘shortcode displaying where it pleases not where it is put’ is closed to new replies.