WordPress tag: get_posts. How to list Posts in one category, with offset?

Usage:

<?php get_posts(‘arguments’); ?>

Examples:

Posts list in one category with offset:

If you have your blog configured to show just one post on the front page, but also want to list links to the previous five posts in category ID 1, you can use this:


<ul>

<?php

global $post;

$myposts = get_posts('numberposts=5&offset=1&category=1');

foreach($myposts as $post) :

setup_postdata($post);

?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endforeach; ?>

</ul>

Parameters:

$category

(integer) (optional) Only show posts from this category ID. Making the category ID negative (-3 rather than 3) will show results not matching that category ID. Multiple category IDs can be specified by separating the category IDs with commas – but an array of IDs does not work.

Default: None

$category_name

(string) (optional) Only show posts from this category name or category slug.

Default: None

Note: With use of the offset, the above query should be used only on a category that has more than one post in it, otherwise there’ll be no output.

===========================

Read more in WordPress Codes: http://codex.wordpress.org/Template_Tags/get_posts

===========================

My example:

For HYG website, I created 3 categories: Events(ID#11), Updates(ID#12), News (ID#13), and three respective pages that display all the posts of each category. (See how I do that here.) Now I want to display a list of posts that belong to the same category in each page. In the list, each post will be displayed with its title, an excerpt, and a thumbnail image.

Here’s the code that I put in my theme’s function.php

function display_post_list() {

$post = $wp_query->post;

if ( in_category('11') ) { ?>

<h1>Past Events:</h1>

<div class="post_list">

<ul>

<?php

global $post;

$myposts = get_posts('numberposts=10&offset=1&category=11');

} elseif ( in_category('12') ) { ?>

<p>Other News:</p>

<div class="post_list">

<ul>

<?php

global $post;

$myposts = get_posts('numberposts=5&offset=1&category=12');

} elseif ( in_category('13') ) { ?>

<p>Other Updates on Art:</p>

<div class="post_list">

<ul>

<?php

global $post;

$myposts = get_posts('numberposts=5&offset=1&category=13');

} else { ?>

<h1>Other blog posts:</h1>

<div class="post_list">

<ul>

<?php

global $post;

$myposts = get_posts('numberposts=10&offset=1&category=-11,-12,-13');

}

foreach($myposts as $post) :

setup_postdata($post);

?>

<li>

<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>

<p><?php the_excerpt() ?></p>

<p><a href=”<?php the_permalink() ?>” rel=”bookmark”><?php if ( function_exists( 'get_the_image' ) ) get_the_image(); ?></a></p>

</li>

<?php endforeach; ?>

</ul>

</div>

<?php

}

===========================

Also, read about How to have Previous/Next Post in Category on the Single Post page here:

http://wordpress.org/support/topic/269003?replies=30

Advertisements

2 Comments

  1. tia
    Posted September 6, 2010 at 11:49 pm | Permalink

    I wish I knew where to put that code.

  2. Posted September 18, 2010 at 3:49 pm | Permalink

    how can i combine it to make a shortcode ?? got any idea?

%d bloggers like this: