Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Can you please help me?

For getting post url I use: <?php the_permalink() ?> But for pages and for categories? If I use <?php the_permalink() ?> on categories it displays post url.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

There's currently no way in core to get the current archive / category / tag / custom taxonomy URL. I have a patch waiting for that to be added right here. For now though, you could just use the code in the last patch on that ticket:

<?php
/* Get the current archive link 
 *  
 * @param $paged boolean whether or not to return a link with the current page in the archive, default true 
 * @since 3.3 
 */ 
function get_current_archive_link( $paged = true ) { 
        $link = false; 

        if ( is_front_page() ) { 
                $link = home_url( '/' ); 
        } else if ( is_home() && "page" == get_option('show_on_front') ) { 
                $link = get_permalink( get_option( 'page_for_posts' ) ); 
        } else if ( is_tax() || is_tag() || is_category() ) { 
                $term = get_queried_object(); 
                $link = get_term_link( $term, $term->taxonomy ); 
        } else if ( is_post_type_archive() ) { 
                $link = get_post_type_archive_link( get_post_type() ); 
        } else if ( is_author() ) { 
                $link = get_author_posts_url( get_query_var('author'), get_query_var('author_name') ); 
        } else if ( is_archive() ) { 
                if ( is_date() ) { 
                        if ( is_day() ) { 
                                $link = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day') ); 
                        } else if ( is_month() ) { 
                                $link = get_month_link( get_query_var('year'), get_query_var('monthnum') ); 
                        } else if ( is_year() ) { 
                                $link = get_year_link( get_query_var('year') ); 
                        }                                                
                } 
        } 

        if ( $paged && $link && get_query_var('paged') > 1 ) { 
                global $wp_rewrite; 
                if ( !$wp_rewrite->using_permalinks() ) { 
                        $link = add_query_arg( 'paged', get_query_var('paged'), $link ); 
                } else { 
                        $link = user_trailingslashit( trailingslashit( $link ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var('paged'), 'archive' ); 
                } 
        } 
        return $link; 
}
share|improve this answer
    
that's an awesome script :) thanks allot! and how do I use it? put it in functions.php and call it echo get_current_archive_link(); ? –  Sandro Dzneladze Sep 26 '11 at 14:59
    
Yes, or just change the function to echo instead of return :) –  Joost de Valk Sep 26 '11 at 15:17
    
nice :) will check how it works right now. –  Sandro Dzneladze Sep 26 '11 at 15:27

for pages, it should be the same as for posts;

to link to category archives, try to use the category ID with

<?php echo get_category_link( $category_id ); ?>  

http://codex.wordpress.org/Function_Reference/get_category_link

edit: to get the link to the category of the current category archive, try:

<?php echo get_category_link( get_query_var('cat') ); ?>
share|improve this answer
    
I want to use it in the theme, so I cannot define $category_id 12 times (my blog category count). I just need to get current category url. –  Sandro Dzneladze Sep 26 '11 at 14:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.