1

Got a website that has two top level pages called commercial and domestic, and in these pages there is other content that shares a similar format, e.g. I want case studies in both the commercial and domestic pages.

I thought it would be best to have case studies as a custom post type (case_studies) and have have a taxonomy (work_type) that has the two terms, commercial and domestic, to help distinguish what type of case study it is.

I thought to achieve this I would rewrite the case studies post type URL to show the term first followed by the word case_studies, so: commercial/Case_studies/single

I actually got the above to work, well almost! Got the rewritten page slug to function, but when it did, it broke all the top level pages, and turned the commercial and domestic pages to categories. I think the reason those pages used category pages was because the originals can't be found.

Did some research and I think everyone was saying that a custom field has to be prefixed by static text. Is there anyway of making it so I don't need that text prefixed?

The current issue is this makes the case_study post type work, %work_type%/case_studies, but breaks top level pages, whereas case_studies/%work_type% will work for everything, but it is not what I really want.

I have included the code below:

add_action( 'init', 'cptui_register_my_cpts' );
function cptui_register_my_cpts() {
    $labels = array(
        "name" => "Case Studies",
        "singular_name" => "Case Study",
    );    
    $args = array(
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "show_ui" => true,
        "has_archive" => false,
        "show_in_menu" => true,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => array( "slug" => "%work_type%/case_studies", "with_front" => false ),
        "query_var" => true,
        "supports" => array( "title", "thumbnail", "page-attributes" ),     
        "taxonomies" => array( "work_type" )
    );
    register_post_type( "case_studies", $args );    
// End of cptui_register_my_cpts()
}

add_action( 'init', 'cptui_register_my_taxes' );
function cptui_register_my_taxes() {   
    $labels = array(
        "name" => "Work Type",
        "label" => "Work Type",
    );    
    $args = array(
        "labels" => $labels,
        "hierarchical" => true,
        "label" => "Work Type",
        "show_ui" => true,
        "query_var" => true,
        "rewrite" => array( 'slug' => '%work_type%', 'with_front' => false ),
        "show_admin_column" => true,
    );
    register_taxonomy( "work_type", array( "testimonial", "case_studies" ), $args );
}

add_filter('post_type_link', 'work_types_permalink_structure', 10, 4);
function work_types_permalink_structure($post_link, $post, $leavename, $sample) {
    if (false !== strpos($post_link, '%work_type%')) {
        $work_types_type_term = get_the_terms($post->ID, 'work_type');
        if (!empty($work_types_type_term))
            $post_link = str_replace('%work_type%', array_pop($work_types_type_term)->
            slug, $post_link);
        else
            $post_link = str_replace('%work_type%', 'uncategorized', $post_link);
    }
    return $post_link;
}

Your Answer

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

Browse other questions tagged or ask your own question.