Custom post type and taxonomy permalinks – Structure

Been trying to get a nest of custom post type and taxonomy working
(even took a look here) but couldn’t get it working 100%.

Here is the thing, I created a custom post type texturas_temp and a custom taxonomy inside it categorias_texturas_temp.

What I would like to do is to get this structure:

  • /texturas_temp/ <- Displays all categorias_texturas_temp
  • /texturas_temp/categorias_texturas_temp <- Displays all texturas_temp with that particular categorias_texturas_temp term
  • /texturas_temp/categorias_texturas_temp/texturas_temp_post <- Displays single texturas_temp

It’s pretty much the same as a store with /products/category/product

I used this code for creating my custom taxonomy first:

// texturas_temp - Categorias
// =============================================================================
if ( ! function_exists( 'categorias_texturas_temp' ) ) {

// Register Custom Taxonomy
function taxonomy_categorias_texturas_temp() {

  $labels = array(
    'name'                       => _x( 'Categorias de texturas_temp', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Categoria de texturas_temp', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Categoria de texturas_temp', 'text_domain' ),
    'all_items'                  => __( 'Todos as categorias', 'text_domain' ),
    'parent_item'                => __( 'Categoria Pai', 'text_domain' ),
    'parent_item_colon'          => __( 'Categoria Pai:', 'text_domain' ),
    'new_item_name'              => __( 'Adicionar novo nome', 'text_domain' ),
    'add_new_item'               => __( 'Adicionar novo item', 'text_domain' ),
    'edit_item'                  => __( 'Editar Categoria', 'text_domain' ),
    'update_item'                => __( 'Atualizar Categoria', 'text_domain' ),
    'view_item'                  => __( 'Visualizar categoria', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separar itens com vírgulas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Adicionar ou Remover Itens', 'text_domain' ),
    'choose_from_most_used'      => __( 'Escolher dos mais utilizados', 'text_domain' ),
    'popular_items'              => __( 'Itens Populares', 'text_domain' ),
    'search_items'               => __( 'Procurar Itens', 'text_domain' ),
    'not_found'                  => __( 'Não Encontrado', 'text_domain' ),
    'items_list'                 => __( 'Lista de Itens', 'text_domain' ),
    'items_list_navigation'      => __( 'Navegação Lista de Itens', 'text_domain' ),
  );
  $args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'rewrite' => array(
      'slug' => 'texturas_temp', // This controls the base slug that will display before each term
    ),   
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
  );
  register_taxonomy( 'categorias-texturas_temp', array( 'texturas_temp' ), $args );

}
add_action( 'init', 'taxonomy_categorias_texturas_temp', 0 );

}

I used this code for creating my permalink categorias-texturas_temp:

// Modificar permalink
add_filter('post_link', 'brand_permalink', 1, 3);
add_filter('post_type_link', 'brand_permalink', 1, 3);

function brand_permalink($permalink, $post_id, $leavename) {
  //con %categorias_texturas_temp% catturo il rewrite del Custom Post Type
    if (strpos($permalink, '%categorias-texturas_temp%') === FALSE) return $permalink;
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'categorias-texturas_temp');
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
          $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'no-categorias-texturas_temp';

    return str_replace('%categorias-texturas_temp%', $taxonomy_slug, $permalink);
}

And then this for my custom post type:

// texturas_temp
// =============================================================================
if ( ! function_exists('texturas_temp') ) {

// Register Custom Post Type
function texturas_temp() {

  $labels = array(
    'name'                  => _x( 'texturas_temp', 'Post Type General Name', 'text_domain' ),
    'singular_name'         => _x( 'Textura', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'             => __( 'texturas_temp', 'text_domain' ),
    'name_admin_bar'        => __( 'texturas_temp', 'text_domain' ),
    'parent_item_colon'     => __( 'Textura:', 'text_domain' ),
    'all_items'             => __( 'Todas', 'text_domain' ),
    'add_new_item'          => __( 'Adicionar Nova Textura', 'text_domain' ),
    'add_new'               => __( 'Adicionar Nova', 'text_domain' ),
    'new_item'              => __( 'Adicionar Textura', 'text_domain' ),
    'edit_item'             => __( 'Editar Textura', 'text_domain' ),
    'update_item'           => __( 'Atualizar Textura', 'text_domain' ),
    'view_item'             => __( 'Visualizar Textura', 'text_domain' ),
    'search_items'          => __( 'Pesquisar Textura', 'text_domain' ),
    'not_found'             => __( 'Nenhuma Textura Encontrada', 'text_domain' ),
    'not_found_in_trash'    => __( 'Nenhuma Textura Econtrada no Lixo', 'text_domain' ),
    'items_list'            => __( 'Lista de texturas_temp', 'text_domain' ),
    'items_list_navigation' => __( 'Navegação Lista de texturas_temp', 'text_domain' ),
    'filter_items_list'     => __( 'Filtras Lista de texturas_temp', 'text_domain' ),
  );
  $args = array(
    'label'                 => __( 'Textura', 'text_domain' ),
    'description'           => __( 'texturas_temp da Futon Company.', 'text_domain' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
    'taxonomies'            => array( 'texturas_temp' ),
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-tagcloud',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'hierarchical'          => true, 
    'rewrite' => array(
      'slug' => 'texturas_temp/%categorias-texturas_temp%', // This controls the base slug that will display before each term
      'with_front' => false // Don't display the category base before "/locations/"
    ),
    'query_var'             => true,   
    'exclude_from_search'   => false,
    'capability_type'       => 'page',
  );
  register_post_type( 'texturas_temp', $args );

}
add_action( 'init', 'texturas_temp', 0 );

}

What I got:

  • /texturas_temp/ <- 404 with archive-texturas_temp.php template
  • /texturas_temp/categorias_texturas_temp <- Great with taxonomy-categorias-texturas_temp.php template
  • /texturas_temp/categorias_texturas_temp/texturas_temp_post <- Great with single-texturas_temp.php template

So basically I cannot seem to get the parent page working, also my breadcrumbs does not display it in the right way (jumps the category)

Am I doing everything wrong? That’s my concern…

I know It has been asked a million of times, hell I even tried everything from that link that I found.

Can anyone gimme some light?

Thanks very much !

UPDATE:
I rolled the plugin rewrite rules inspector and got no errors inside:

texturas_temp/%categorias-texturas_temp%/?$ index.php?post_type=texturas_temp   other
texturas_temp/%categorias-texturas_temp%/feed/(feed|rdf|rss|rss2|atom)/?$   index.php?post_type=texturas_temp&feed=$matches[1]  other
texturas_temp/%categorias-texturas_temp%/(feed|rdf|rss|rss2|atom)/?$    index.php?post_type=texturas_temp&feed=$matches[1]  other
texturas_temp/%categorias-texturas_temp%/page/([0-9]{1,})/?$    index.php?post_type=texturas_temp&paged=$matches[1] other

texturas_temp/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$  index.php?categorias-texturas_temp=$matches[1]&feed=$matches[2] texturas_temp
texturas_temp/([^/]+)/(feed|rdf|rss|rss2|atom)/?$   index.php?categorias-texturas_temp=$matches[1]&feed=$matches[2] texturas_temp
texturas_temp/([^/]+)/page/?([0-9]{1,})/?$  index.php?categorias-texturas_temp=$matches[1]&paged=$matches[2]    texturas_temp
texturas_temp/([^/]+)/?$    index.php?categorias-texturas_temp=$matches[1]  texturas_temp
texturas_temp/[^/]+/.+?/attachment/([^/]+)/?$   index.php?attachment=$matches[1]    texturas_temp
texturas_temp/[^/]+/.+?/attachment/([^/]+)/trackback/?$ index.php?attachment=$matches[1]&tb=1   texturas_temp
texturas_temp/[^/]+/.+?/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ index.php?attachment=$matches[1]&feed=$matches[2]   texturas_temp
texturas_temp/[^/]+/.+?/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$  index.php?attachment=$matches[1]&feed=$matches[2]   texturas_temp
texturas_temp/[^/]+/.+?/attachment/([^/]+)/comment-page-([0-9]{1,})/?$  index.php?attachment=$matches[1]&cpage=$matches[2]  texturas_temp
texturas_temp/([^/]+)/(.+?)/trackback/?$    index.php?categorias-texturas_temp=$matches[1]&texturas_temp=$matches[2]&tb=1   texturas_temp
texturas_temp/([^/]+)/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$    index.php?categorias-texturas_temp=$matches[1]&texturas_temp=$matches[2]&feed=$matches[3]   texturas_temp
texturas_temp/([^/]+)/(.+?)/(feed|rdf|rss|rss2|atom)/?$ index.php?categorias-texturas_temp=$matches[1]&texturas_temp=$matches[2]&feed=$matches[3]   texturas_temp
texturas_temp/([^/]+)/(.+?)/page/?([0-9]{1,})/?$    index.php?categorias-texturas_temp=$matches[1]&texturas_temp=$matches[2]&paged=$matches[3]  texturas_temp
texturas_temp/([^/]+)/(.+?)/comment-page-([0-9]{1,})/?$ index.php?categorias-texturas_temp=$matches[1]&texturas_temp=$matches[2]&cpage=$matches[3]  texturas_temp
texturas_temp/([^/]+)/(.+?)/cornerstone-endpoint(/(.*))?/?$ index.php?categorias-texturas_temp=$matches[1]&texturas_temp=$matches[2]&cornerstone-endpoint=$matches[4]   texturas_temp
texturas_temp/([^/]+)/(.+?)/wc-api(/(.*))?/?$   index.php?categorias-texturas_temp=$matches[1]&texturas_temp=$matches[2]&wc-api=$matches[4] texturas_temp
texturas_temp/[^/]+/.+?/([^/]+)/cornerstone-endpoint(/(.*))?/?$ index.php?attachment=$matches[1]&cornerstone-endpoint=$matches[3]   texturas_temp
texturas_temp/[^/]+/.+?/attachment/([^/]+)/cornerstone-endpoint(/(.*))?/?$  index.php?attachment=$matches[1]&cornerstone-endpoint=$matches[3]   texturas_temp
texturas_temp/[^/]+/.+?/([^/]+)/wc-api(/(.*))?/?$   index.php?attachment=$matches[1]&wc-api=$matches[3] texturas_temp
texturas_temp/[^/]+/.+?/attachment/([^/]+)/wc-api(/(.*))?/?$    index.php?attachment=$matches[1]&wc-api=$matches[3] texturas_temp
texturas_temp/([^/]+)/(.+?)(/[0-9]+)?/?$    index.php?categorias-texturas_temp=$matches[1]&texturas_temp=$matches[2]&page=$matches[3]   texturas_temp
texturas_temp/([^/]+)/comment-page-([0-9]{1,})/?$   index.php?categorias-texturas_temp=$matches[1]&cpage=$matches[2]    texturas_temp
texturas_temp/([^/]+)/cornerstone-endpoint(/(.*))?/?$   index.php?categorias-texturas_temp=$matches[1]&cornerstone-endpoint=$matches[3] texturas_temp
texturas_temp/([^/]+)/wc-api(/(.*))?/?$ index.php?categorias-texturas_temp=$matches[1]&wc-api=$matches[3]   texturas_temp

1 Answer
1

DID IT

My problem was the miss code found in the array of the custom post type texturas_temp

This:

// Register Custom Post Type
function texturas_temp() {

  $labels = array(
    'name'                  => _x( 'texturas_temp', 'Post Type General Name', 'text_domain' ),
    'singular_name'         => _x( 'Textura', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'             => __( 'texturas_temp', 'text_domain' ),
    'name_admin_bar'        => __( 'texturas_temp', 'text_domain' ),
    'parent_item_colon'     => __( 'Textura:', 'text_domain' ),
    'all_items'             => __( 'Todas', 'text_domain' ),
    'add_new_item'          => __( 'Adicionar Nova Textura', 'text_domain' ),
    'add_new'               => __( 'Adicionar Nova', 'text_domain' ),
    'new_item'              => __( 'Adicionar Textura', 'text_domain' ),
    'edit_item'             => __( 'Editar Textura', 'text_domain' ),
    'update_item'           => __( 'Atualizar Textura', 'text_domain' ),
    'view_item'             => __( 'Visualizar Textura', 'text_domain' ),
    'search_items'          => __( 'Pesquisar Textura', 'text_domain' ),
    'not_found'             => __( 'Nenhuma Textura Encontrada', 'text_domain' ),
    'not_found_in_trash'    => __( 'Nenhuma Textura Econtrada no Lixo', 'text_domain' ),
    'items_list'            => __( 'Lista de texturas_temp', 'text_domain' ),
    'items_list_navigation' => __( 'Navegação Lista de texturas_temp', 'text_domain' ),
    'filter_items_list'     => __( 'Filtras Lista de texturas_temp', 'text_domain' ),
  );
  $args = array(
    'label'                 => __( 'Textura', 'text_domain' ),
    'description'           => __( 'texturas_temp da Futon Company.', 'text_domain' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
    'taxonomies'            => array( 'texturas_temp' ),
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-tagcloud',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'hierarchical'          => true, 
    'rewrite' => array(
      'slug' => 'texturas_temp/%categorias-texturas_temp%', // This controls the base slug that will display before each term
      'with_front' => false // Don't display the category base before "/locations/"
    ),
    'query_var'             => 'texturas_temp-term',   
    'exclude_from_search'   => false,
    'has_archive' => 'texturas_temp',
    'capability_type'       => 'page',
  );
  register_post_type( 'texturas_temp', $args );

}
add_action( 'init', 'texturas_temp', 0 );

}

To this:

// Register Custom Post Type
function texturas_temp() {

  $labels = array(
    'name'                  => _x( 'texturas_temp', 'Post Type General Name', 'text_domain' ),
    'singular_name'         => _x( 'Textura', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'             => __( 'texturas_temp', 'text_domain' ),
    'name_admin_bar'        => __( 'texturas_temp', 'text_domain' ),
    'parent_item_colon'     => __( 'Textura:', 'text_domain' ),
    'all_items'             => __( 'Todas', 'text_domain' ),
    'add_new_item'          => __( 'Adicionar Nova Textura', 'text_domain' ),
    'add_new'               => __( 'Adicionar Nova', 'text_domain' ),
    'new_item'              => __( 'Adicionar Textura', 'text_domain' ),
    'edit_item'             => __( 'Editar Textura', 'text_domain' ),
    'update_item'           => __( 'Atualizar Textura', 'text_domain' ),
    'view_item'             => __( 'Visualizar Textura', 'text_domain' ),
    'search_items'          => __( 'Pesquisar Textura', 'text_domain' ),
    'not_found'             => __( 'Nenhuma Textura Encontrada', 'text_domain' ),
    'not_found_in_trash'    => __( 'Nenhuma Textura Econtrada no Lixo', 'text_domain' ),
    'items_list'            => __( 'Lista de texturas_temp', 'text_domain' ),
    'items_list_navigation' => __( 'Navegação Lista de texturas_temp', 'text_domain' ),
    'filter_items_list'     => __( 'Filtras Lista de texturas_temp', 'text_domain' ),
  );
  $args = array(
    'label'                 => __( 'Textura', 'text_domain' ),
    'description'           => __( 'texturas_temp da Futon Company.', 'text_domain' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
    'taxonomies'            => array( 'categorias-texturas_temp' ),
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-tagcloud',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'hierarchical'          => true, 
    'rewrite' => array(
      'slug' => 'texturas_temp/%categorias-texturas_temp%', // This controls the base slug that will display before each term
      'with_front' => false // Don't display the category base before "/locations/"
    ),
    'query_var'             => 'texturas_temp-term',   
    'exclude_from_search'   => false,
    'has_archive' => 'texturas_temp',
    'capability_type'       => 'page',
  );
  register_post_type( 'texturas_temp', $args );

}
add_action( 'init', 'texturas_temp', 0 );

}

Basically I was naming my custom post type with its own taxonomy, changed:

'taxonomies'            => array( 'texturas_temp' ),

To:

'taxonomies'            => array( 'categorias-texturas_temp' ),

Working fine now…

Leave a Comment