If parent slug equals

I’m trying to write an if statement where, if a child page’s has a slug equal to a specific value, a different statement is echoed. Regardless of the slug value, the function always returns the else value rather than any other.

<?php 
    global $post;
    $post_data = get_post($post->post_parent->post_name);
    if ($post_data == 'slug-one'){
        $ticket = 'Cats';
    } elseif ($post_data == 'slug-two') {
        $ticket = 'Dogs';   
    } else {
        $ticket = 'Birds';  
    }

    echo $ticket;
?> 

Any ideas as to how I can better write the statement, or what the error occurring is?

Related posts

1 comment

  1. As it turns out, I shouldn’t have called $post_data = get_post($post->post_parent->post_name). My fixed code is below. Thanks for the advice everyone.

    <?php 
    global $post;
    $post_data = get_post($post->post_parent);
    
    if ($post_data->post_name == 'in-the-city'){
        $ticket = 'Cats';
    } elseif ($post_data->post_name == 'on-the-beach') {
        $ticket = 'Dogs';   
    } else {
        $ticket = 'Birds';  
    }
    
    echo $ticket;
    ?>
    

Comments are closed.