1

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">
  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

1
  • Props for a very well-described question. Oct 24, 2011 at 14:39

1 Answer 1

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)
1
  • Well I thought it was the framework itself to put my <li></li> elements between <ul> tags... Well thank you very much... even for those get_postS and ID things... Oct 24, 2011 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.