Skip to content

Using p2p_list_posts()

scribu edited this page Apr 19, 2012 · 8 revisions

p2p_list_posts() is an utility function that, as it's name suggests, outputs a list of posts.

It accepts either a WP_Query instance or a plain array of posts.

Here's how we could rewrite the Basic usage example:

<?php p2p_list_posts( $connected, 'before_list=<h3>Related posts</h3><ul>' ); ?>

To use an ordered list instead of an unordered list:

<?php p2p_list_posts( $connected, 'before_list=<h3>Related posts</h3><ol>&after_list=</ol>' ); ?>

To output links directly, without wrapping them in an unordered list, use:

<?php p2p_list_posts( $connected, 'before_list=&after_list=&before_item= &after_item=' ); ?>

You can also pass an array instead of a long string:

<?php
p2p_list_posts( $connected, array(
	'before_list' => '<h3>Related posts</h3><ol>',
	'after_list'  => '</ol>',
	'before_item' => '<li>',
	'after_item'  => '</li>',
) );
?>

You can also use separator instead of before_item and after_item:

<?php
p2p_list_posts( $connected, array(
	'before_list' => '<h3>Related posts</h3><p>',
	'after_list'  => '</p>',
	'separator'   => ', ',
) );
?>

(note that if separator is defined, then before_item and after_item are unnecessary and will be ignored regardless of their presence)