0

我正在这里开发我的开发服务器;

http://www.trevorpeters.co.uk/plainarts/?page_id=11

我有一个帖子列表,我在 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; ?>
4

2 回答 2

2

is_category“检查是否正在显示类别存档页面。” . 您似乎正在尝试使用来确定特定帖子是否属于某个类别,这不是它的本意。你可能需要has_category. 尝试:

if (has_category('featured')) :
于 2013-03-14T13:56:48.570 回答
0

尝试获取类别 ID 而不是类别名称

<?php 
       // Lets suppose you're looking for the category ID 16
       $category = get_the_category();
       if($category->cat_ID == 16) :
?>
于 2013-03-14T13:56:15.250 回答