Taxonomy links and template tags from andy. see #6357
git-svn-id: http://svn.automattic.com/wordpress/trunk@7520 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
+141
-15
@@ -34,15 +34,23 @@ $wp_taxonomies['link_category'] = (object) array('name' => 'link_category', 'obj
|
||||
*
|
||||
* @uses $wp_taxonomies
|
||||
*
|
||||
* @param array|string $object_type Name of the type of taxonomy object
|
||||
* @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts)
|
||||
* @return array The names of all taxonomy of $object_type.
|
||||
*/
|
||||
function get_object_taxonomies($object_type) {
|
||||
function get_object_taxonomies($object) {
|
||||
global $wp_taxonomies;
|
||||
|
||||
if ( is_object($object) ) {
|
||||
if ( $object->post_type == 'attachment' )
|
||||
return get_attachment_taxonomies($object);
|
||||
$object = $object->post_type;
|
||||
}
|
||||
|
||||
$object = (array) $object;
|
||||
|
||||
$taxonomies = array();
|
||||
foreach ( $wp_taxonomies as $taxonomy ) {
|
||||
if ( in_array($object_type, (array) $taxonomy->object_type) )
|
||||
if ( array_intersect($object, (array) $taxonomy->object_type) )
|
||||
$taxonomies[] = $taxonomy->name;
|
||||
}
|
||||
|
||||
@@ -119,44 +127,51 @@ function is_taxonomy_hierarchical($taxonomy) {
|
||||
}
|
||||
|
||||
/**
|
||||
* register_taxonomy() - Create or modify a taxonomy object.
|
||||
* register_taxonomy() - Create or modify a taxonomy object. Do not use before init.
|
||||
*
|
||||
* A simple function for creating or modifying a taxonomy object based on the parameters given.
|
||||
* The function will accept an array (third optional parameter), along with strings for the
|
||||
* taxonomy name and another string for the object type.
|
||||
*
|
||||
* The function keeps a default set, allowing for the $args to be optional but allow the other
|
||||
* functions to still work. It is possible to overwrite the default set, which contains two
|
||||
* keys: hierarchical and update_count_callback.
|
||||
*
|
||||
* Nothing is returned, so expect error maybe or use is_taxonomy() to check whether taxonomy exists.
|
||||
*
|
||||
* Optional $args contents:
|
||||
* hierarachical - has some defined purpose at other parts of the API and is a boolean value.
|
||||
* update_count_callback - works much like a hook, in that it will be called when the count is updated.
|
||||
* rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize permastruct; default will use $taxonomy as slug
|
||||
* query_var - false to prevent queries, or string to customize query var (?$query_var=$term); default will use $taxonomy as query var
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Taxonomy
|
||||
* @since 2.3
|
||||
* @uses $wp_taxonomies Inserts new taxonomy object into the list
|
||||
* @uses $wp_rewrite Adds rewrite tags and permastructs
|
||||
* @uses $wp Adds query vars
|
||||
*
|
||||
* @param string $taxonomy Name of taxonomy object
|
||||
* @param array|string $object_type Name of the object type for the taxonomy object.
|
||||
* @param array|string $args See above description for the two keys values.
|
||||
*/
|
||||
function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
|
||||
global $wp_taxonomies, $wp_rewrite;
|
||||
global $wp_taxonomies, $wp_rewrite, $wp;
|
||||
|
||||
$defaults = array('hierarchical' => false, 'update_count_callback' => '');
|
||||
$defaults = array('hierarchical' => false, 'update_count_callback' => '', 'rewrite' => true, 'query_var' => true);
|
||||
$args = wp_parse_args($args, $defaults);
|
||||
|
||||
if ( !empty( $args['rewrite'] ) ) {
|
||||
if ( false !== $args['query_var'] ) {
|
||||
if ( empty($args['query_var']) )
|
||||
$args['query_var'] = $taxonomy;
|
||||
$args['query_var'] = sanitize_title_with_dashes($args['query_var']);
|
||||
$wp->add_query_var($args['query_var']);
|
||||
}
|
||||
|
||||
if ( false !== $args['rewrite'] ) {
|
||||
if ( !is_array($args['rewrite']) )
|
||||
$args['rewrite'] = array();
|
||||
if ( !isset($args['rewrite']['slug']) )
|
||||
$args['rewrite']['slug'] = sanitize_title_with_dashes($taxonomy);
|
||||
$wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', "taxonomy=$taxonomy&term=");
|
||||
$wp_rewrite->add_permastruct("{$args['rewrite']['slug']}/%$taxonomy%");
|
||||
$wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=$term");
|
||||
$wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%");
|
||||
}
|
||||
|
||||
$args['name'] = $taxonomy;
|
||||
@@ -1036,6 +1051,22 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
|
||||
|
||||
$defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
|
||||
$terms = array();
|
||||
if ( count($taxonomies) > 1 ) {
|
||||
foreach ( $taxonomies as $index => $taxonomy ) {
|
||||
$t = get_taxonomy($taxonomy);
|
||||
if ( is_array($t->args) && $args != array_merge($args, $t->args) ) {
|
||||
unset($taxonomies[$index]);
|
||||
$terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$t = get_taxonomy($taxonomies[0]);
|
||||
if ( is_array($t->args) )
|
||||
$args = array_merge($args, $t->args);
|
||||
}
|
||||
|
||||
extract($args, EXTR_SKIP);
|
||||
|
||||
if ( 'count' == $orderby )
|
||||
@@ -1067,10 +1098,10 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
|
||||
$query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) ORDER BY $orderby $order";
|
||||
|
||||
if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
|
||||
$terms = $wpdb->get_results($query);
|
||||
$terms = array_merge($terms, $wpdb->get_results($query));
|
||||
update_term_cache($terms);
|
||||
} else if ( 'ids' == $fields || 'names' == $fields ) {
|
||||
$terms = $wpdb->get_col($query);
|
||||
$terms = array_merge($terms, $wpdb->get_col($query));
|
||||
} else if ( 'tt_ids' == $fields ) {
|
||||
$terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) ORDER BY tr.term_taxonomy_id $order");
|
||||
}
|
||||
@@ -1899,4 +1930,99 @@ function _update_post_term_count( $terms ) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get_term_link() - Generates a permalink for a taxonomy term archive
|
||||
*
|
||||
* @param object|int|string $term
|
||||
* @param string $taxonomy
|
||||
* @return string HTML link to taxonomy term archive
|
||||
*/
|
||||
function get_term_link( $term, $taxonomy ) {
|
||||
global $wp_rewrite;
|
||||
|
||||
$termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
|
||||
|
||||
if ( !is_object($term) ) {
|
||||
if ( is_int($term) ) {
|
||||
$term = &get_term($term, $taxonomy);
|
||||
} else {
|
||||
$term = &get_term_by('slug', $term, $taxonomy);
|
||||
}
|
||||
}
|
||||
if ( is_wp_error( $term ) )
|
||||
return $term;
|
||||
|
||||
$slug = $term->slug;
|
||||
|
||||
if ( empty($termlink) ) {
|
||||
$file = get_option('home') . '/';
|
||||
$t = get_taxonomy($taxonomy);
|
||||
if ( $t->query_var )
|
||||
$termlink = "$file?$t->query_var=$slug";
|
||||
else
|
||||
$termlink = "$file?taxonomy=$taxonomy&term=$slug";
|
||||
} else {
|
||||
$termlink = str_replace("%$taxonomy%", $slug, $termlink);
|
||||
$termlink = get_option('home') . user_trailingslashit($termlink, 'category');
|
||||
}
|
||||
return apply_filters('term_link', $termlink, $term, $taxonomy);
|
||||
}
|
||||
|
||||
function the_taxonomies($args = array()) {
|
||||
$defaults = array(
|
||||
'post' => 0,
|
||||
'before' => '',
|
||||
'sep' => ' ',
|
||||
'after' => '',
|
||||
);
|
||||
|
||||
$r = wp_parse_args( $args, $defaults );
|
||||
extract( $r, EXTR_SKIP );
|
||||
|
||||
echo $before . join($sep, get_the_taxonomies($post)) . $after;
|
||||
}
|
||||
|
||||
function get_the_taxonomies($post = 0) {
|
||||
if ( is_int($post) )
|
||||
$post =& get_post($post);
|
||||
elseif ( !is_object($post) )
|
||||
$post =& $GLOBALS['post'];
|
||||
|
||||
$taxonomies = array();
|
||||
|
||||
if ( !$post )
|
||||
return $taxonomies;
|
||||
|
||||
$_template = '%s: %l.';
|
||||
|
||||
foreach ( get_object_taxonomies($post) as $taxonomy ) {
|
||||
$t = (array) get_taxonomy($taxonomy);
|
||||
if ( empty($t['label']) )
|
||||
$t['label'] = $taxonomy;
|
||||
if ( empty($t['args']) )
|
||||
$t['args'] = array();
|
||||
if ( empty($t['template']) )
|
||||
$t['template'] = $_template;
|
||||
|
||||
$terms = get_object_term_cache($post->ID, $taxonomy);
|
||||
if ( empty($terms) )
|
||||
$terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
|
||||
|
||||
$links = array();
|
||||
|
||||
foreach ( $terms as $term )
|
||||
$links[] = "<a href='" . attribute_escape(get_term_link($term, $taxonomy)) . "'>$term->name</a>";
|
||||
|
||||
if ( $links )
|
||||
$taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms);
|
||||
}
|
||||
return $taxonomies;
|
||||
}
|
||||
|
||||
function get_post_taxonomies($post = 0) {
|
||||
$post =& get_post($post);
|
||||
|
||||
return get_object_taxonomies($post);
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user