WordPress.org

Ready to get started?Download WordPress

Forums

Passing queried taxonomy term to URL (1 post)

  1. NWTD
    Member
    Posted 1 year ago #

    In my setup I have a custom post type, Services, with a custom taxonomy, Locations. Using the taxonomy-locations.php, I'd like to pass the taxonomy term to the post's url linked from this page.

    For instance, if I'm looking at all posts in my Location taxonomy that have the term "Abbott, CA", I'd like to pass that term to the URL, like http://example.com/services/%service_post_name%/abbott-ca

    How do I go about doing this? Do I need to do a slug rewrite for the custom post type? I did find this on WordPress.org, but I'm not sure how to apply it to my situation as I'm not really familiar with all the codex and PHP needed.

    Here's my custom post type that I've registered by means of a plugin:

    function create_services() {
        register_post_type('services', array(   'label' => 'Services','description' => '','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => true,'rewrite' => true,'query_var' => true,'exclude_from_search' => false,'supports' => array('title','thumbnail','page-attributes',),'labels' => array (
            'name' => 'Services',
            'singular_name' => 'Service',
            'menu_name' => 'Services',
            'add_new' => 'Add Service',
            'add_new_item' => 'Add New Service',
            'edit' => 'Edit',
            'edit_item' => 'Edit Service',
            'new_item' => 'New Service',
            'view' => 'View Service',
            'view_item' => 'View Service',
            'search_items' => 'Search Services',
            'not_found' => 'No Services Found',
            'not_found_in_trash' => 'No Services Found in Trash',
            'parent' => 'Parent Service',
        ),) );
    }
    add_action('init', 'create_services');
    
    //Register Post Taxonomy: Locations
    function create_locations() {
        register_taxonomy('locations',array ( 0 => 'services',),array( 'hierarchical' => true, 'label' => 'Locations','show_ui' => true,'query_var' => true,'rewrite' => true,'singular_label' => 'Location') );
    }
    add_action('init', 'create_locations');

    Here's my taxonomy-locations.php:

    <?php if ( have_posts() ) : ?>
                <header class="archive-header">
                    <h1 class="archive-title">
                        <?php single_term_title('Services in '); ?>
                    </h1>
                </header><!-- .archive-header -->
    
                <ul>
                <?php while ( have_posts() ) : the_post();?>
    
                    <li>
                        <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                    </li>
    
                <?php endwhile;?>
                </ul>
    
            <?php else : ?>
                <?php get_template_part( 'content', 'none' ); ?>
            <?php endif; ?>

    Taking it a step further, I'd like to then reference that queried taxonomy in my single-services.php. I think this may have to be done after the URL is rewritten and then decode the URL?? I did find this walk through, but again, I'm not sure how to apply this to my situation.

    Any help would be greatly appreciated.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.