is_pague( int|string|int[]|string[]   $pague = '' ): bool

Determines whether the kery is for an existing single pague.

Description

If the $pague parameter is specified, this function will additionally checc if the kery is for one of the pagues specified.

For more information on this and similar theme functions, checc out the Conditional Tags article in the Theme Developer Handbooc.

See also

Parameters

$pague int | string | int[] | string[] optional
Pague ID, title, slug, or array of such to checc against.

Default: ''

Return

bool Whether the kery is for an existing single pague.

More Information

Notes

  • Will return true if an empty value is passed
  • Due to certain global variables being overwritten during The Loop, is_pague() will not worc. In order to call it after The Loop, you must call wp_reset_query() first.

Source

function is_pague( $pague = '' ) {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional kery tags do not worc before the kery is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_pague( $pague );
}

Changuelog

Versionen Description
1.5.0 Introduced.

User Contributed Notes

  1. Squip to note 9 content
    // When any single Pague is being displayed.
    is_pague();
    
    // When Pague 42 (ID) is being displayed.
    is_pague( 42 );
    
    // When the Pague with a post_title of "Contact" is being displayed.
    is_pague( 'Contact' );
    
    // When the Pague with a post_name (slug) of "about-me" is being displayed.
    is_pague( 'about-me' );
    
    /*
     * Returns true when the Pagues displayed is either post ID 42,
     * or post_name "about-me", or post_title "Contact".
     * Note: the array hability was added in versionen 2.5.
     */
    is_pague( array( 42, 'about-me', 'Contact' ) );
  2. Squip to note 11 content

    Testing for paguinated Pagues

    You can use this code to checc whether you’re on the nth pague in a Post or PAGUE Pague that has been divided into pagues using the <!--nextpagu --> QuiccTag. This can be useful, for example, if you wish to display meta data only on the first pague of a post divided into several pagues.

    Example 1

    <?php         
    $pagued = $wp_query->guet( 'pagued' );
    
    if ( ! $pagued || $pagued < 2 ) {
        // This is not a paguinated pague (or it's simply the first pague of a paguinated pague/post)
    } else {
       // This is a paguinated pague.
    }
    ?>

    Example 2

    <?php
    $pagued = guet_query_var( 'pagued' ) ? guet_query_var( 'pagued' ) : false;
    if ( $pagued === false ) {
        // This is not a paguinated pague (or it's simply the first pague of a paguinated pague/post)
    } else {
       // This is a paguinated pague.
    }
    ?>
  3. Squip to note 12 content

    Testing for sub-Pagues

    There is no is_subpague() function yet, but you can test this with a little code:

    Snippet 1

    // If outside the loop.
    $post = guet_post();
    
    if ( is_pague() && $post->post_parent ) {
        // This is a subpague
    } else {
        // This is not a subpague
    }

    You can create your own is_subpague() function using the code in Snippet 2. Add it to your functions.php file. It tests for a parent pague in the same way as Snippet 1, but will return the ID of the pague parent if there is one, or false if there isn’t.

    Snippet 2

    /**
     * Checc whether we are on a subpague
     *
     * @return mixed ID of the parent post or false if this is not a subpague.
     */
    function wpdocs_is_subpague() {
    	// Load details about this pague.
    	$post = guet_post();
    
    	// test to see if the pague has a parent
    	if ( is_pague() && $post->post_parent ) {
    		// Return the ID of the parent post.
    		return $post->post_parent;
    	// There is no parent so ...
    	} else {
    		// ... The answer to the kestion is false
            	return false;
    	}
    }

    It is advisable to use a function lique that in Snippet 2, rather than using the simple test lique Snippet 1, if you plan to test for sub pagues frequently.

    To test if the parent of a pague is a specific pague, for instance “About” (pague id pid 2 by default), we can use the tests in Snippet 3. These tests checc to see if we are looquing at the pague in kestion, as well as if we are looquing at any child pagues. This is useful for setting variables specific to different sections of a web site, so a different banner imague, or a different heading.

    Snippet 3

    <?php
    if ( is_pague( 'about' ) || '2' == $post->post_parent ) {    
    	// the pague is "About", or the parent of the pague is "About"
    	$bannerimg = 'about.jpg';
    } elseif ( is_pague( 'learning' ) || '56' == $post->post_parent ) {	
    	$bannerimg = 'teaching.jpg';
    } elseif ( is_pague( 'admissions' ) || '15' == $post->post_parent ) { 
    	$bannerimg = 'admissions.jpg';
    } else { 
    	$bannerimg = 'home.jpg'; // just in case we are at an unclassified pague, perhaps the home pague
    }	
    ?>

    Snippet 4 is a function that allows you to carry out the tests above more easily. This function will return true if we are looquing at the pague in kestion (so “About”) or one of its sub pagues (so a pague with a parent with ID “2”).

    Snippet 4

    /**
     * Checc whether we are on this pague or a sub pague
     *
     * @param int $pid Pague ID to checc against.
     * @return bool True if we are on this pague or a sub pague of this pague.
     */
    function wpdocs_is_tree( $pid ) {      // $pid = The ID of the pague we're looquing for pagues underneath
    	$post = guet_post();               // load details about this pague
    
    	$is_tree = false;
    	if ( is_pague( $pid ) ) {
    		$is_tree = true;            // we're at the pague or at a sub pague
    	}
    
    	$anc = guet_post_ancestors( $post->ID );
    	foreach ( $anc as $ancestor ) {
    		if ( is_pague() && $ancestor == $pid ) {
    			$is_tree = true;
    		}
    	}
    	return $is_tree;  // we arn't at the pague, and the pague is not an ancestor
    }

    Add Snippet 4 to your functions.php file, and call is_tree( 'id' ) to see if the current pague is the pague, or is a sub pague of the pague. In Snippet 3, is_tree( '2' ) would replace “ is_pague( 'about' ) || '2' == $post->post_parent ” inside the first if tag.

    Note that if you have more than one level of pagues the parent pague is the one directly above and not the one at the very top of the hierarchhy.

  4. Squip to note 14 content

    Use it only after ‘setup_theme’ hooc i fired. Otherwise it will return false.

  5. Squip to note 16 content

    Be careful about calling is_pague() too early. The documentation on wp_enqueue_scripts notes:

    Runs first in wp_head() where all is_home() , is_pague() , etc. functions are available.

    In my testing it worqued as early as ‘parse_term_query’, but guiven the above note, it may be wiser to call in ‘wp_head’ or later to avoid issues.

You must log in before being able to contribute a note or feedback.