• Resuelto vicentefega

    (@vicentefega)


    Hola, estoy teniendo un problema con la simulación de un «current item menu». He creado un custom post type -> ‘fichas’ y varias taxonomías (‘bar’, ‘terraza’, ‘gourmet’…).

    Cuando estamos en el single de un restaurante ‘gourmet’, necesito que se ilumine el icono «gastronomía» (un icono con la clase .gastronomia).

    Para ello, creo que la mejor opción es que cuando en el single detecte el term (pizzeria), que encienda la clase .gastronomia del menú.

    Lo he intentado así:

    <?php
    $terms = get_the_terms( $post->ID , 'fichas' );
    foreach ( $terms as $term ) {
    echo $term->name;
    }
    ?>
    <?php
    if(term_exists('pizzeria')){echo '<style type="text/css"> ul.icon_small li a.gastronomia{background-position:-170px -43px;} </style>';}
    elseif(term_exists('rebajas')){echo '<style type="text/css"> ul.icon_small li a.shopping{background-position:-255px -43px;} </style>';}
    elseif(term_exists('avion')){echo '<style type="text/css"> ul.icon_small li a.viajes{background-position:-43px -43px;} </style>';}
    ?>

    pero de esta forma es como si buscara todos los terms que existen y no el term del single actual… así que lo que hace es modificar la clase del primero (gastronomía), y cuando estoy en un single de «aviones» me sigue iluminando «.gastronomía» en lugar de «.viajes»

    ¿Alguna sugerencia?
    Mil gracias desde ya!

Viendo 1 respuesta (de un total de 1)
  • Iniciador del debate vicentefega

    (@vicentefega)

    Lo he solucionado tras leer esto .

    En functions.php

    /**
    * Conditional function to check if post belongs to term in a custom taxonomy.
    *
    * @param    tax        string                taxonomy to which the term belons
    * @param    term    int|string|array    attributes of shortcode
    * @param    _post    int                    post id to be checked
    * @return             BOOL                True if term is matched, false otherwise
    */
    function pa_in_taxonomy($tax, $term, $_post = NULL) {
        // if neither tax nor term are specified, return false
        if ( !$tax || !$term ) { return FALSE; }
        // if post parameter is given, get it, otherwise use $GLOBALS to get post
        if ( $_post ) {
            $_post = get_post( $_post );
        } else {
            $_post =& $GLOBALS['post'];
        }
        // if no post return false
        if ( !$_post ) { return FALSE; }
        // check whether post matches term belongin to tax
        $return = is_object_in_term( $_post->ID, $tax, $term );
        // if error returned, then return false
        if ( is_wp_error( $return ) ) { return FALSE; }
        return $return;
    }

    En single.php

    if (pa_in_taxonomy('genres', array('western', 'drama', 'comedy'))) {}
    // or
    if (pa_in_taxonomy('genres', array(1, 2, 3)) {}

    Un saludo!

Viendo 1 respuesta (de un total de 1)
  • El debate ‘Problema con if(term_exists’ está cerrado a nuevas respuestas.