Tag:
- Any calls to the_category() can be replaced by calls to the_tag()
- in "page.php", wrap the whole file in the following if block:
...rest of file...
* OPTIONALLY, you can also conditionally create the content of the 'tag'
static page by wrapping the content display area of 'page.php' with an if
block:
- in your sidebar, or wherever, you can add a call to to
display a list of your current tags
5) Now you can access your tagged "category" pages via:
http://your.url/goes-here/tag/TAGNAME/
And if you followed the OPTIONAL step above, the tag archive page is:
http://your.url/goes-here/tag/
NEW TEMPLATE TAGS:
', $aff='', $showcount=true, $thresh=0, $href=true);?>
See below for usage of these tags.
*/
/*
* ACTION HOOK: edit_form_advanced, edit_form
*
* Displays a list of current tags for your convenience when tagging posts.
*/
function tag_admin_edit_form() {
echo 'Available Tags: ';
get_tags('', ' ', false, 0, false);
echo "
\n";
}
add_action('edit_form_advanced', 'tag_admin_edit_form');
add_action('edit_form', 'tag_admin_edit_form');
$tag_page_name = '';
$tag_now = gmdate('Y-m-d H:i:59');
/*
* FILTER: the_posts
*
* Brings in the related posts with the same tag as is requested instead
* of displaying the static 'tag' page's content.
*/
function tag_posts_filter($posts) {
global $wpdb, $tableposts, $tablepostmeta;
global $tag_page_name, $tag_now;
if ( $tag_page_name != '' ) {
if (!isset($tableposts)) {
$tableposts = $wpdb->tableposts;
$tablepostmeta = $wpdb->tablepostmeta;
}
$request = "SELECT DISTINCT * FROM $tableposts, $tablepostmeta ".
" WHERE $tablepostmeta.meta_key='tags' AND".
" $tableposts.ID=$tablepostmeta.post_id AND".
" ($tablepostmeta.meta_value RLIKE '^$tag_page_name$|^$tag_page_name | $tag_page_name$| $tag_page_name ') AND".
" post_date_gmt <= '$tag_now' AND (post_status = 'publish')";
$posts = $wpdb->get_results($request);
}
return $posts;
}
add_filter('the_posts', 'tag_posts_filter');
/*
* FILTER: posts_where
*
* Intercepts and extracts the current $now date from the query originally
* retrieved by WP, and stores it in the global variable $tag_now for use in the
* 'the_posts' filter.
*/
function tag_posts_where_filter($where) {
global $tag_page_name, $tag_now;
if ( $tag_page_name != '' ) {
$match = array();
if (preg_match('/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/', $where, $match)) {
$tag_now = $match[1];
}
}
return $where;
}
add_filter('posts_where', 'tag_posts_where_filter');
/*
* FILTER: query_string
*
* Uses the query-string variable 'tagname' to deduce if a tag page request is
* issued and stores the requested tagname into the global variable
* $tag_page_name for use in the 'the_posts' filter.
*/
function tag_query_filter($query_string) {
global $tag_page_name;
$query = new WP_Query();
$query->parse_query($query_string);
if ( $query->is_page ) {
if ( $query->query_vars['pagename'] == 'tag' ) {
if ( isset($_GET['tagname']) ) {
$tag = $_GET['tagname'];
$tag_page_name = $tag;
tag_mangle($tag_page_name);
}
}
}
return $query_string;
}
add_filter('query_string', 'tag_query_filter');
function tag_rss_filter($the_list) {
//global $HTTP_SERVER_VARS;
//return basename($HTTP_SERVER_VARS['SCRIPT_FILENAME']);
//echo $the_list;
//return $the_list;
return the_tag_rss(false);
}
add_filter('the_category_rss', 'tag_rss_filter');
/////////////////////////////////////////
/*
* TEMPLATE: the_tag
*
* ++ Inside THE LOOP ++
*
* Behaves much like 'the_category' does in that it provides you an alphabetical
* list of the tags for the current post.
*
* Parameters:
* $sep : the separator to put between the tag links. Defaults to ', '
* $href : true/false. If true, the url is wrapped in an tagname, otherwise just the URL is returned.
*
* Returns:
* Nothing.
*
* Outputs:
* The hyperlinked tag list.
*/
function the_tag($sep = ', ', $href = true) {
global $id, $post, $more, $single, $withcomments, $page, $pages, $multipage, $numpages;
global $preview, $cookiehash;
global $pagenow;
$custom = get_post_custom_values('tags');
if ( isset($custom) ) {
$tags = implode(' ', $custom);
tag_mangle($tags);
$tag_list = explode(' ', $tags);
sort($tag_list);
for ($i = 0; $i < count($tag_list); $i++) {
$tag = $tag_list[$i];
if ( $href ) {
$tag_list[$i] = tag_link($tag);
} else {
$tag_list[$i] = $tag;
}
}
$out = implode($sep, $tag_list);
echo $out;
} else {
echo '';
}
}
/*
*
*/
function the_tag_rss($display = true) {
global $id, $post, $more, $single, $withcomments, $page, $pages, $multipage, $numpages;
global $preview, $cookiehash;
global $pagenow;
$custom = get_post_custom_values('tags');
if ( isset($custom) ) {
$tags = implode(' ', $custom);
tag_mangle($tags);
$tag_list = explode(' ', $tags);
sort($tag_list);
for ($i = 0; $i < count($tag_list); $i++) {
$tag_list[$i] = "\n\t".convert_chars($tag_list[$i])."";
}
$out = implode('', $tag_list);
if ( $display ) {
echo $out;
}
return $out;
} else {
if ( $display ) {
echo '';
}
return '';
}
}
/*
* TEMPLATE: tag_link
*
* Fashions an URL to link to the given tag.
*
* Parameters:
* $tag : Tag to link to, defaults to ''. If equal to '', then it provides a
* link to the general tag display page instead.
* $href : true/false. If true, the url is wrapped in an tagname, otherwise just the URL is returned.
*
* Returns:
* Either a plain URL or a hyperlinked URL.
*
* Outputs:
* Nothing.
*/
function tag_link($tag = '', $href = true) {
if ( $tag == '' ) {
$href = false;
$url = get_settings('home').'/tag/';
} else {
$url = get_settings('home').'/tag/'.$tag.'/';
}
if ( $href ) {
return ''.$tag.'';
} else {
return $url;
}
}
/*
* TEMPLATE: is_tag_single
*
* If a page listing for a single tag name is requested, this returns true.
*
* Parameters:
* None.
*
* Returns:
* True/false depending upon whether a tag page is requested.
*
* Outputs:
* Nothing.
*/
function is_tag_single() {
return isset($_GET['tagname']);
}
/*
* TEMPLATE: the_single_tag
*
* Displays the tag name when a single tag is requested.
*
* Parameters:
* $display : true/false. If true, the tag name is output as well as returned
*
* Returns:
* the tag name
*
* Outputs:
* conditionally outputs the tag name
*/
function the_single_tag($display = true) {
$out = '';
if ( isset($_GET['tagname']) ) {
$tag = $_GET['tagname'];
tag_mangle($tag);
$out = $tag;
}
if ( $display ) {
echo $out;
}
return $out;
}
/*
* TEMPLATE: get_tags
*
* Gets an alphabetical list of all current tags.
*
* Parameters:
* $pre : Text to prefix each element with. Defaults to ''.
* $aff : Text to affix each element with. Defaults to ''.
* $showcount : true/false. If true, the total count is displayed after the
* hyperlinked tag name. Defaults to true.
* $thresh : only tags with more than this number of entries will be listed
* $href : true/false. If true, the url is wrapped in an tagname, otherwise just the URL is returned.
*
* Returns:
* Nothing.
*
* Outputs:
* The hyperlinked tag list.
*/
function get_tags($pre = '', $aff = '', $showcount = true, $thresh = 0,
$href = true) {
global $wpdb, $tableposts, $tablepostmeta;
if (!isset($tableposts)) {
$tableposts = $wpdb->tableposts;
$tablepostmeta = $wpdb->tablepostmeta;
}
$list = array();
$sql = "SELECT meta_value FROM $tableposts, $tablepostmeta".
" WHERE $tableposts.ID=$tablepostmeta.post_id AND".
" $tableposts.post_status='publish' AND".
" $tablepostmeta.meta_key='tags' AND".
" $tableposts.post_password='' AND $tablepostmeta.meta_value!= ''";
$results = $wpdb->get_results($sql);
if ( !empty($results) ) {
foreach ($results as $tag) {
$name = $tag->meta_value;
tag_mangle($name);
$tlist = explode(' ', $name);
foreach ($tlist as $t) {
if ( isset($list[$t]) ) {
$list[$t]++;
} else {
$list[$t] = 1;
}
}
}
ksort($list);
foreach (array_keys($list) as $tag) {
if ( $list[$tag] > $thresh ) {
$line = '';
if ( $href ) {
$line = tag_link($tag);
} else {
$line = $tag;
}
if ( $showcount ) {
$line .= " ($list[$tag])";
}
echo $pre.$line.$aff."\n";
}
}
}
}
/*
* TEMPLATE: tag_mangle
*
* Mangles a parameter string to remove invalid tag characters
*
* Parameters:
* $s : REFERENCE to the string you wish to alter
*
* Returns:
* Nothing, though it does alter its inputs directly.
*
* Outputs:
* Nothing.
*/
function tag_mangle(&$s) {
trim($s);
$s = strtolower($s);
$s = preg_replace("/\s+/", ' ', $s); // concatenate all whitespace together
$s = preg_replace('/[^a-z0-9_. -]/', '', $s); // no bad characters
}
?>