views:

42

answers:

1

I have been using conditionals to be able to tell if I'm on a page or a subpage of that page, but I cannot for the life of me get the code to do one thing if I'm on the master page,and another thing if I'm on a subpage of that master page. This is what I have been using so far:

if ( is_page('3') || $post->post_parent == '3' ){}

Does anyone know how to check for page and subpage? In ExpressionEngine, I would use the URL slugs, but I'm not sure how to do this in WordPress. Thanks in advance!

A: 

did you try this from wordpress codex

Testing for sub-Pages

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

Snippet 1

<?php

 global $post;     // if outside the loop

 if ( is_page() && $post->post_parent )
 {
     // This is a subpage

 } else {
     // This is not a subpage
 }
?>


Using the URL

$url = '/shop/';
$url2 = '/shop/category/accessories';

function isSubPage( $page, $url = null ) {

    if ( strtolower(trim( $url, '/' )) == $page ) {
     return false;
    }
    return true;

}

var_dump(isSubPage( 'shop', $url )); //false
var_dump(isSubPage( 'shop', $url2 )); //true

for the second argument you're goign to want to put $_SERVER['REQUEST_URI'] or whatever variable you can get the request from

var_dump(isSubPage( 'shop', $_SERVER['REQUEST_URI'] ));
Galen
Hrm, that's what I have been using to test against specific parents and children pages. Maybe this is different, since I am using the Shopp plugin. I have a parent page of .com/shop/ and then clicking a category of products brings me to .com/shop/category/accessories/ and the current code isn't working. I was hoping to be able to check against URLs… any idea on how I could achieve that?
bccarlso
Thank you Galen, this works great!
bccarlso