CPT-onomies: Using Custom Post Types as Taxonomies
[resolved] Sidebar List with terms linking to archive page (4 posts)

  1. TiagoPires
    Posted 3 years ago #

    If we're in the 'Movies' archive page and we need to list 'Directors' on the sidebar. So Clicking on a director will take us to the archive of'Movies' filtered by a direcotr like 'Almodovar' or 'Woody Alen'.

    It's a similar functionality to wp_list_categories() excepr that this cannot distinguish post types.

    Anyone has achieved this?


  2. Rachel Carden
    Plugin Author

    Posted 3 years ago #

    Well, considering the archive page you're wanting is custom made, you're going to have to print the directors list yourself and add in the link to your custom archive page.

    First, you need to decide what you want the URL to be for the movies archive page filtered by director. But, for this example, let's say your movies archive page is http://mywebsite.com/movies/ and you want your movies-director archive page to be http://mywebsite.com/movies/director/woody-allen. Here's what you do:

    Using the code on Custom CPT-onomy Archive Pages, add a rewrite rule to create your custom archive page. This will probably go in your functions.php file.

    add_action( 'init', 'my_website_add_rewrite_rule' );
    function my_website_add_rewrite_rule() {
       // allows our query to pick up the director slug
       add_rewrite_rule( '^movies/director/([^/]*)/?', 'index.php?post_type=movies&directors=$matches[1]&cpt_onomy_archive=1', 'top' );

    Now all you need to do is print the list.

    // first, retrieve the directors terms
    if ( ( $directors = get_terms( 'directors' ) ) && !is_wp_error( $directors ) ) {
       // print each director with your custom link
       foreach ( $directors as $director ) {
             ?><li><a href="<?php echo get_post_type_archive_link( $post_type ); ?>director/<?php echo $director->slug; ?>"><?php echo $director->name; ?></a></li><?php

    Was that helpful?

  3. TiagoPires
    Posted 3 years ago #

    Spot on! I'm proud that I got close, with a lot more code, but close. Your code is so much better! :P Thanks for all your help Rachel! unreal!

  4. Rachel Carden
    Plugin Author

    Posted 3 years ago #

    Good deal! Glad I could help!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic


No tags yet.