Created
May 2, 2024 15:10
-
-
Save rhcarlosweb/8e4fffc38170f41099808dbd78dace77 to your computer and use it in GitHub Desktop.
Post type and taxonomy with same slug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Função que altera o título da página de taxonomia 'category_photos'. | |
* | |
* @param string $title O título atual da página. | |
* @return string O novo título da página. | |
*/ | |
add_filter('pre_get_document_title', 'change_title_taxonomy_page', 10, 1); | |
function change_title_taxonomy_page($title) { | |
if (is_tax('category_photos')) { | |
$term = get_queried_object(); | |
$title = $term->name; | |
} | |
return $title; | |
} | |
/** | |
* Função para adicionar regras de reescrita personalizadas. | |
* | |
* A primeira regra permite que URLs no formato "fotos/{slug}" sejam tratados como a página "index.php?fotos={slug}". | |
* A segunda regra permite que URLs no formato "fotos/category/{slug}" sejam tratados como a página "index.php?category_photos={slug}". | |
* | |
* @since 2024 | |
*/ | |
function custom_rewrite_rule() { | |
add_rewrite_rule('^fotos/([^/]*)/?', 'index.php?fotos=$matches[1]', 'top'); | |
add_rewrite_rule('^fotos/category/([^/]*)/?', 'index.php?category_photos=$matches[1]', 'top'); | |
} | |
add_action('init', 'custom_rewrite_rule', 10, 0); | |
/** | |
* Define a solicitação de termo para uma determinada consulta. | |
* | |
* Esta função é usada como um filtro para o gancho 'request'. Ela modifica os parâmetros da consulta | |
* com base no nome do termo e na taxonomia fornecida. | |
* | |
* @param array $query Os parâmetros atuais da consulta. | |
* @return array Os parâmetros da consulta modificados. | |
*/ | |
add_filter('request', 'setTermRequest', 1, 1); | |
function setTermRequest($query) | |
{ | |
$taxonomy_name = 'category_photos'; | |
$include_children = false; | |
$name = $query['name']; | |
$term = get_term_by('slug', $name, $taxonomy_name); // obtém o termo atual para garantir que ele exista | |
if (isset($name) && $term && !is_wp_error($term)) { // verifica aqui | |
if ($include_children) { | |
unset($query['attachment']); | |
$parent = $term->parent; | |
while ($parent) { | |
$parent_term = get_term($parent, $taxonomy_name); | |
$name = $parent_term->slug . '/' . $name; | |
$parent = $parent_term->parent; | |
} | |
} else { | |
unset($query['name']); | |
} | |
switch ($taxonomy_name) { | |
case 'category': | |
{ | |
$query['category_name'] = $name; | |
break; | |
} | |
default: | |
{ | |
$query[$taxonomy_name] = $name; | |
break; | |
} | |
} | |
} | |
return $query; | |
} | |
function registerPostTypePhotos() { | |
$labels = array( | |
'name' => 'Fotos', | |
'singular_name' => 'Foto', | |
'menu_name' => 'Fotos', | |
'name_admin_bar' => 'Foto', | |
'add_new' => 'Adicionar Nova', | |
'add_new_item' => 'Adicionar Nova Foto', | |
'new_item' => 'Nova Foto', | |
'edit_item' => 'Editar Foto', | |
'view_item' => 'Ver Foto', | |
'all_items' => 'Todas as Fotos', | |
'search_items' => 'Buscar Fotos', | |
'parent_item_colon' => 'Foto Pai:', | |
'not_found' => 'Nenhuma foto encontrada.', | |
'not_found_in_trash' => 'Nenhuma foto encontrada na lixeira.', | |
); | |
$args = array( | |
'labels' => $labels, | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'fotos/category' ), | |
'capability_type' => 'post', | |
'has_archive' => true, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ), | |
'menu_icon' => 'dashicons-format-image', | |
); | |
register_post_type( 'fotos', $args ); | |
} | |
add_action( 'init', 'registerPostTypePhotos' ); | |
/* | |
Register Post Type: Fotos | |
*/ | |
function registerTaxonomyCategoryPhotos() { | |
$labels = array( | |
'name' => _x( 'Categorias de Fotos', 'taxonomy general name', 'textdomain' ), | |
'singular_name' => _x( 'Categoria de Foto', 'taxonomy singular name', 'textdomain' ), | |
'search_items' => __( 'Buscar Categorias', 'textdomain' ), | |
'all_items' => __( 'Todas as Categorias', 'textdomain' ), | |
'parent_item' => __( 'Categoria Pai', 'textdomain' ), | |
'parent_item_colon' => __( 'Categoria Pai:', 'textdomain' ), | |
'edit_item' => __( 'Editar Categoria', 'textdomain' ), | |
'update_item' => __( 'Atualizar Categoria', 'textdomain' ), | |
'add_new_item' => __( 'Adicionar Nova Categoria', 'textdomain' ), | |
'new_item_name' => __( 'Nova Categoria', 'textdomain' ), | |
'menu_name' => __( 'Categorias de Fotos', 'textdomain' ), | |
); | |
$args = array( | |
'hierarchical' => true, | |
'labels' => $labels, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'fotos' ), | |
); | |
register_taxonomy( 'category_photos', array( 'fotos' ), $args ); | |
} | |
add_action( 'init', 'registerTaxonomyCategoryPhotos' ); | |
// function flush_rewrite_rules_on_shutdown() { | |
// flush_rewrite_rules(); | |
// } | |
// add_action( 'shutdown', 'flush_rewrite_rules_on_shutdown' ); | |
// add_filter( 'term_link', 'writeTermPerm', 10, 3 ); | |
// function writeTermPerm( $url, $term, $taxonomy ){ | |
// $taxonomy_name = 'category_photos'; | |
// $taxonomy_slug = 'category_photos'; | |
// if ( strpos($url, $taxonomy_slug) === FALSE || $taxonomy != $taxonomy_name ) return $url; | |
// $url = str_replace('/'.$taxonomy_slug, '/fotos', $url); | |
// return $url; | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment