PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 17, 2022

[FIXED] Check if a page is a parent or if it's a child page?

 February 17, 2022     php, wordpress     No comments   

Issue

Is it possible to check if a page is a parent or if it's a child page?

I have my pages set up like this:

-- Parent

---- Child page 1

---- Child page 2

etc.

I want to show a certain menu if it's a parent page and a different menu if it's on the child page.

I know I can do something like below but I want to make it a bit more dynamic without including specific page ID's.

<?php
if ($post->post_parent == '100') { // if current page is child of page with page ID 100
   // show image X 
}
?>

Solution

You can test if the post is a subpage like this:
*(from http://codex.wordpress.org/Conditional_Tags)*

<?php

global $post;     // if outside the loop

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

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


Answered By - Alex
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing