All the query stuff we need for tag= URLs to work.

git-svn-id: http://svn.automattic.com/wordpress/trunk@5149 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
matt 2007-03-31 06:16:12 +00:00
parent 2706ae78bd
commit 14f1c622ff
8 changed files with 89 additions and 10 deletions

View File

@ -1,13 +1,14 @@
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<?php is_tag(); ?>
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h2 class="pagetitle">Archive for the &#8216;<?php single_cat_title(); ?>&#8217; Category</h2>
<?php } elseif( is_tag() ) { ?>
<h2 class="pagetitle">Archive for the &#8216;<?php single_cat_title(); ?>&#8217; Tag</h2>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h2 class="pagetitle">Archive for <?php the_time('F jS, Y'); ?></h2>
@ -40,7 +41,7 @@
<?php the_content() ?>
</div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
</div>

View File

@ -18,7 +18,7 @@
<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
<small><?php the_time('l, F jS, Y') ?></small>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
</div>
<?php endwhile; ?>

View File

@ -40,6 +40,24 @@ function get_category_link($category_id) {
return apply_filters('category_link', $catlink, $category_id);
}
function get_tag_link( $tag_id ) {
global $wp_rewrite;
$catlink = $wp_rewrite->get_category_permastruct();
$category = &get_category($tag_id);
$category_nicename = $category->category_nicename;
if ( empty($catlink) ) {
$file = get_option('home') . '/';
$catlink = $file . '?tag=' . $category_nicename;
} else {
$catlink = str_replace('%tag%', $category_nicename, $catlink);
$catlink = get_option('home') . user_trailingslashit($catlink, 'category');
}
return apply_filters('tag_link', $catlink, $category_id);
}
function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
$chain = '';
$parent = &get_category($id);
@ -182,7 +200,7 @@ function get_the_tags( $before, $sep, $after ) {
$return = $before;
foreach ( $tags as $tag )
$tag_links[] = '<a href="' . get_category_link($tag->cat_ID) . '">' . $tag->cat_name . '</a>';
$tag_links[] = '<a href="' . get_tag_link($tag->cat_ID) . '">' . $tag->cat_name . '</a>';
$tag_links = join( $sep, $tag_links );
$tag_links = apply_filters( 'the_tags', $tag_links );

View File

@ -206,6 +206,15 @@ function get_category_by_path($category_path, $full_match = true, $output = OBJE
return NULL;
}
function get_category_by_slug( $slug ) {
global $wpdb;
$slug = sanitize_title( $slug );
if ( empty( $slug ) )
return false;
$category = $wpdb->get_var( "SELECT * FROM $wpdb->categories WHERE category_nicename = '$slug' " );
return get_category( $category );
}
// Get the ID of a category from its name
function get_cat_ID($cat_name='General') {
global $wpdb;

View File

@ -1,7 +1,7 @@
<?php
class WP {
var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots');
var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots');
var $private_query_vars = array('offset', 'posts_per_page', 'posts_per_archive_page', 'what_to_show', 'showposts', 'nopaging', 'post_type');
var $extra_query_vars = array();

View File

@ -86,6 +86,20 @@ function is_category ($category = '') {
return false;
}
function is_tag( $slug = '' ) {
global $wp_query;
if ( !$wp_query->is_tag )
return false;
if ( empty( $slug ) )
return true;
$cat_obj = $wp_query->get_queried_object();
if ( $category == $cat_obj->category_nicename )
return true;
return false;
}
function is_comments_popup () {
global $wp_query;
@ -305,6 +319,7 @@ class WP_Query {
var $is_time = false;
var $is_author = false;
var $is_category = false;
var $is_tag = false;
var $is_search = false;
var $is_feed = false;
var $is_comment_feed = false;
@ -329,6 +344,7 @@ class WP_Query {
$this->is_time = false;
$this->is_author = false;
$this->is_category = false;
$this->is_tag = false;
$this->is_search = false;
$this->is_feed = false;
$this->is_comment_feed = false;
@ -383,6 +399,7 @@ class WP_Query {
, 'year'
, 'w'
, 'category_name'
, 'tag'
, 'author_name'
, 'feed'
, 'tb'
@ -531,6 +548,9 @@ class WP_Query {
$this->is_category = true;
}
if ( '' != $qv['tag'] )
$this->is_tag = true;
if ( empty($qv['author']) || ($qv['author'] == '0') ) {
$this->is_author = false;
} else {
@ -541,7 +561,7 @@ class WP_Query {
$this->is_author = true;
}
if ( ($this->is_date || $this->is_author || $this->is_category) )
if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag ) )
$this->is_archive = true;
}
@ -837,7 +857,7 @@ class WP_Query {
$in_cats = substr($in_cats, 0, -2);
$out_cats = substr($out_cats, 0, -2);
if ( strlen($in_cats) > 0 )
$in_cats = " AND $wpdb->post2cat.category_id IN ($in_cats)";
$in_cats = " AND $wpdb->post2cat.category_id IN ($in_cats) AND rel_type = 'category' ";
if ( strlen($out_cats) > 0 ) {
$ids = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE $wpdb->post2cat.category_id IN ($out_cats)");
if ( is_array($ids) && count($ids > 0) ) {
@ -854,6 +874,21 @@ class WP_Query {
$groupby = "{$wpdb->posts}.ID";
}
if ( '' != $q['tag'] ) {
$reqcat= get_category_by_slug( $q['tag'] );
if ( !empty($reqcat) )
$reqcat = $reqcat->cat_ID;
else
$reqcat = 0;
$q['cat'] = $reqcat;
$tables = ", $wpdb->post2cat, $wpdb->categories";
$join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
$whichcat = " AND category_id IN ({$q['cat']}) AND rel_type = 'tag' ";
$groupby = "{$wpdb->posts}.ID";
}
// Category stuff for nice URLs
if ( '' != $q['category_name'] ) {
$reqcat = get_category_by_path($q['category_name']);
@ -881,13 +916,15 @@ class WP_Query {
$tables = ", $wpdb->post2cat, $wpdb->categories";
$join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
$whichcat = " AND category_id IN ({$q['cat']}, ";
$whichcat = " AND category_id IN ({$q['cat']} ";
$whichcat .= get_category_children($q['cat'], '', ', ');
$whichcat = substr($whichcat, 0, -2);
$whichcat .= ")";
$whichcat .= ") AND rel_type = 'category'";
$groupby = "{$wpdb->posts}.ID";
}
// Author/user stuff
if ( empty($q['author']) || ($q['author'] == '0') ) {

View File

@ -35,6 +35,9 @@ if ( defined('WP_USE_THEMES') && constant('WP_USE_THEMES') ) {
} else if ( is_category() && $template = get_category_template()) {
include($template);
return;
} else if ( is_tag() && $template = get_tag_template()) {
include($template);
return;
} else if ( is_author() && $template = get_author_template() ) {
include($template);
return;

View File

@ -328,6 +328,17 @@ function get_category_template() {
return apply_filters('category_template', $template);
}
function get_tag_template() {
$template = '';
if ( file_exists(TEMPLATEPATH . "/tag-" . get_query_var('tag') . '.php') )
$template = TEMPLATEPATH . "/tag-" . get_query_var('tag') . '.php';
elseif ( file_exists(TEMPLATEPATH . "/tag.php") )
$template = TEMPLATEPATH . "/tag.php";
return apply_filters('tag_template', $template);
}
function get_date_template() {
return get_query_template('date');
}