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 } ?>
Related
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.
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";
}
I am trying to achieve an outcome that combines two plugins in WordPress.
Basically, I am using Easing Slider Pro and Advanced Custom Fields. When the website owner edits a page, I want them to be able to add a slideshow by simply entering the slideshow ID into an Advanced Custom Field called 'slider'.
This is how one would normally add the PHP to display a slideshow:
<?php if ( function_exists('easingsliderpro') ) { easingsliderpro( 5 ); } ?>
The 5 is an example of a slideshow ID that can be changed.
Here is the PHP for the advanced custom field:
<?php if( get_field('slider') ): ?><?php the_field('slider'); ?><?php endif; ?>
Both of these work fine by themselves. But I want a way to combine these two pieces of code so that in the page editor the website manager only has to enter the ID of the slideshow. I don't know a lot about PHP and I am often confused by it, but this was my initial attempt:
<?php if( get_field('slider') ): ?>
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) { easingsliderpro( <?php the_field('slider'); ?> ); } ?></div>
<?php endif; ?>
It didn't work, I am assuming because you're not allowed to have PHP code within PHP code. Is there any workaround that anyone knows that could make this achievable?
Many thanks.
Am I crazy? Can't you just:
AHA!
I think I see the confusion: the_field echoes the value out, so it gets passed to easingsliderpro() as just true, and displays the value.
You need to use a function that returns the value, so you can pass it to the next function.
In this case, it's get_field():
<?php if( get_field('slider') ): ?>
<div id="sliderframe">
<?php
if ( function_exists('easingsliderpro') ) :
easingsliderpro( get_field('slider') );
endif;
?>
</div>
<?php endif; ?>
See more in the documentation:
http://www.advancedcustomfields.com/resources/functions/get_field/
You shouldn't put php open close tags within a php open/close tag.
For your code above, this is valid:
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) {
easingsliderpro(the_field('slider'));
} ?></div>
I'm trying to use the is_page function in wordpress and it's not working.
I read here that <?php wp_reset_query(); ?> should solve the problem, but it didn't help. Here is the code:
<?php wp_reset_query(); ?>
<?php if (is_page(379) ): ?>
<!-- Google Analytics Content Experiment code -->
<?php endif; ?>
Any idea/suggestions on this?
Thanks!
None of the other solutions worked for me. What did work was to use the wp hook.
add_action('wp', 'page_check');
function page_check() {
if (is_page('my-page-slug')) {
// code to run on this page
}
}
Try this
<?php if (get_the_ID()==379): ?>
<!-- Google Analytics Content Experiment code -->
<?php endif; ?>
this works fine for me
<?php if(is_page(379)): ?>
<?php echo 'x'; ?>
<?php endif; ?>
I had a similar problem recently and it's because I was using query_posts somewhere on the page.
The way I tested it was to echo the $post->ID at the very top of the template file and it gave me the correct ID.
when I echoed it at the bottom (after my query_posts), it was not getting the correct ID.
So you might want to test by doing this and changing any query_posts you have to use WP_Query instead: http://codex.wordpress.org/Class_Reference/WP_Query
I am trying to do an if/else-statement on my WP-template. But i can't seem to pick the right element.
I wan't it to look for the latest post on the blog. I already added a script that add a class of .first to the latest post.
What im trying to do in dummy-php-language:
<?php
if ( class is .first) { do something to .first}
else () { do something to everything else }
?>
i've looked in the wp-documentation. But i can't seem to find anything that will select "latest post"...
Thanks
Jonas
A very simple solution would be to keep a count of the post you're at, and do something if it's the first. In the index.php, in the have_posts() loop:
<?php if (have_posts()) : $count = 0; ?>
<?php while (have_posts()) : the_post(); $count++; ?>
<?php if($count ==1) ?>
// do something to first
<?php else ?>
// do something to everything else
<?php endif; ?>
<?php endwhile; else: ?>
<?php endif; ?>