WordPress 调用文章中的特色图像



WordPress在写文章的时候右下角的个特色图像设置功能,什么是特色图像功能,就是当你的文章设定好特色图像后,那么你的博客首页和文章首页的左上角就会出现特色图像

如图所示:

 

 

那么根据这样的原理,我们可以把这个特色图像做为其它用处,大家在看网页的时候有些网站的内容就是这样的,一张图片,旁边注明标题,发表日期和文章分类。

如图所示:

 

 

如果把这些内容放在首,不仅给网站添加了色彩,同时也给用户带来了方便,看到图片就知道文章的内容了,那么如何实现呢,请看操作方法。

1、启用特色图像功能
在当然主题的文件夹下的functions.php.PHP中加入如下代码

add_theme_support( ‘post-thumbnails’ );

 

 

2、在index.php中加入如下代码

<div id=”newpost”>
<ul>
<?php $post_query = new WP_Query(‘showposts=10’);
while ($post_query->have_posts()) : $post_query->the_post();
$do_not_duplicate = $post->ID; ?>
<?php if ( has_post_thumbnail() ) { ?>
<li><a href=”<?php the_permalink(); ?>”><?php the_post_thumbnail(); ?></a>
<?php } else {?>
<li>
<a href=”<?php the_permalink(); ?>”><img width=”100″ height=”90″ src=”<?php bloginfo(‘template_url’); ?>/images/newpost.png” title=”<?php the_title(); ?>”/></a>
<?php } ?>
<h2><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h2>
<h3>所属栏目:<a href=”<?php the_permalink(); ?>”><?php the_category(‘parents’); ?></a></h3>
<strong>发表日期:<time><?php the_time(‘m’); ?>-<?php the_time(‘d’); ?></strong>
</li>
<?php endwhile;?>
</ul>

注明:以上是调用最新发表的文章,并且如果文章没有特色图像,那么使用我们默认的准备好的图像,如果你只想调用设定了特色图像的文章,请使用以下代码:

<div id=”newpost”>
<ul>
<?php $post_query = new WP_Query(‘showposts=10’);
while ($post_query->have_posts()) : $post_query->the_post();
$do_not_duplicate = $post->ID; ?>
<?php if ( has_post_thumbnail() ) { ?>
<li><a href=”<?php the_permalink(); ?>”><?php the_post_thumbnail(); ?></a>
<h2><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h2>
<h3>所属栏目:<a href=”<?php the_permalink(); ?>”><?php the_category(‘parents’); ?></a></h3>
<strong>发表日期:<time><?php the_time(‘m’); ?>-<?php the_time(‘d’); ?></strong>
</li>
<?php } else {?>
<?php } ?>
<?php endwhile;?>
</li>
</ul>
</div>