1

I have a custom post type "article" where posts have a permalink structure (www.mysite.com/article/post-name). I need each post in it to have two different templates according to its permalinks: the first template single-article.php: displayed with the common permalink (www.mysite.com/article/post-name). the second template abstract-template.php : is used whith a custom permalink (www.mysite.com/article/abstract/post-name). I found a code and it worked perfectly with wordpress normal posts, but it didn't work with custom post type:

function wpd_query_var( $query_vars ){
$query_vars[] = 'is_abstract';
return $query_vars;
}
add_filter('query_vars', 'wpd_query_var');
function wpd_post_rewrite(){
    add_rewrite_rule(
        'abstract/([^/]+)/?$',
        'index.php?name=$matches[1]&is_abstract=1',
        'top'
    );
}
add_action( 'init', 'wpd_post_rewrite' );
function wpd_abstract_template( $single_template ){
    global $wp_query;
    if ( isset( $wp_query->query_vars['is_abstract'] ) ) {
        $single_template = locate_template( 'abstract_template.php', false );
    }
    return $single_template;
}
add_filter( 'single_template', 'wpd_abstract_template' );

is there any suggestions to make it work with this custom post type?

  • Why would you want this? Can you provide some context as to what problem this is intended to solve? You're aware there are SEO and duplicate content implications if you're succesful? – Tom J Nowell Jul 30 '15 at 1:06
  • thanks for reply. i work on a website for a project in which each article would be displayed in two ways: full text & abstract only. seo problems for this issue can be solved by noindex for any one of the two types. i really need this code to work because i have a time limit to put the project online. – nisr Jul 30 '15 at 2:01
  • this answer may help you. – Milo Jul 30 '15 at 3:47
  • thanks @Milo , but i don't want an endpoint rewrite. what i need exactly is a code to activate a second template for a custom post type "article" when the custom post url include "/abstract/"before post name. Can you help me with that please? – nisr Jul 30 '15 at 11:30
  • your rewrite rule doesn't specify post type. – Milo Jul 30 '15 at 13:58

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.