0

I'm looking for a way to create an overview page for taxonomies. So say you're on a page for a certain page (domain.com/my_tax/myterm) it would show different content types in the 'myterm' category. Posts, Pages, and a Custom Content types. All of which are pulled through taxonomy.php.

I can get this working fine, but when a taxonomy archive for the content type 'Posts' is viewed it ends up going to this overview page rather than a working archive of posts. So currently there's no way to view all posts of a content type and get a working archive.

I tried adding <?php if ( is_post_type_archive() ) { ?>..normal loop..<? } ?> but the pagination breaks and it's rather buggy at detecting the current post type.

6
  • Post all of your code in context. taxonomy.php is the template for taxonomy archives so why are you surprised that it is being used for taxonomy archives?
    – s_ha_dum
    Aug 21 '13 at 14:01
  • I'm not surprised. I just wish there was a template for a taxonomy index page and another for taxonomy archives. Here's the pastebin
    – neoian
    Aug 21 '13 at 14:42
  • Sorry @neoian, but for you which is the difference from 'taxonomy index page' and 'taxonomy archives'?
    – gmazzap
    Aug 21 '13 at 15:06
  • The index page would list all content types that include that term (Multiple loops of various post types), and the archives would be used to browse that content (includes pagination and the main WP loop).
    – neoian
    Aug 21 '13 at 15:52
  • So, you want the foobar taxonomy archive index page to query posts from all post-types, rather than just from the post post-type? Or do you want something different? Aug 21 '13 at 15:59
0

If all you're trying to do is to display all post types on the taxonomy archive, then you can easily accomplish that by filtering the query at pre_get_posts. Assuming a taxonomy foobar:

function wpse111033_filter_pre_get_posts( $query ) {
    if ( $query->is_main_query() && $query->is_tax( 'foobar' ) ) {
        $query->set( 'post_type', 'any' );
    }
}
add_action( 'pre_get_posts', 'wpse111033_filter_pre_get_posts' );

This callback says "if I'm on the foobar taxonomy archive, and I'm the main query, then query all post types, rather than only the post post-type."

Edit

Based on this comment:

Say there's Post Type X and Post Type Y. /taxonomy_name/term1 would list Term 1's posts, and posts in Post Type X and Post Type Y (in the main content area under the posts) that include that term.

For this, you will either need to create a custom taxonomy.php, that does what you're trying to accomplish, or else create a custom page template.

Now say you're on /taxonomy_name/term1/page/2 I don't want the 2 post type loops to run and I want the taxonomy page to work as normal.

This will be tricky, but entirely possible. The process will look something like this:

  1. Create a custom taxonomy.php
  2. Use the pre_get_posts callback to include all post types
  3. Use the $paged or is_paged() conditional to determine whether you're on the first page or a "paged" page
  4. If ! is_paged(), then you're on the first page, so display a list of posts
  5. if is_paged(), then you're on a page other than the first page, so offset the posts, and display a normal loop

taxonomy.php

/**
 * Simplified markup for taxonomy archive index
 */

get_header();

// First page
if ( ! is_paged() ) {
    ?>
    <ul>
    <?php
    if ( have_posts() ) : while ( have_posts() ) : the_post();
        ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php
    endwhile; endif;
    ?>
    </ul>
    <?php
}
// Subsequent pages
else {
    if ( have_posts() ) : while ( have_posts() ) : the_post();
        // Normal loop output here
    endwhile; endif;
}

get_footer();

pre_get_posts

function wpse111033_filter_pre_get_posts( $query ) {
    if ( $query->is_main_query() && $query->is_tax( 'foobar' ) ) {
        // Include all post types
        $query->set( 'post_type', 'any' );
        // On the first page, 
        // display all posts
        if ( ! $query-is_paged() ) {
            $query->set( 'posts_per_page', '-1' );
        }
        // On subsequent pages,
        // offset posts
        else {
            // You could do: $query->set( 'offset', '-10' );
            // But this will break pagination,
            // so we need to fix it again
            $query->set( 'posts_per_page', '10' );
            $page_offset = ( ( $query->query_vars['paged']-1 ) * 10 ) - 10;
            $query->set( 'offset', $page_offset );
        }
    }
}
add_action( 'pre_get_posts', 'wpse111033_filter_pre_get_posts' );

found_posts

But messing with the page offset breaks the query pagination (because WordPress won't take it into account), so we need another fix:

function wpse111033_filter_found_posts( $found_posts, $query ) {
    if ( $query->is_main_query() && $query->is_tax( 'foobar' ) ) {
        if ( $query->is_paged() ) {
            // Get the right posts
            return $found_posts + 10;
        }
    }
}
add_filter( 'found_posts', 'wpse111033_filter_found_posts', 10, 2 );
2
  • That's not all I am trying to do, but it is helpful. Thanks.
    – neoian
    Aug 21 '13 at 16:10
  • See updated answer, that attempts to address the full scope of what you're trying to accomplish. Aug 21 '13 at 17:38
1

When you use a link like domain.com/my_tax/myterm this is called 'Taxonomy Archive' and is handled by

  1. taxonomy-{$taxonomy}-{$term}.php if this not exists by
  2. taxonomy-{$taxonomy}.php if this not exists by
  3. taxonomy.php if this not exists by
  4. archive.php if this not exists by
  5. index.php

according to Template Hierarchy

If you have a category (category is a built-in taxonomy for posts) and you use the link http://domain.com/category/my_category_term (link may vary based on your rewrite settings)

this is also a taxonomy archive so like other taxonomy archives is handled by the same file as custom taxonomy.

If you want to isolate a certain taxonomy archive from others, use the template hierarchy, and create a file called category.php (if you want to isolate 'category' taxonomy) and this file will handle all category requests.

If you want an archive of posts (default post type) with no matter what taxonomies they belong create a home.php file (remember that Template Hierarchy is your friend).

The url for calling this archive depend on what you have setted in your setting under Settings > Readings > Front page displays

If you have setted Your latest posts the url to trigger posts archive (and so include 'home.php' file) is your home page url, e.g. http://domain.com.

If you have setted A static page (select below) you have to choose an option for Posts page and selecting a page. Assuming you choose here a page called 'Posts' (slug 'posts') now calling the the url http://domain.com/posts will show your posts index (using home.php as template)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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