Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Created June 24, 2010 20:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thefuxia/451940 to your computer and use it in GitHub Desktop.
Save thefuxia/451940 to your computer and use it in GitHub Desktop.
Adds a view all posts page to any archive.
<?php
/**
* Adds a view all posts page to any archive.
*
* @author Thomas Scholz http://toscho.de
* @version 1.1
* @license: GPL2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
Copyright 2010 Thomas Scholz (email: info@toscho.de)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2, as published by the Free Software Foundation.
You may NOT assume that you can use any other version of the GPL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
The license for this software can be found here:
http://www.gnu.org/licenses/gpl-2.0.html
*/
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'] . '&amp;';
}
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;
}
/**
* 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;
// Comment this out, if you don’t need it.
add_action(
'wp_head'
, array ( $GLOBALS['view_all_posts'], 'meta_noindex' )
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment