How to detect if is subpage in wordpress? - php

I want to make a check if im on a subpage. On the homepage it is :
<?php if(is_front_page()): ?> <?php endif; ?>
But how to do it when it is another page?

This is literally right from the docs:
There is no function to check if a page is a sub-page. We can get around the problem:
if ( is_page() && $post->post_parent > 0 ) {
echo "This is a child page";
}

Related

Correct way to write an IF statement but without an ENDIF?

Am I doing this right? It works but wanted to ask if what I have done is bad practice?
Originally if the page is home then will get include a php file otherwise if it is a generic page will call in the featured image in WordPress.
<?php
if ( is_page('home')) {
get_template_part( 'hero' );
} ?>
<?php echo get_the_post_thumbnail($post->ID); ?>
Your code is wrong in my opinion based on what you requested in question. You only check if is home and if it is get template part 'hero'. After that no matter what page is you call get_the_post_thumbnail.
otherwise if it is a generic page will call in the featured image in WordPress
otherwise means else and your code should look like this:
<?php
if ( is_page('home')) {
get_template_part( 'hero' );
} else {
echo get_the_post_thumbnail($post->ID);
}
?>
Do not forget to be in loop to call $post->ID
You can aford endif by using <?php if(){ ... } ?> or <?php if(){ ?> ... <?php } ?>
no, it's okay. If the condition is not fulfilled, nothing will happen.

how to display wordpress title with conditional if else?

I added a title section in WordPress theme option for site title. I want that if that field is empty then it will display wp_title regular title with front page condition or the field is fill up then the title will display what written in the field. I tried this
<title>
<?php if (is_home()) {
$website_title = ot_get_option('website_title');
if (isset($website_title) && $website_title != "") {
echo $website_title;
} else {
is_front_page() ? bloginfo('description') : wp_title(''); bloginfo('name');
}
} else {
is_front_page() ? bloginfo('description') : wp_title(''); bloginfo('name');
}
?>
</title>
But this code works great in localhost of my computer and when i upload this code in website online it works as regular wp_title, no change happen from theme option. But from localhost theme option change is correctly.
If someone any idea please.

WordPress : Simple php IF statement not processing

can someone explain why the else statement is not working in WordPress for the sidebar.php?
<?php if(is_front_page() ) : ?>
**content shows on main page sidebar**
<?php elseif(!is_front_page() ) : ?>
<?php // else: ?> // tried **else:** also
**some content**
**nothing is shown on any other page...**
<?php endif;?>
The is_front_page() conditional will always return false if it is used inside the loop or after the loop has ran (like in a sidebar). You can call wp_reset_query(); after your loop to reset the page query_vars.
See this answer on WPSE for more info.
Im not sure if is_front_page() exists, but just use {} around each condition result:
<?php if(is_front_page() ) { ?>
**content shows on main page sidebar**
<?php } else { ?>
// tried **else:** also
**some content**
**nothing is shown on any other page...**
<?php } ?>

Wordpress: Condition Tags PHP

This may be a simple one for you PHP experts out there. I need to give a certain <h1> to a post else show the page/post title.
I have this so far, it works if it is on a single post page, but when I am on a different page it just shows 'the_title' instead of the page title. I think its basically about calling a php function inside an already open php tag, if that makes sense. Here is the code:
<?php
if ( is_single() ) {
echo 'News';
} else {
echo the_title();
}
?>
The Wordpress tag for the page title is <?php the_title ?>
You are echoing 'the_title' as a string, you need to actually execute the function like so:
if ( is_single() ) {
echo '<h1>News</h1>';
} else {
echo '<h1>' . the_title() . '</h1>';
}
Note the closing quote to halt the string, and the . to concatenate the WordPress function the_title(), and then another to join the ending <h1> tag.
A cleaner way is to add the tags inside the function itself, like this:
<?php the_title('<h1>', '</h1>'); ?>
No need for 'echo'.

Wordpress PHP Syntax

I'm trying to create a PHP 'if' statement that tells an image to display on certain pages in Wordpress. What I'm trying to do is shown below, but I think the syntax is incorrect.
I'm trying to say "If the page displayed equals 'about' or 'gallery' and that page is not 'services', go ahead and display the image.
Here's the section of code that you should be able to analyze and see where I went wrong:
<?php if(is_page('about' || 'gallery') && (is_page != 'services')) { ?>
<li><img id="profile_pic" alt="DermaKare (Official Fan Page)" src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/50556_123510051042639_3343345_n.jpg" class="photo img" /></li>
<?php } ?>
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Banner") ) : ?>
<?php /*?><p><a href='<?php bloginfo('siteurl')?>/wp-admin/widgets.php'>Widgetize this sidebar</a></p><?php */?>
<?php endif; ?>
Thanks in advance for any help on this.....
If a page is about or gallery then it never is services. So you can spare that not part.
Next to that you need to call the function twice:
if (is_page('about') || is_page('gallery'))
{
# then
}
This should do it. Wrapped into your code this should look like:
<?php if (is_page('about') || is_page('gallery')) { ?>
if ( ( is_page('about') || is_page('gallery') ) && !is_page('services') )
watch the brackets.
edit: and what #hakre said, if it's about or gallery, it never is services
<?php if( (is_page('about') || is_page('gallery') ) && (!is_page('services') ) {
echo "<li><img id=\"profile_pic\" alt=\"DermaKare (Official Fan Page)\" src=\"http://profile.ak.fbcdn.net/hprofile-ak-snc4/50556_123510051042639_3343345_n.jpg\" class=\"photo img\" /></li>";
} ?>
this should work

Categories