I’m trying to add a sidebar loop to list 3 news post.
I’m using thematic framework and since I don’t find any hook to use I first write this function in functions.php
function sideloop(){
global $post;
echo '<div id="side-News"><ul>';
// The News Query
$args = array(
'numberposts' => 3,
'post_type' => 'post',
'category' => '123',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish' );
$news = get_posts( $args );
// The Loop
foreach ($news as $post) : setup_postdata($post);
echo '<li class="prova">';
the_title();
echo '</li>';
endforeach;
// Reset Post Data
wp_reset_postdata();
echo '</ul></div>';
}
Than I load a plugin to write php code in the widget and i write inside a widget in the primary aside widget area:
<?php
if (function_exists('sideloop')) {
sideloop();
} ?>
I expect to get this result
<div id="main">
<div id="container">...</div>
<div id="primary">
<ul class="xoxo">
<li id="execphp-4" class="widgetcontainer widget_execphp">
<div id="side-News">
<li class="prova">1st Post</li>
<li class="prova">2nd Post</li>
<li class="prova">3rd Post</li>
</div>
</li>
</ul>
</div>
<div id="secondary">...</div>
</div>
Unfortunately it seems not working right and I get this html output
<div id="container">
<div id="primary" class="aside main-aside">
<ul class="xoxo">
<li id="execphp-4" class="widgetcontainer widget_execphp">
<div id="side-News"></div>
<li class="prova"></li>
<li class="prova"></li>
<li class="prova"></li>
<li class="prova"></li>
<li class="prova"></li>
<li class="prova"></li>
</ul>
</div>
</div>
</div>
<div id="secondary" class="aside main-aside">
- First of all the function doesn’t get me 3 post but a lot more.
- The posts of the sidebar-loop are empty.
- The loop is outside the I wanted to be in.
- The “sidebar-loop” pull the “secondary” div outside the main wrapper of the framework.
Anyone can help?
Thank you guys.
Bye
Carletto
PS: I’ve already tried using WP_query instead of get_posts but I get the same results. Please help!
EDIT
Wondering how I can retrieve an error message (like “Sorry, no news”) when my query is empty… trying some if thing but I’m not good enough 😉
Code corrected with Chip’s tips and working!
Thanks
Bye
I think the problem is that you’re calling
get_post()
(note: singular) instead ofget_posts()
(note: plural).EDIT
Other problems:
get_posts()
function uses thecategory
array key, rather thancategory_name
. Thecategory
array key expects category ID, rather than category slug.<li>...</li>
) properly (i.e. within<ul></ul>
tags)