Sidebar loop – Thematic Framework

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:

Read More
<?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">
  1. First of all the function doesn’t get me 3 post but a lot more.
  2. The posts of the sidebar-loop are empty.
  3. The loop is outside the I wanted to be in.
  4. 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

Related posts

Leave a Reply

1 comment

  1. I think the problem is that you’re calling get_post() (note: singular) instead of get_posts() (note: plural).

    EDIT

    Other problems:

    • The get_posts() function uses the category array key, rather than category_name. The category array key expects category ID, rather than category slug.
    • You’re not wrapping your looped list items (<li>...</li>) properly (i.e. within <ul></ul> tags)