How To List All Posts Of An Archive, A Category Or A Search Result
June 26th, 2010 by Michael • WordPress Plugins • 7 Comments
Archive pages are usually paged according to your settings in options/reading. Sometimes you may want to offer a page with all posts for an archive (time, category, search result).You need:
- a separate address for the unpaged archive,
- a filter for the internal WordPress query and
- a link to your ‘all posts’ page.
We name the file
class.View_All_Posts.php
.Let’s start with the class, the parameter and a checker, this is easy:
- class View_All_Posts
- {
- /**
- * GET parameter to trigger a complete, not paged archive.
- * @var string
- */
- protected $all_param = 'all';
- /**
- * Are we there already?
- *
- * @return bool
- */
- public function is_all_posts()
- {
- return isset ( $_GET[$this->all_param] );
- }
- }
all
. You may change the name here; just stay with ASCII chars from a—z. все_сообщения will get you in trouble!Next we need a constructor that manages our work:
- public function __construct()
- {
- /* Register the query argument. */
- add_filter('query_vars', array ( $this, 'add_query_arg') );
- /* Hook into the query. */
- add_action('pre_get_posts', array ( $this, 'view_all_posts') );
- }
add_query_arg()
and view_all_posts(), which we build next:- /**
- * Registers the query arg in WordPress.
- * Otherwise it will be unset.
- *
- * @param array $vars Already registered query args.
- * @return array
- */
- public function add_query_arg( array $vars )
- {
- return array_merge( $vars, array ( $this->query_arg ) );
- }
- /**
- * Alters the query to remove the paging.
- * @return void
- */
- public function view_all_posts()
- {
- if ( ! $this->is_all_posts() )
- {
- return;
- }
- $GLOBALS['wp_query']->query_vars['nopaging'] = TRUE;
- return;
- }
We are almost done. A template tag for the link would be nice, wouldn’t it?
- /**
- * Creates the markup for the link.
- *
- * Usage in archive.php, category.php or search.php:
- * $GLOBALS['view_all_posts']->get_allposts_link();
- *
- * @param string $text Linktext
- * @param bool $print echo 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 link needed.
- return;
- }
- if ( isset ( $_SERVER['QUERY_STRING'] )
- && ! empty ( $_SERVER['QUERY_STRING'] )
- )
- {
- /* We have already visible GET parameters: /?hello=world. */
- $new_url = $_SERVER['REQUEST_URI'] . '&';
- }
- else
- {
- /* Note the difference: REQUEST_URL doesn't include
- * the query string while 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['wp_query']->found_posts
holds the sum of all posts for a given query, not just for the current page. Useful if you want to print out the total number on a paged archive.If you want to avoid duplicate content, hide the full archives from search engines in your header:
- /**
- * Prevents indexing from search engines.
- *
- * Add this as an action to 'wp_head'.
- *
- * @return void
- */
- public function meta_noindex()
- {
- if ( $this->is_all_posts() )
- {
- print '<meta name="robots" content="noindex">';
- }
- }
- $GLOBALS['view_all_posts'] = new View_All_Posts;
wp_head
…- add_action(
- 'wp_head'
- , array ( $GLOBALS['view_all_posts'], 'meta_noindex' )
- );
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR
- . 'class.View_All_Posts.php';
- $GLOBALS['view_all_posts']->get_allposts_link();
…
Oh, wait … maybe you want to see the full code? And a download link?
Here’s the link: Download class.View_All_Posts.php
The complete code:
- /**
- * Adds a view 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
- */
- protected $all_param = 'all';
- public function __construct()
- {
- /* Register the query argument. */
- add_filter('query_vars', array ( $this, 'add_query_arg') );
- /* Hook into the query. */
- add_action('pre_get_posts', array ( $this, 'view_all_posts') );
- }
- /**
- * Registers the query arg in WordPress.
- * Otherwise it will be unset.
- *
- * @param array $vars Already registered query args.
- * @return array
- */
- public function add_query_arg( array $vars )
- {
- return array_merge( $vars, array ( $this->query_arg ) );
- }
- /**
- * Alters the query to remove the 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 the markup for the link.
- *
- * Usage in archive.php, category.php or search.php:
- * $GLOBALS['view_all_posts']->get_allposts_link();
- *
- * @param string $text Linktext
- * @param bool $print echo 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 link needed.
- return;
- }
- if ( isset ( $_SERVER['QUERY_STRING'] )
- && ! empty ( $_SERVER['QUERY_STRING'] )
- )
- {
- /* We have already visible GET parameters: /?hello=world. */
- $new_url = $_SERVER['REQUEST_URI'] . '&';
- }
- else
- {
- /* Note the difference: REQUEST_URL doesn't include
- * the query string while 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;
No hay comentarios:
Publicar un comentario