I got this code to show my thumbnails images and videos on my blog page, but when i try to use to show the thumbs in another page, it doesnt works.

Function to display images and videos thumbs

function show_blog_posts() {
global $post, $blog, $posts;
$first_vid = '';
$output = preg_match_all('/<iframe.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_vid = $matches [1] [0];

if ($output == '1') {
    echo '<iframe width="100%" height="400" src="';
    echo $first_vid;
    echo '" frameborder="0" allowfullscreen></iframe>';
} else { ?>
            <?php the_post_thumbnail('large');?>

My shortcode

add_shortcode('recent_blog_posts', 'shortcode_recent_blog_posts');

function shortcode_recent_blog_posts($atts) {

        $str = '';
        $args = array( 'numberposts' => 3, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
        $postslist = get_posts( $args );

        foreach ($postslist as $post) : setup_postdata($post); 
            $str .= '<div id ="article-section-box2" class="span3">';
                            $str .= '<div id= "image1" class="image-arcticle">';     
                            $str .= show_blog_posts($post->ID, '');
                            $str .= '</div>'; 
                            $str .='<p class="article_text">'.excerpt(18).'</p>';
                            $str .='</div>';

        return $str;

Is there anything wrong? Thanks

  • 1
    Turn on debugging so you can learn more than "it doesnt work".
    – Wyck
    Jan 28, 2013 at 15:46
  • debugging information
    – s_ha_dum
    Jan 28, 2013 at 15:48
  • Debugging is informing : Notice: Trying to get property of non-object in C:\xampp-portable\htdocs\wordpress\wp-content\themes\Pabloswebsite\functions.php on line 28 and 29 Jan 28, 2013 at 16:20

1 Answer 1


You are passing show_blog_posts two parameters...

$str .= show_blog_posts($post->ID, '');

... but you have defined it to accept none...

function show_blog_posts() {

There seems to be some disconnect between your two blocks of code. I'd suggest actually passing the $post object the way that you are trying to.

function show_blog_posts($post = NULL) {
    global $blog, $posts;
    if (empty($post)) {
        global $post;
    // the rest of your function

No idea what that second parameter is supposed to be.

  • I did what u said but keeps "not working" to me yet :( sry for my english Jan 28, 2013 at 16:13
  • What changed between when you posted that comment and when you accepted the answer?
    – s_ha_dum
    Jan 30, 2013 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.