我正在尝试根据选择框中的值在WordPress上显示不同的文本。我写了一个“if and else”的声明,但它并不是出于一些奇怪的原因。这是我的代码:
<?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://for-b.tk/_ph/13/934023873.gif" 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://for-b.tk/_ph/13/934023873.gif" 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 }
?>
代码说明:首先,我尝试使用简单的JS代码从选择框中获取值。我然后echo
它,以便我可以看到它返回正确的值。然后我运行if
和else
问题:当值== 0时,“test”文本显示,但是当它不等于0时,文本“test”仍会显示。我不确定我的代码有什么问题。请帮忙。