X

Come verificare se una pagina è figlia (Avanzato con ID o SLUG)

Snippet di IsItWP

Stai cercando un modo per scoprire se una pagina sul tuo sito WordPress è una pagina figlia o meno? Sebbene probabilmente esista un plugin per questo, abbiamo creato un rapido snippet di codice che puoi utilizzare per verificare se una pagina è una pagina figlia in WordPress.

Istruzioni:

Tutto quello che devi fare è aggiungere questo codice al file functions.php del tuo tema o in un plugin specifico per il sito:

/*
* Checks if the page is the parent or a child of the page matching the $pid param
* $pid can be the page slug or the page ID #
*
* This function is best used to display menus when the parent/top level menu item needs
* a rollover state or special treatment.
*
* @param $pid (string or int)
* returns true if current page ID matches , else return false
*/


function is_child( $pid ) {  //  = The ID of the page we're looking for pages underneath
        global $post;        // load details about this page

    // if $pid is an ID
        if (is_int($pid)) {

                if ( is_page($pid) )
                        return true;
            // get the page's children if the ID of a child matches the  return true
                $anc = get_post_ancestors( $post->ID );
                foreach ( $anc as $ancestor ) {
                        if( is_page() && $ancestor == $pid ) {
                                return true;
                        }
                }
        }
    // if  is a page slug
        elseif (is_string($pid)) {
                // Get page by 'SLUG'
                $page = get_page_by_path( $pid );
                if ($post->ID == $page->ID) {
                        return true;
                }

                $anc = get_post_ancestors( $post->ID );
                foreach ( $anc as $ancestor ) {
                        if( is_page() &&  == $page->ID ) {
                                return true;
                        }
                }
        }

        return false;  // we arn't at the page, and the page is not an ancestor
}

/*
Used in a template:

<img src="home<?php if (is_child(0)){?>-ro<?php } ?>.jpg">
or
<img src="home<?php if (is_child('home')){?>-ro<?php } ?>.jpg">

if true equals
<img src="home-ro.jpg">


*/

Nota: Se questa è la prima volta che aggiungi snippet di codice in WordPress, consulta la nostra guida su come aggiungere correttamente snippet di codice in WordPress, in modo da non compromettere accidentalmente il tuo sito.

Se ti è piaciuto questo snippet di codice, prendi in considerazione la possibilità di consultare i nostri altri articoli sul sito come: I 7 migliori plugin per sondaggi WordPress per aumentare l'interazione sul sito e come impostare il monitoraggio dei download in WordPress con Google Analytics.

Commenti   Lascia una risposta

  1. Ciao, c'è un errore di sintassi al n. 39

Aggiungi un commento

Siamo lieti che tu abbia scelto di lasciare un commento. Tieni presente che tutti i commenti sono moderati secondo la nostra normativa sulla privacy e tutti i link sono nofollow. NON utilizzare parole chiave nel campo del nome. Avviamo una conversazione personale e significativa.

Checklist per il lancio di WordPress

La Guida Definitiva per il Lancio di WordPress

Abbiamo raccolto tutti gli elementi essenziali della checklist per il lancio del tuo prossimo sito web WordPress in un comodo ebook.
Sì, Inviami l'eBook Gratuito!