if else语句不能在Wordpress中工作
问题描述:
我正在我的开发服务器上工作;if else语句不能在Wordpress中工作
我的职位名单,我已经在WordPress创建了一个“特色”类别,当我这个类别中选择一个文章,我想CSS样式应用到它。
这是我正在使用的代码,但不知何故<?php if (is_category('featured')) : ?>
无法找到特色帖子!您可以在下面查看我的代码。
<?php if(have_posts()) :
$wud = wp_upload_dir();
$width = get_option('thumbnail_size_w');
$height = get_option('thumbnail_size_h');
remove_filter('get_the_excerpt', 'wp_trim_excerpt'); ?>
<ul class="cat-posts">
<?php while(have_posts()) : the_post();
$title = get_the_title(); ?>
<?php if (is_category('featured')) : ?>
<li class="featured cat-post cat-post-<?php the_ID(); ?>">
<?php else : ?>
<li class="cat-post cat-post-<?php the_ID(); ?>">
<?php endif; ?>
<a href="<?php the_permalink(); ?>" class="post-image" title="<?php echo $title; ?>"><img src="<?php echo $wud['baseurl'] . '/thumb-' . sanitize_title($title) . '-' . $width . 'x' . $height; ?>.jpg" alt="<?php echo $title; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" /></a>
<a href="<?php the_permalink(); ?>" title="<?php echo $title; ?>"><h2><?php echo $title; ?></h2></a>
<div class="post-intro">
<?php if ($intro != "") :
echo (function_exists('te_obfuscate_email')) ? te_obfuscate_email(str_replace('>http://', '>', make_clickable(str_replace(array("\r\n", "\r", "\n"), "<br />", $intro)))) : str_replace('>http://', '>', make_clickable(str_replace(array("\r\n", "\r", "\n"), "<br />", $intro)));
elseif ($use_content) :
echo neat_trim(strip_tags (str_replace(".", ". ", str_replace(array("\r\n", "\r", "\n"), "<br />", get_the_content('', TRUE))), '<p>'), $max_chars);
endif; ?>
</div>
</li>
<?php endwhile; ?>
</ul>
<div style="clear: both"></div>
<div class="navigation">
<?php if(function_exists('wp_pagenavi')) {
wp_pagenavi();
} else {
posts_nav_link();
} ?>
</div>
<?php else : ?>
<h2><?php _e('Not Found'); ?></h2>
<?php endif; ?>
答
is_category
"checks if a Category archive page is being displayed."。你似乎试图用来确定一个特定的帖子是否属于一个类别,这不是它的意图。您可能需要has_category
。尝试:
if (has_category('featured')) :
+0
工作正常!你先生是钻石! – 2013-03-14 13:58:47
答
尝试获得了categoty ID而不是类别名称
<?php
// Lets suppose you're looking for the category ID 16
$category = get_the_category();
if($category->cat_ID == 16) :
?>
http://codex.wordpress.org/Function_Reference/is_category 'is_category'检查该页面是否在类别中,什么是你想达到什么目的? – Luceos 2013-03-14 13:53:54
我试图突出显示帖子列表中的“精选”帖子。通过将帖子添加到“精选”类别来定义精选帖子 – 2013-03-14 13:55:52