0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
/**
* Plugin Name: WP Plugin
* Description: A plugin for displaying translated CPT titles.
* Version: 1.0
* Author: WP Turbo
* Author URI: https://wpturbo.dev
* Text Domain: wpturbo
*/
// Hook into the post title display
add_filter('register_post_type_args', 'wpturbo_translate_cpt_labels', 10, 2);
function wpturbo_translate_cpt_labels($args, $post_type) {
// Check if it is the desired CPT
if ($post_type === 'producto') {
// Get the current language
$current_language = pll_current_language();
// Check if the current language is English
if ($current_language === 'en') {
// Translate the labels to English
$args['labels']['name'] = 'Products';
$args['labels']['singular_name'] = 'Product';
}
}
// Return the modified arguments
return $args;
}
En este código, hemos utilizado el filtro register_post_type_args para modificar los argumentos del CPT "producto" y así cambiar las etiquetas de nombre y nombre singular cuando el sitio se visualiza en inglés. Hemos reemplazado "producto" por "Products" y "Producto" por "Product" respectivamente.
Asegúrate de que el nombre del CPT en el código sea exactamente el mismo que el que has definido en tu plugin o tema.
Con este código, deberías poder traducir el nombre del CPT "producto" a "product" cuando el sitio se visualice en inglés. Si tienes alguna otra pregunta o necesitas más ayuda, no dudes en preguntar.