samedi 20 décembre 2014

If and else "==" not working

I'm trying to show different text on WordPress based on a value from a select box. I have written an "if and else" statement but it isn't working for some weird reason. Here is my code:



<?php global $wpUserStylesheetSwitcher;$wpUserStylesheetSwitcher->show_wp_user_stylesheet_switcher(array('list_title'=>'Change theme', 'show_list_title'=>'true', 'list_type'=>'blogstyle'));
$gdgt_theme = '<script>var yourSelect = document.getElementById( "gdgt-test" );document.write( yourSelect.options[ yourSelect.selectedIndex ].value )</script>';
echo $gdgt_theme;
if ($gdgt_theme == 0) { ?>
<article class="gdgt-article">
<div class="gdgt-loop-post">
<a href="<?php echo get_permalink(); ?>" title="<?php echo the_title(); ?>">
<div class="gdgt-loop-thumb">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('gdgt_loop_thumb');
}
else {
echo '<img src="http://ift.tt/1AoDALp" title="Image not available yet" alt="image-coming-soon"/>';
}
?>
<div class="loop-overlay"></div>
</div>
<div class="gdgt-post-meta">
<h1><?php echo the_title_limit(70, '…'); ?></h1>
</a>
<div class="gdgt-article-meta">
by <?php the_author_meta( 'display_name' ); ?> - <?php echo the_time(); ?>
</div>
<div class="gdgt-post-excerpt">
<?php echo "test" ?>
</div>
</div>
</div>
</article>
<?php }

else {
?>
<article class="gdgt-article">
<div class="gdgt-loop-post">
<a href="<?php echo get_permalink(); ?>" title="<?php echo the_title(); ?>">
<div class="gdgt-loop-thumb">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('gdgt_loop_thumb');
}
else {
echo '<img src="http://ift.tt/1AoDALp" title="Image not available yet" alt="image-coming-soon"/>';
}
?>
<div class="loop-overlay"></div>
</div>
<div class="gdgt-post-meta">
<h1><?php echo the_title_limit(70, '…'); ?></h1>
</a>
<div class="gdgt-article-meta">
by <?php the_author_meta( 'display_name' ); ?> - <?php echo the_time(); ?>
</div>
<div class="gdgt-post-excerpt">
<?php echo "else works"; ?>
</div>
</div>
</div>
</article>
<?php }
?>


Code Explanation: First I try to get the value from the select box using a simple JS code. I then echo it so that I can see it is returning the correct value. Then I run the if and else


The Problem: When the value == 0, the "test" text shows up, but when it isn't equal to 0, the text "test" still shows up. I'm not sure what's wrong with my code. Please help.


Aucun commentaire:

Enregistrer un commentaire