Created
September 19, 2023 01:04
-
-
Save dantetesta/2dbe9c49fd937c6245e263ba1f3cff25 to your computer and use it in GitHub Desktop.
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 OBTEM O ID DO FILHO PARA A QUERY DO ARCHIVE - NO QUERBY BUILDER*/ | |
function ac_tax_query_category_shortcode() { | |
$cat_id = isset($_POST['query']['_tax_query_category']) ? $_POST['query']['_tax_query_category'] : ''; | |
return $cat_id; | |
} | |
add_shortcode('ac_tax_query_category', 'ac_tax_query_category_shortcode'); | |
function get_taxonomy_name_by_id_shortcode($atts) { | |
// Verifica se a variável _tax_query_category foi informada no $_POST | |
$taxonomy_id = isset($_POST['query']['_tax_query_category']) ? intval($_POST['query']['_tax_query_category']) : ''; | |
// Se não houver ID do $_POST ou estiver vazio, pegue o termo com base na slug do termo atual | |
if (!$taxonomy_id) { | |
$queried_object = get_queried_object(); | |
if ($queried_object && isset($queried_object->slug)) { | |
$term = get_term_by('slug', $queried_object->slug, $queried_object->taxonomy); | |
if ($term && !is_wp_error($term)) { | |
return $term->name; | |
} | |
} | |
} | |
// Se ainda não houver ID após todas as verificações, tente obter a slug da URL de referência | |
if (!$taxonomy_id) { | |
if (isset($_SERVER['HTTP_REFERER'])) { | |
$url_path = trim(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH), '/'); | |
$slug = end(explode('/', $url_path)); | |
$term = get_term_by('slug', $slug, 'category'); // Aqui, estou assumindo que a taxonomia é 'category'. Ajuste conforme necessário. | |
if ($term && !is_wp_error($term)) { | |
return $term->name; | |
} | |
} | |
else{ | |
$url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/'); | |
$slug = end(explode('/', $url_path)); | |
$term = get_term_by('slug', $slug, 'category'); // Aqui, estou assumindo que a taxonomia é 'category'. Ajuste conforme necessário. | |
if ($term && !is_wp_error($term)) { | |
return $term->name; | |
} | |
} | |
} | |
// Se ainda não houver ID após todas as verificações, retorne uma mensagem de erro | |
if (!$taxonomy_id) { | |
return "Taxonomy ID not provided."; | |
} | |
// Obtém os detalhes da taxonomia usando o ID | |
$term = get_term($taxonomy_id); | |
// Se a taxonomia não existir, retorne uma mensagem de erro | |
if (is_wp_error($term) || !$term) { | |
return "Taxonomy not found."; | |
} | |
// Retorna o nome da taxonomia | |
return $term->name; | |
} | |
// Registra o shortcode | |
add_shortcode('get_taxonomy_name_by_id', 'get_taxonomy_name_by_id_shortcode'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment