• Resolved robertallen

    (@robertallen)


    Hi all,

    I’m trying to display the prior two posts on my main page, but I’m not getting anywhere with this code. Can anyone tell me what I am doing wrong in this code? I show no syntax errors on Dreamweaver, but I get nothing.

    My ultimate goal is to display a section with the latest article, then display the previous 2 articles in another section of the page. I decided to start with the previous 2 articles first just to make sure offset was working.

    <ul>
        <?php
            $args = array( 'numberposts' => '2', 'offset' => 1 );
            $recent_posts = wp_get_recent_posts( $args );
            if( $recent_posts->have_posts() ) : while( $recent_posts->have_posts() ) : $recent_posts->the_post();
    
            echo "<li class='postWrapper'>
    
          <div class='featured_image'><a href=". the_permalink(). "rel='bookmark'>" . the_post_thumbnail('thumbnail') . "</a></div>
          <div class='post_content'>
              <h2 class='postTitle'><a href=" . the_permalink() . "rel='bookmark'>" . the_title() . "</a></h2> 
    
              <div class='post'>" . the_content(__('(more...)')) . "</div>
              <div class='readmore'><a href=". the_permalink() . " rel='bookmark'>Read More...</a></div>
          </div>
    
          <hr class='post_divider' />
    
        	</li>";
    		endwhile; endif; wp_reset_postdata();
        ?>
        </ul>

    Any help would be greatly appreciated.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Thread Starter robertallen

    (@robertallen)

    I get this syntax error.

    Fatal error: Call to a member function have_posts() on a non-object in /home/user/public_html/dzindna/wp-content/themes/dzindna/page.php on line 36

    Thread Starter robertallen

    (@robertallen)

    Thank you much, but that’s what I’m basing my code on…just having issues altering it a bit.

    the example clearly works with a ‘foreach’ loop – not a ‘while(have_posts())’ loop.

    rewrite your code with ‘foreach’ following the example from https://codex.www.ads-software.com/Function_Reference/wp_get_recent_posts#Examples

    or use WP_Query(); https://codex.www.ads-software.com/Class_Reference/WP_Query

    <ul>
        <?php
            $args = array( 'posts_per_page' => '2', 'offset' => 1 );
            $recent_posts = new WP_Query( $args );
            if( $recent_posts->have_posts() ) : while( $recent_posts->have_posts() ) : $recent_posts->the_post();
    
            echo "<li class='postWrapper'>
    
          <div class='featured_image'><a href=". get_permalink($post->ID). "rel='bookmark'>" . get_the_post_thumbnail($post->ID,'thumbnail') . "</a></div>
          <div class='post_content'>
              <h2 class='postTitle'><a href=" . get_permalink($post->ID) . "rel='bookmark'>" . get_the_title() . "</a></h2> 
    
              <div class='post'>" . apply_filters('the_content',get_the_content(__('(more...)'))) . "</div>
              <div class='readmore'><a href=". get_permalink($post->ID) . " rel='bookmark'>Read More...</a></div>
          </div>
    
          <hr class='post_divider' />
    
        	</li>";
    		endwhile; endif; wp_reset_postdata();
        ?>
        </ul>

    with your string concatenation, you will also need to use those functions which return the result instead of echoing it.

    Thread Starter robertallen

    (@robertallen)

    Thank you so much, I believe with that foreach loop I’m on the right track. All I have left to do is make the featured image thumbnail and the excerpt…but I’m stuck as to why it won’t resize.

    My goal is to use the ‘thumbmnail’ default of WordPress…

    This is what I have…
    <a href='".get_permalink($recent->ID[thumbnail])."'>".$recent[post_title]."</a>

    but it still shows up with the non-thumbnail image.

    Here’s my complete new code…

    <ul>
        <?php
            $args = array( 'numberposts' => '2', 'offset' => 1 );
            $recent_posts = wp_get_recent_posts( $args );
            foreach( $recent_posts as $recent ) {
    
            echo "
    
    		<li>
    		<div class='featured_image'>
    		<a href='".get_permalink($recent['ID'])."'>".get_the_post_thumbnail($recent['ID']). "</a>
    		</div>
    		<div class='post_content'>
    		<a href='".get_permalink($recent->ID[thumbnail])."'>".$recent[post_title]."</a>
    
    		</div>
    
    		</li>"
    
    		;
    		}
        ?>
        </ul>

    https://codex.www.ads-software.com/Function_Reference/get_the_post_thumbnail

    you might need to add a thumbnail size?

    are the thumbnail sizes set in your theme?
    https://codex.www.ads-software.com/Post_Thumbnails

    Thread Starter robertallen

    (@robertallen)

    Yep, to save time I set them at 250×250…which work on the blog index page…but I was researching that link you sent me and I couldn’t figure out how to get it inside the foreach loop. It kept displaying the larger version of the image, not the actual resized/cropped thumbnail.

    Also I just realized I was putting it in the wrong area…oops!

    <a href='".get_permalink($recent['ID'])."'>".get_the_post_thumbnail($recent -> ID[thumbnail]). "</a>

    Now it doesn’t even display.

    I even tried ($recent[‘ID’][‘thumbnail’]) and nothing.

    Thread Starter robertallen

    (@robertallen)

    I’m using the default ‘thumbnail’ as my thumbnail size.

    Thread Starter robertallen

    (@robertallen)

    Not even

    the_content($recent[‘ID’]) or get_the_content($recent[‘ID’)

    shows up.

    I’m so frustrated.

    Yeah, quite complicated science from scratch. This is why developers provided extended function reference with code examples.

    Compare these two lines:
    get_permalink( $recent[“ID”] )
    get_the_post_thumbnail( $post->ID, ‘thumbnail’ )

    First works in this context, second not. What do you have to change in second line to get it work in foreach( $recent_posts as $recent )
    loop?

    Thread Starter robertallen

    (@robertallen)

    Ah ha! Thank you, vjpo. Your question was an answer.

    get_the_post_thumbnail( $recent[‘ID’], ‘thumbnail’ )

    Now I just need to get get_the_excerpt($recent[‘ID’]) to work.

    Any ideas? Thank you guys so much for your help so far.

    Thread Starter robertallen

    (@robertallen)

    I even tried

    apply_filters(‘the_excerpt’, $recent[‘ID’]->post_content)

    but it didn’t work.

    Here’s my code so far. Everything works great except the content (which I’ll change to excerpt if I can just get it working)

    $args = array( 'numberposts' => '2', 'offset' => 1 );
            $recent_posts = wp_get_recent_posts( $args );
            foreach( $recent_posts as $recent ) {
    
            echo "
    
    		<li>
    		<div class='featured_image'>
    		<a href='".get_permalink($recent['ID'])."'>".get_the_post_thumbnail( $recent['ID'], 'thumbnail' ). "</a>
    		</div>
    		<div class='post_content'>
    		<h2 class='post_title'><a href='".get_permalink($recent['ID'])."'>".$recent[post_title]."</a></h2>
    
    		".apply_filters('the_content', $recent['ID']->post_content) ."
    		</div>
    
    		</li>"
    
    		;
    		}

    var_dump($recent_posts); after $recent_posts = wp_get_recent_posts( $args );line can tall you what in this array.
    Something like

    Array ( [0] => Array (
            [ID] => 33
            [post_author] => 1
            [post_date] => 2013-02-05 20:01:49
            [post_content] => This is my post content.
            [post_title] => My post title
            [post_excerpt] =>
            [post_status] => publish

    So $recent[‘post_title’] – is title etc…

    If you want to display posts content also, it would be easier to use WP_Query (as alchymyth shown) or get_posts function.

    Thread Starter robertallen

    (@robertallen)

    Thank you all! It’s working great now.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Trying to display latest posts’ is closed to new replies.