WordPress如何定义指定文章的样式

作者:     目录: PHP+MySQL     发表: 2009年12月12日

通过post_class() 函数及post ID属性来定义指定文章或某些类别的文章的样式。

 通过修改<div <?php post_class() ?> id=“post-<?php the_ID(); ?>”> ,来实现定义指定文章或某些类别文章的样式。

打开single.php,将其中有关文章内容部分的代码修改成如下形式:

<?php if (have_posts()) : ?>
  <?php while (have_posts()) : the_post(); ?>
  <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
  <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
  <?php the_content(); ?>
  </div>
  <?php endwhile; else: ?>
  <?php _e('Sorry, no posts matched your criteria.'); ?>
  <?php endif; ?> 

 

主要是第三行代码部分,添加<?php post_class() ?> 及<?php the_ID(); ?> 
通过上述代码后,我们察看具体文章的源代码就会发现如下样式的代码:

<div class=”post sticky hentry category-design tag-news tag-wordpress tag-markup”  id=”post-23″>。

如果需要定义指定类别的的文章样式,只需定义下列类:

 .post
 .hentry
 .sticky
 .category-misc
 .tag-news
 .tag-wordpress
 .tag-markup

如果需定义指定某篇文章的样式,只需定义post-ID即可。

例如:

#post-23{
 background:#aaa;
	}  

发表评论