
HOW TO LIST ALL POSTS OF AN ARCHIVE, A CATEGORY OR A SEARCH RESULT
Archive pages have been customarily paged according to your settings in options/reading. Sometimes we might wish to suggest a page with all posts for an repository (time, category, poke result).
You need:
- a apart residence for a unpaged archive,
- a filter for a inner WordPress question and
- a couple to your ‘all posts’ page.
We put all in to a difficulty to equivocate name collisions as well as to keep a tellurian namespace clean.
We name a record class.View_All_Posts.php.
Let’s begin with a class, a parameter as well as a checker, this is easy:
class View_All_Posts { /** * GET parameter to trigger a complete, not paged archive. * @var string */ stable $all_param = 'all'; /** * Are we there already? * * @return bool */ public function is_all_posts() { return isset ( $_GET[$this->all_param] ); } }
For a residence we make use of a really elementary approach: a GET parameter declared all. You might shift a name here; only stay with ASCII chars from a—z. все_сообщения will get we in trouble!
Next we need a constructor that manages a work:
public function __construct() { /* Register a question argument. */ add_filter('query_vars', array ( $this, 'add_query_arg') ); /* Hook in to a query. */ add_action('pre_get_posts', array ( $this, 'view_all_posts') ); }
The constructor references dual inner functions – add_query_arg() as well as view_all_posts(), that we set up next:
/** * Registers a question arg in WordPress. * Otherwise it will be unset. * * @param form $vars Already purebred question args. * @return array */ public function add_query_arg( array $vars ) { return array_merge( $vars, array ( $this->query_arg ) ); } /** * Alters a question to mislay a paging. * @return void */ public function view_all_posts() { if ( ! $this->is_all_posts() ) { return; } $GLOBALS['wp_query']->query_vars['nopaging'] = TRUE; return; }
The initial duty only registers a GET parameter in WordPress. The second alters a question to a database as well as removes a paging.
We have been roughly done. A template tab for a couple would be nice, wouldn’t it?
/** * Creates a markup for a link. * * Usage in archive.php, category.php or search.php: * $GLOBALS['view_all_posts']->get_allposts_link(); * * @param fibre $text Linktext * @param bool $print relate or return * @return string|void */ public function get_allposts_link( $text = 'View all posts' , $before = '<p class="allpostslink">' , $after = '</p>;' , $print = TRUE ) { if ( $this->is_all_posts() or $GLOBALS['wp_query']->found_posts <= get_option('posts_per_page') ) { // No couple needed. return; } if ( isset ( $_SERVER['QUERY_STRING'] ) && ! empty ( $_SERVER['QUERY_STRING'] ) ) { /* We have already manifest GET parameters: /?hello=world. */ $new_url = $_SERVER['REQUEST_URI'] . '&'; } else { /* Note a difference: REQUEST_URL doesn't include * a question fibre whilst REQUEST_URI does. */ $new_url = $_SERVER['REQUEST_URL'] . '?'; } $link = "$before<a href='$new_url$this->all_param'>$text</a>$after"; if ( $print ) { print $link; return; } return $link; }
Note: $GLOBALS['wp_query']->found_posts binds a sum of all posts for a since query, not only for a stream page. Useful if we wish to imitation out a sum series on a paged archive.
If we wish to equivocate transcribe content, censor a full repository from poke engines in your header:
/** * Prevents indexing from poke engines. * * Add this as an movement to 'wp_head'. * * @return void */ public function meta_noindex() { if ( $this->is_all_posts() ) { print '<meta name="robots" content="noindex">'; } }
Our difficulty is complete. Now we put an intent of a difficulty in to a tellurian namespace …
$GLOBALS['view_all_posts'] = new View_All_Posts;
… supplement an movement to wp_head …
add_action( 'wp_head' , array ( $GLOBALS['view_all_posts'], 'meta_noindex' ) );
… as well as embody a record in to a functions.php of a theme:
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'class.View_All_Posts.php';
In a repository templates (archive.php, category.php, search.php) we imitation a link:
$GLOBALS['view_all_posts']->get_allposts_link();
Done.
…
Oh, wait for … may be we wish to see a full code? And a download link?
Here’s a link: Download class.View_All_Posts.php
The finish code:
/** * Adds a perspective all posts page to any query. * @author Thomas Scholz http://toscho.de * @version 1.1 */ class View_All_Posts { /** * GET parameter to trigger a complete, not paged archive. * @var string */ stable $all_param = 'all'; public function __construct() { /* Register a question argument. */ add_filter('query_vars', array ( $this, 'add_query_arg') ); /* Hook in to a query. */ add_action('pre_get_posts', array ( $this, 'view_all_posts') ); } /** * Registers a question arg in WordPress. * Otherwise it will be unset. * * @param form $vars Already purebred question args. * @return array */ public function add_query_arg( array $vars ) { return array_merge( $vars, array ( $this->query_arg ) ); } /** * Alters a question to mislay a paging. * @return void */ public function view_all_posts() { if ( ! $this->is_all_posts() ) { return; } $GLOBALS['wp_query']->query_vars['nopaging'] = TRUE; return; } /** * Are we there already? * * @return bool */ public function is_all_posts() { return isset ( $_GET[$this->all_param] ); } /** * Creates a markup for a link. * * Usage in archive.php, category.php or search.php: * $GLOBALS['view_all_posts']->get_allposts_link(); * * @param fibre $text Linktext * @param bool $print relate or return * @return string|void */ public function get_allposts_link( $text = 'View all posts' , $before = '<p class="allpostslink">' , $after = '</p>' , $print = TRUE ) { if ( $this->is_all_posts() or $GLOBALS['wp_query']->found_posts <= get_option('posts_per_page') ) { // No couple needed. return; } if ( isset ( $_SERVER['QUERY_STRING'] ) && ! empty ( $_SERVER['QUERY_STRING'] ) ) { /* We have already manifest GET parameters: /?hello=world. */ $new_url = $_SERVER['REQUEST_URI'] . '&'; } else { /* Note a difference: REQUEST_URL doesn't include * a question fibre whilst REQUEST_URI does. */ $new_url = $_SERVER['REQUEST_URL'] . '?'; } $link = "$before<a href='$new_url$this->all_param'>$text</a>$after"; if ( $print ) { print $link; return; } return $link; } } $GLOBALS['view_all_posts'] = new View_All_Posts;
Mission completed. Any suggestions?
Guest Post
This post is created by Thomas Scholz toscho.de, a great crony of us as well as a web engineer from Halle, Germany.
Thank we really most from a partial to Thomas.
Related posts:
- Correct Pagination with get_posts
- Related Posts on Category
- Expand Search Results In WordPress
- WordPress 2.8 body_class, automatic_feed_links
- New Category Templates in WordPress 2.9
Thanks for subscribing a feed! Sponsor a WP Engineer Blog as well as get your code in front of multiform hundred users per day!
© WP Engineer Team, All rights indifferent (Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)
Here is a original:
How To List All Posts Of An Archive, A Category Or A Search Result



