Show content if child of a specific page - php

I am building a website in WordPress and have a set of photos that display along the top of the page (in the background) and have them to where the set of photos can change depending on where the visitor is on the website. For example, if they are looking at the FAQ page, then photos pertaining to asking questions are shown. If they are looking at the news page, then photos pertaining to news are shown. The code I have works great, however, I'd like to now show photos based on the parent of the page they are on; for instance, if they are looking at a child page under a parent.
Here's my code as it stands now:
<?php if ( is_front_page() ) { include'media/home.php'; }
elseif ( is_404() ) { include'media/404s.php'; }
elseif ( is_page('news') ) { include'media/home.php'; }
elseif ( is_page('faqs') ) { include'media/faqs.php'; }
elseif ( is_category('africa-2') ) { include'media/faqs.php'; }
else { include'media/general.php'; }
?>
How can I tell WordPress, I want it to show a certain php file if it is a parent page and all of it's children?

If you're in the loop you can just use
if( is_page('faqs') && $post->post_parent ){
//include template here
}
If you're outside the loop you'll need to include global $post before the conditional.
global $post
You can reference this link http://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages

Related

Copy advanced custom field content from other page using if statement

Im trying to copy some content from another page with an if statement that has an input from advanced custom fields so I dont have to manage two pages with the same content. However the code has a bug in it which prevents the page from loading when this section starts. I guess I made a type somewhere. What could it be?
The first block contains the information from post id 4767 that i want to display on post id 4940. The second block is the information that would normally be loaded and is entered on all the individual pages.
<?php if(is_single('4940')) {
if( have_rows('partner', 4767) ):
while ( have_rows('partner', 4767) ) : the_row();
$partnerTag = get_sub_field('partner_tag', 4767);
$partnerName = get_sub_field('partner_name', 4767);
$partnerImage = get_sub_field('partner_image', 4767);
$partnerImageUrl = $partnerImage['sizes']['medium'];
$partnerLink = get_sub_field('link', 4767);
} else {
if( have_rows('partner') ):
while ( have_rows('partner') ) : the_row();
$partnerTag = get_sub_field('partner_tag');
$partnerName = get_sub_field('partner_name');
$partnerImage = get_sub_field('partner_image');
$partnerImageUrl = $partnerImage['sizes']['medium'];
$partnerLink = get_sub_field('link');
} ?>
ps. I tried both: is_single('4940') and is_single(4940)
You could just check the value of $post->ID directly.
if( $post->ID == 4940 ) { ... }
Are you within the query? is_single() and is_singular() will not work outside loop.

is_home not working in wordpress when using templates

I have a div of services (full width) which I only want to display on the home page of my Wordpress site. I have used the conditional tag if(is_home()) and it is working fine. But when I added a new template this division is showing up in the template page, too. I tried using:
(if(is_home())&&(!is_page_template('blog.php'))
...but unfortunately it is not working. I have also tried it using ID and slug, and yet still this particular div is coming in the template.
I some how want this div to show up only on the homepage. You can see the services div here. The same is being displayed here, but not in other inner pages.
Use is_front_page() instead.
See Here
This code
<?php if (is_home()) : ?>
// yes, home page
<?php else : ?>
//no, not home page
<?php endif; ?>
Simply use
if(is_home() && !is_page_template('blog.php'))
Everything Should be in one condition
You can do it with any of the following conditional tags
if( is_home() && !is_page_template( 'page- template-Blog-php' ) ) {
// service div here
}
Note: If you are using template in any folder you need to give folder path too.
For example: If you are placing all the templates in 'templates' folder your code will be:
if( is_home() && !is_page_template( 'templates/page-template-Blog-php' ) ) {
// service div here
}
or you can try following code:
if( is_home() || is_front_page() ) {
// service div here
}
Braces wrongly placed:
if( (is_home()) && (!is_page_template('blog.php')) )

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

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
}
?>
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
}
?>
Put this function in the functions.php file of your theme.
function is_page_child($pid) {// $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
$anc = get_post_ancestors( $post->ID );
foreach($anc as $ancestor) {
if(is_page() && $ancestor == $pid) {
return true;
}
}
if(is_page()&&(is_page($pid)))
return true; // we're at the page or at a sub page
else
return false; // we're elsewhere
};
Then you can use it:
<?php
if(is_page_child(100)) {
// show image X
}
?>
I know this is an old question but I was searching for this same question and couldn't find a clear and simple answer until I came up with this one. My answer doesn't answer his explanation but it answers the main question which is what I was looking for.
This checks whether a page is a child or a parent and allows you to show, for example a sidebar menu, only on pages that are either a child or a parent and not on pages that do not have a parent nor children.
<?php
global $post;
$children = get_pages( array( 'child_of' => $post->ID ) );
if ( is_page() && ($post->post_parent || count( $children ) > 0 )) :
?>
For Wordpress, you can simply check:
<?php
if (wp_get_post_parent_id(get_the_ID())) {
echo "I am a child page";
}
?>
You can use the get_pages() function. it takes an associative array as an argument. you can give that array 'child_of' => get_the_ID() to get the children of the current page, and if it hasn't any children the whole get_pages() function will return false, otherwise it will return a value that evaluates to true, which can be assigned to a variable to use as a conditional in an if statement.
$iAmParent = get_pages(array('child_of' => get_the_ID()));

Whats the url that displays all posts in wordpress?

I simply want a link to my blog archives. They are just normal posts, but I cannot seem to find the right url for them to add to my menu.
right url for them to add to my menu.
http://yourwebsite.com/?post_type=post
You don't have to necessarily use a category to every post.
Actually the file to list all posts of any category and date is the index.php. You just write 'the loop' as told on codex.
So if you changed your index.php to make it as a fancy page and not the post list only, now you're trying to create another page to do that for you.
So, if you follow me, you're doing this the wrong way. What you should do is to create a separate page and assign it as the home page of your blog. It would then free the index.php file for you to use it as the blog list, as it is by default.
Assuming that you did it the correct way (as mentioned by Guilherme), you should have a page designated as the blog list page.
The URL for the blog list if using the page 'My Blog' to display posts and pretty links for the url should be something like http://mywebsite.com/my-blog.
<?php if ( is_front_page() && is_home() ) {
// Default homepage
echo "Default homepage";
} elseif ( is_front_page()){
echo "Static homepage";
// Static homepage
} elseif ( is_home()){
echo "Blog page";
// Blog page
} elseif ( is_page( 'cart' ) || is_cart()){
echo "cart";
// Blog page
} elseif (is_single()){
echo "is_single";
// Blog page
} elseif (is_product_category()){
echo "is_product_category";
}
else {
echo "Everything else";
// Everything else
} ?>

Wordpress highlight current page link

What's the best way to approach this problem:
I have a list of navigation links in the sidebar, which must all have different, specific colours when hovered over, they are all the same colour the rest of the time. They are also nested (manually, is there an easy way to do hierarchical pages too?). The links all point to Wordpress pages. I want to be able to set the link to the current page to be permanently colourful (not just on hover).
Other than hard-coding this checking page-ids, how could I tackle this problem?
Found this helpful when I was doing it. From the Wordpress Codex.
if ( is_page('Page One') ) { $current
= 'one'; } elseif ( is_page('Page Two') ) { $current = 'two'; } elseif (
is_page('Page Three') ) { $current =
'three'; } elseif ( is_page('Page
Four') ) { $current = 'four'; }
Wordpress already has page identification built in. The is_page function accepts the page name as the variable. Just toss that in your id or class and you're golden.

Categories