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
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 http://codex.wordpress.org/Function_Reference/wp_get_recent_posts#Examples
or use WP_Query(); http://codex.wordpress.org/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.
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>
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.
I’m using the default ‘thumbnail’ as my thumbnail size.
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?
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.
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.
Thank you all! It’s working great now.