" />
This function can take three optional parameters:
before (default = blank) - text/html to insert before each keyword
after (default = blank) - text/html to insert after each keyword
separator (default = ",") - text/html to insert between keywords
get_the_keywords() is a non-echoing version
the_post_keywords() - must be used inside the loop
Outputs a comma-separated list of the keywords for the current post. This function can take one optional parameters:
include_cats (default=false) - if true, post categories are included in the list.
get_the_post_keywords() is a non-echoing version.
the_post_keytags() - must be used inside the loop
Outputs the keywords for the current post as a series of links. By default these link a query for other posts with matching
keywords (can also link to the WordPress search function or to Technorati's page for that tag)
This function can take three optional parameters:
include_cats (default=false) - if true, post categories are included in the list.
local_search (default="tag") - if false or "technorati", the links will be to Technorati's tag page for the keyword instead,
if "search", the links will be to the local Wordpress search function.
link_title (default="") - alternate link title text to use, e.g. "My link title for" (tag name will be added at the end)
An example from my site:
[...]
[...]
>>> Tags:
[...]
get_the_post_keytags() is a non-echoing version.
is_keyword() - can be used outside the loop
Returns true if the current view is a keyword/tag search
the_search_keytag() - can be used outside the loop
Outputs the keyword/tag used for the search
get_the_search_keytag() is a non-echoing version
Rewrite Rules - The plugin can generate new tag search rewrite rules automatically. You need to
re-save your permalinks settings (Options -> Permalinks) for this to occur.
If your .htaccess file cannot be written to by WordPress, add the following to your
.htaccess file to use the tag search feature, preferably below the "# END WordPress" line:
RewriteRule ^tag/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?tag=$1&feed=$2 [QSA,L]
RewriteRule ^tag/(.+)/(feed|rdf|rss|rss2|atom)/?$ /index.php?tag=$1&feed=$2 [QSA,L]
RewriteRule ^tag/(.+)/page/?([0-9]{1,})/?$ /index.php?tag=$1&paged=$2 [QSA,L]
RewriteRule ^tag/(.+)/?$ /index.php?tag=$1 [QSA,L]
*/
/* You can change these constants if you wish for further customization*/
define('KEYWORDS_META', 'keywords'); // post meta key used in the wp database
define('KEYWORDS_TECHNORATI', 'http://technorati.com/tag'); // Technorati link to use if local search is false
define('KEYWORDS_ATOMTAGSON', '1'); // flag to add tags to Atom feed (required for Technorati)
define('KEYWORDS_QUERYVAR', 'tag'); // get/post variable name for querying tag/keyword from WP
define('KEYWORDS_TAGURL', 'tag'); // URL to use when querying tags
define('KEYWORDS_TEMPLATE', 'keywords.php'); // template file to use for displaying tag queries
define('KEYWORDS_SEARCHURL', 'search'); // local search URL (from mod_rewrite rules)
define('KEYWORDS_REWRITERULES', '1'); // flag to determine if plugin can change WP rewrite rules
define('KEYWORDS_SUGGESTED', '8'); // maximum number of keywords suggested
/* WP 2.0 doesn't initialize the rewrite object before plugins are loaded anymore
so these constants are set later
*/
function keywords_init() {
global $wp_rewrite;
/* Shouldn't need to change this - can set to 0 if you want to force permalinks off */
if (isset($wp_rewrite) && $wp_rewrite->using_permalinks()) {
define('KEYWORDS_REWRITEON', '1'); // nice permalinks, yes please!
define('KEYWORDS_LINKBASE', $wp_rewrite->root); // set to "index.php/" if using that style
} else {
define('KEYWORDS_REWRITEON', '0'); // old school links
define('KEYWORDS_LINKBASE', ''); // don't need this
}
/* generate rewrite rules for above queries */
if (KEYWORDS_REWRITEON && KEYWORDS_REWRITERULES)
add_filter('search_rewrite_rules', 'keywords_createRewriteRules');
}
add_action('init','keywords_init');
/* use in the loop*/
function get_the_post_keywords($include_cats=true) {
$keywords = '';
if ($include_cats) {
$categories = get_the_category();
foreach($categories as $category) {
if (!empty($keywords))
$keywords .= ",";
$keywords .= $category->cat_name;
}
}
$post_keywords = get_post_custom_values(KEYWORDS_META);
if (is_array($post_keywords)) {
foreach($post_keywords as $post_keys) {
if (!empty($post_keys))
$keywords .= ",";
$keywords .= $post_keys;
}
}
return( $keywords );
}
/* use in the loop*/
function the_post_keywords($include_cats=true) {
echo get_the_post_keywords($include_cats);
}
/* use in the loop*/
function get_the_post_keytags($include_cats=false, $localsearch="tag", $linktitle=false) {
// determine link mode
$linkmode = strtolower(trim($localsearch));
switch ($linkmode) {
case '':
case 'technorati':
$linkmode = 'technorati';
break;
case 'search':
$linkmode = 'search';
break;
//case 'tag':
//case 'keyword':
default:
$linkmode = 'tag';
break;
}
$output = "";
if ($linktitle === false)
$linktitle = ($linkmode == 'technorati') ? "Technorati tag page for" : "Search site for";
// do categories separately to get category links instead of tag links
if ($include_cats) {
$categories = get_the_category();
foreach($categories as $category) {
$keyword = $category->cat_name;
if ($linkmode == 'technorati')
$taglink = KEYWORDS_TECHNORATI . "/" . jkeywords_localLink($keyword);
else
$taglink = get_category_link($category->cat_ID);
$tagtitle = empty($linktitle) ? "" : " title=\"$linktitle $keyword\"";
if (!empty($output))
$output .= ", ";
$output .= "$keyword";
}
}
$post_keywords = get_post_custom_values(KEYWORDS_META);
if (is_array($post_keywords)) {
$keywordlist = array();
foreach($post_keywords as $post_keys)
$keywordlist = array_merge($keywordlist, explode(",", $post_keys));
foreach($keywordlist as $keyword) {
$keyword = trim($keyword);
if (!empty($keyword)) {
switch ($linkmode) {
case 'tag':
if (KEYWORDS_REWRITEON)
$taglink = get_settings('home') . '/' . KEYWORDS_LINKBASE . KEYWORDS_TAGURL .
'/' . jkeywords_localLink($keyword);
else
$taglink = get_settings('home') . "/?" . KEYWORDS_TAGURL . "=" . urlencode($keyword);
break;
case 'technorati':
$taglink = KEYWORDS_TECHNORATI . "/" . jkeywords_localLink($keyword);
break;
case 'search':
if (KEYWORDS_REWRITEON)
$taglink = get_settings('home') . '/' . KEYWORDS_LINKBASE . KEYWORDS_SEARCHURL .
'/' . jkeywords_localLink($keyword);
else
$taglink = get_settings('home') . '/?s=' . urlencode($keyword) . '&submit=Search';
break;
}
$tagtitle = empty($linktitle) ? "" : " title=\"$linktitle $keyword\"";
if (!empty($output))
$output .= ", ";
$output .= "$keyword";
}
}
}
return($output);
}
/* use in the loop*/
function the_post_keytags($include_cats=false, $localsearch=true, $linktitle=false) {
$taglist = get_the_post_keytags($include_cats, $localsearch, $linktitle);
if (empty($taglist))
echo "none";
else
echo $taglist;
}
/* works outside the loop*/
function get_the_keywords($before='', $after='', $separator=',', $include_cats='default') {
global $cache_categories, $category_cache, $post_meta_cache;
$keywords = "";
if ($include_cats) {
if ( isset($cache_categories) && ( ($include_cats == 'all') ||
(($include_cats == 'default') && is_home()) ) ) {
foreach($cache_categories as $category)
$keywordarray[$category->cat_name] += 1;
} elseif (isset($category_cache)) {
foreach($category_cache as $post_category) {
foreach($post_category as $category)
$keywordarray[$category->cat_name] += 1;
}
}
}
if (isset($post_meta_cache)) {
foreach($post_meta_cache as $post_meta) {
if (is_array($post_meta[KEYWORDS_META])) {
foreach($post_meta[KEYWORDS_META] as $post_keys) {
$keywordlist = explode(",", $post_keys);
foreach($keywordlist as $keyvalue)
if (!empty($keyvalue))
$keywordarray[$keyvalue] += 1;
}
}
}
}
if (is_array($keywordarray)) {
foreach($keywordarray as $key => $count) {
if (!empty($keywords))
$keywords .= $separator;
$keywords .= $before . $key . $after;
}
}
return ($keywords);
}
/* works outside the loop */
function the_keywords($before='', $after='', $separator=',') {
echo get_the_keywords($before, $after, $separator);
}
function is_keyword() {
global $wp_version;
$keyword = ( isset($wp_version) && ($wp_version >= 2.0) ) ?
get_query_var(KEYWORDS_QUERYVAR) :
$GLOBALS[KEYWORDS_QUERYVAR];
if (!is_null($keyword) && ($keyword != ''))
return true;
else
return false;
}
function get_the_search_keytag() {
$keyword = ( isset($wp_version) && ($wp_version >= 2.0) ) ?
get_query_var(KEYWORDS_QUERYVAR) :
$GLOBALS[KEYWORDS_QUERYVAR];
$searchtag = stripslashes($keyword);
return(get_magic_quotes_gpc() ? stripslashes($searchtag) : $searchtag);
}
function the_search_keytag() {
echo get_the_search_keytag();
}
/***** Tag cosmos functions *****/
function get_all_keywords($include_cats = false) {
global $wpdb, $cache_categories;
if ($include_cats && isset($cache_categories)) {
$catkeys = $wpdb->get_results("SELECT p2c.category_id AS cat_id, COUNT(p2c.rel_id) AS cat_count
FROM $wpdb->post2cat p2c, $wpdb->posts posts
WHERE posts.ID = p2c.post_id
AND posts.post_status IN('publish', 'static')
GROUP BY p2c.category_id");
if (is_array($catkeys)) {
foreach($catkeys as $category)
$keywordarray[ $cache_categories[$category->cat_id]->cat_name .
'::Category::' . $category->cat_id ] += $category->cat_count;
}
}
$metakeys = $wpdb->get_results("SELECT meta.meta_id, meta.meta_value
FROM $wpdb->posts posts, $wpdb->postmeta meta
WHERE posts.ID = meta.post_id
AND posts.post_status IN('publish', 'static')
AND meta.meta_key = '" . KEYWORDS_META . "'");
if (is_array($metakeys)) {
foreach($metakeys as $post_meta) {
if (!empty($post_meta->meta_value)) {
$post_keys = explode(',', $post_meta->meta_value);
foreach($post_keys as $keyword) {
$keyword = trim($keyword);
if (!empty($keyword))
$keywordarray[ $keyword ] += 1;
}
}
}
}
if(is_array($keywordarray))
uksort($keywordarray, 'strnatcasecmp');
return($keywordarray);
}
function jkeywords_localLink($keyword) {
return str_replace('%2F', '/', urlencode($keyword));
}
function jkeywords_flickrLink($keyword) {
return urlencode(preg_replace('/[^a-zA-Z0-9]/', '', strtolower($keyword)));
}
function jkeywords_deliciousLink($keyword) {
$del = preg_replace('/\s/', '', $keyword);
if (strstr($del, '+'))
$del = '"' . $del . '"';
return str_replace('%2F', '/', rawurlencode($del));
}
function all_keywords($element = '