WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. TiagoPires
    Member
    Posted 1 year 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?

    http://wordpress.org/extend/plugins/cpt-onomies/

  2. Rachel Carden
    Member
    Plugin Author

    Posted 1 year 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 ) ) {
       ?><ul><?php
       // 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
          }
       ?></ul><?php
    }

    Was that helpful?

  3. TiagoPires
    Member
    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year ago #

    Good deal! Glad I could help!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.