Salta la navigazione

Questo thread è stato risolto. Ecco una descrizione del problema e della soluzione.

Problem:
The client needs to extract the URL of each custom taxonomy within a query on a specific page. The code was working correctly before installing WPML and still works if WPML is disabled.
Solution:
When working with WPML, it is necessary to use our hooks such as wpml_object_id and wpml_current_language. Here is an example:

$currentterm = get_field('selezione_categoria', false, true);
$term_id = $currentterm->term_id;
$taxonomy_name = 'product-category';

// Get current language
$current_language = apply_filters('wpml_current_language', NULL);

// Get term children
$termchildren = get_term_children($term_id, $taxonomy_name);
echo '<ul>';
foreach ($termchildren as a child) {

// Get the translated term ID
$translated_term_id = apply_filters('wpml_object_id', $child, $taxonomy_name, true, $current_language);

if ($translated_term_id) {
        $term = get_term_by('id', $translated_term_id, $taxonomy_name);
        echo '<li><a href="' . get_term_link($term->term_id, $taxonomy_name) . '">' . $term->name . '</a></li>';
    }}
echo '</ul>';

Please note that this code is an example, and we cannot provide custom code.

If this solution does not resolve your issue or seems outdated, we recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If the problem persists, please open a new support ticket.

Questo è il forum di assistenza tecnica di WPML, il plug-in multilingue di WordPress.

La sua lettura è permessa a tutti, ma la pubblicazione è riservata esclusivamente ai clienti di WPML. Il team di WPML risponde sul forum 6 giorni su 7, 22 ore su 24.

Nessun supporter è disponibile a lavorare oggi sul forum Italian. Sentitevi liberi di creare ticket e li gestiremo il prima possibile quando saremo online. Grazie per la comprensione.

Etichettato: 

Questo ticket contiene 6 risposte, ha 2 voci.

Ultimo aggiornamento da Laura 1 mese, 4 settimane fa.

Assistito da: Laura.

Autore Messaggi
Luglio 25, 2024 a 7:57 am #16001814

robertoB-31

Contesto del problema:
In questa pagina link nascosto dovrei estrarre l'url di ciascuna custom taxonomy, all'interno di una query. Il codice funzionava correttamente prima dell'istallazione di WPML (e confermo che funziona se disattivo WPML).

La query è in una singola pagina, prende la categoria prodotto da un custom field impostato nella pagina, e dovrebbe mostrare tutte le sottocategorie relative, con il link a ciascuna pagina archivio.
Codice completo di seguito:

$currentterm = get_field('selezione_categoria', false, true);
$term_id = $currentterm->term_id;
$taxonomy_name = 'product-category';
$termchildren = get_term_children($term_id, $taxonomy_name);

echo '

';
foreach ($termchildren as $child) {
$term = get_term_by('id', $child, $taxonomy_name); ?>

Sintomi:
L'errore PHP è Object of class WP_Error could not be converted to string.
L'errore viene causato da questo codice . get_term_link($child, $taxonomy_name) .
Facendo un test $value = get_term_link("not a real term","fake taxonomy"); if (is_wp_error($value)) { echo $value->get_error_message(); } mi viene detto che le stringhe sono vuote.

Domande:
Avete idea del perché i link alle pagine custom taxonomy (product-category) vengano visti come vuoti?
Avete altri riferimenti da darmi per capire perchè wpml causa questo problema?

Luglio 25, 2024 a 8:41 am #16002313

Laura
Supporter

Lingue: Inglese (English ) Italiano (Italiano )

Fuso orario: Europe/Rome (GMT+02:00)

Salve,

grazie per averci contattato.

Se vado sul sito mi sembra che le categorie prodotto non siano tradotte, quindi WPML non le può vedere.

Luglio 25, 2024 a 8:43 am #16002318

robertoB-31

Grazie Laura,
io credo di averle tradotte, nel senso che ho inserito le 45 voci per ciascuna delle 3 lingue. Che altro passaggio dovrei fare?

Schermata 2024-07-25 alle 10.42.42.png
Luglio 25, 2024 a 8:47 am #16002366

Laura
Supporter

Lingue: Inglese (English ) Italiano (Italiano )

Fuso orario: Europe/Rome (GMT+02:00)

Ma le hai aggiunte e basta o le hai create come traduzioni?

Perché se vado link nascosto">qui per esempio come vedi non c'è nessuna traduzione

Luglio 25, 2024 a 8:57 am #16002394

robertoB-31

Io le ho inserite come traduzioni da WPML > Traduzioni della tassonomia
Credo siano tradotte così,
ci sono altri step?

Schermata 2024-07-25 alle 10.49.55.png
Schermata 2024-07-25 alle 10.50.12.png
Luglio 25, 2024 a 8:58 am #16002424

robertoB-31

link nascosto

Luglio 25, 2024 a 3:42 pm #16004615

Laura
Supporter

Lingue: Inglese (English ) Italiano (Italiano )

Fuso orario: Europe/Rome (GMT+02:00)

Lavorando con WPML è necessario utilizzare i nostri hook come wpml_object_id e wpml_current_language

Vedi per esempio

$currentterm = get_field('selezione_categoria', false, true);
$term_id = $currentterm->term_id;
$taxonomy_name = 'product-category';

// Get current language
$current_language = apply_filters('wpml_current_language', NULL);

// Get term children
$termchildren = get_term_children($term_id, $taxonomy_name);

echo '<ul>';
foreach ($termchildren as $child) {
    // Get the translated term ID
    $translated_term_id = apply_filters('wpml_object_id', $child, $taxonomy_name, true, $current_language);
    
    if ($translated_term_id) {
        $term = get_term_by('id', $translated_term_id, $taxonomy_name);
        echo '<li><a href="' . get_term_link($term->term_id, $taxonomy_name) . '">' . $term->name . '</a></li>';
    }
}
echo '</ul>';

Nota che il codice è un esempio, non possiamo fornire codice custom.

Luglio 26, 2024 a 6:35 am #16006832

robertoB-31

Grazie,
queste indicazioni hanno risolto il mio problema!

robertoB-31 ha confermato che il problema è stato risolto su 2024-07-26 06:35:23.
Questo ticket è stato chiuso. Se sei un cliente di WPML e hai bisogno di aiuto, apri un nuovo ticket di assistenza.