• Is it possible to link to the exact page that the post is on in the categories archive?

    For example, if a post is in the category “drawings”, when I click on the the_category() link in single.php it will bring me to “url.com/drawings/page/3”

    Thanks.

Viewing 1 replies (of 1 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this in your theme’s functions.php:

    function add_pagenumber_to_cat_link( $termlink, $term_id ) {
    
    	global $post, $wpdb;
    
    	if( !$post )
    		return $termlink;
    
    	$where = get_posts_by_author_sql( $post->post_type );
    
    	$query = "SELECT count(*) FROM $wpdb->posts  INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) $where AND ($wpdb->term_relationships.term_taxonomy_id IN ($term_id) ) AND $wpdb->posts.post_date >= '$post->post_date' ORDER BY $wpdb->posts.post_date DESC";
    
    	$count  = (int) $wpdb->get_var( $query );
    
    	if($count) {
    		$posts_per_page = get_option( 'posts_per_page' );
    		$cat_num_page = ceil( $count / $posts_per_page );
    
    		if($cat_num_page > 1) {
    			$termlink = add_query_arg( 'paged', $cat_num_page, $termlink );
    		}
    	}
    
    	return $termlink;
    }

    in single.php use the_category() like this:

    <?php
    add_filter( 'category_link', 'add_pagenumber_to_cat_link',10,2 );
    the_category();
    remove_filter( 'category_link', 'add_pagenumber_to_cat_link',10,2 );
    ?>

Viewing 1 replies (of 1 total)
  • The topic ‘Category link in post to page location’ is closed to new replies.