Recent Posts

From Habari Project

Jump to: navigation, search

It's really simple to get a listing of recent posts to output in your Habari theme. This page describes how to manually add recent posts support by editing your theme.

There are just two steps to add recent posts to your blog.

First, you need to make an array of recent posts available in your theme template. Add this code to the theme.php file for your theme inside the add_template_vars function:

 $this->assign('recent_posts',
                Posts::get( array('limit'=>5, 'status'=>'published', 'orderby'=>'pubdate DESC' ) ) );


This tells Habari to store the 5 most recent published posts into a variable called $recent_posts that will be accessible in your template.

Alternately if you want to show the previous posts in relation to the page the visitor is on(ie, if showing 5 posts per page, and on /page/2, it would show posts 11-15) you can use this:

//visiting page/2, /3 will offset to the next page of posts in the sidebar
		$page =Controller::get_var( 'page' );
		$pagination =Options::get('pagination');
		if ( $page == '' ) { $page = 1; }
		$this->assign( 'recent_posts', Posts::get(array ( 'status' => 'published','content_type' => 'entry', 'offset' => ($pagination)*($page), 'limit' => 5,  ) ) );

Note: above function was previously in Mzingi, but removed in deference to using Core Blocks


Second, add this to your theme's template where you want the comment listing to appear:

<h2>Blog articles</h2>
            <ul>
                <?php
                    foreach ($theme->recent_posts as $post) {
                        echo '<li><a href="', $post->permalink, '">',
                        $post->title, '</a></li>';
                    }
 
                ?>
            </ul>

This loops through each of the post objects in the array of recent posts using a foreach loop and prints them. You can edit the HTML to change how the comments are displayed.

Personal tools
This is a cached copy of the requested page, and may not be up to date.

Sorry! This site is experiencing technical difficulties.
Try waiting a few minutes and reloading.

(Can't contact the database server: Access denied for user 'habari'@'localhost' to database 'habari_wiki' (localhost))


You can try searching via Google in the meantime.
Note that their indexes of our content may be out of date.