• I am trying to get the three most recent posts, but my code doesn’t appear to be working. Can please something tell me what I’ve done wrong?

    <?php
    $args = array(
    post_type' => 'story',
    'offset' => 1,
    'numberposts' => 3,
     ));
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $post ){
    	echo '<div class="awt-static-tweet"><a href="' . get_permalink($post["ID"]) . '" title="Look '.$post["post_title"].'" >' .   $post["post_title"].'</a> </div>
            <div class="awt-static-divider"></div> ';
    	}
    ?>

    Thanks,

    Jon

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hmm, make sure your arguments are correct, I can’t see anything wrong with the code. What output are you getting?

    Thread Starter JSauce16

    (@jsauce16)

    I’m getting nothing! http://www.athleteswhotweet.com/test

    I see, let’s try to troubleshoot this a bit.

    Check your $args, I see that post_type only has one single quote (missing opening ‘ ) on it, but maybe that’s just a typo in this forum post. Also, make sure that ‘post_type’ => ‘story’ is actually a custom post type, maybe ‘story’ is actually a Category/Term or Custom taxonomy you added.

    Check if any values are actually being returned in your $recent_posts. do something like print_r($recent_posts) or var_dump($recent_posts) to see if anything is returned.

    Test different variations of your $args, like taking out the offset or numberpost. This will help us determine where the problem lies.

    Maybe try using a different wordpress function to get you posts like get_posts.

    Thread Starter JSauce16

    (@jsauce16)

    Ah ha! I thought I had the singular version as “story” but I changed it to “stories” and I’m rockin now! Thanks for the help, simple oversight on my part!

    Thread Starter JSauce16

    (@jsauce16)

    I do have a new challenge now though, i want to pull one tweet from another post type. I am also trying to pull the featured image in a certain size (128×128) and a custom field called “name”. I put placed holders in the HTML below, but I’m not sure how to edit this to call those fields.

    <?php
    	$args = array(
    								'post_type' => 'tweets',
    
    								'numberposts' => 30,
     );
    
    	$recent_posts = wp_get_recent_posts( $args );
    	foreach( $recent_posts as $post ){
    		echo '<li class="awt-slide">[INSERT THUBNAIL]<div class="awt-tweet"><h3>Tweet of the Day</h3><p><a href="' . get_permalink($post["ID"]) . '" title="Look '.$post["post_title"].'" >' .   $post["post_title"].'</a></p><span class="awt-author">CUSTOM FIELD AUTHOR</span> </div></li> ';
    	}
    ?>

    If I understood it correctly, the wp_get_recent_posts() for tweets is working and you just need to get the featured image of that post?

    Well, first we need to make sure that your theme supports post thumbnails by adding this to your functions.php

    if ( function_exists( 'add_theme_support' ) ) {
      add_theme_support( 'post-thumbnails' );
    }

    you can use the_post_thumbnail( $size, $attr ) function to display your images but must be done within the loop.

    alternatively you can call get_post_thumbnail_id() to get the ID and use wp_get_attachment_url() to get the url

    ex:

    //I haven't tested this yet
    $thumb_id = get_post_thumbnail_id($post['ID']);
    $url = wp_get_attachment_url($thumb_id);
    echo "<img src='".$url."' />
    //I suggest using timthumbs to crop the pictures incase they're too big

    Read http://codex.wordpress.org/Post_Thumbnails For more information

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get 3 Most Recent Posts’ is closed to new replies.