wordpress怎样实现调用指定分类文章里的图片 标题 和摘要

我的问题是 调出指定分类下文章里的图片 +文章标题+内容摘要

在Function.php里面加入一段获取首张图片的函数:
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
$first_img = get_bloginfo('template_directory')."/images/default.jpg";
}
return $first_img;
}

调用代码(写出大致功能实现方法,具体样式请自己修改):

<?php
query_posts(cat=分类ID,showposts=调用数量);
?>
<ul>
<?php if (have_posts()) :while (have_posts()) : the_post(); ?>
<li>
<img src="<?php echo catch_that_image(); ?>" alt="<?php the_title(); ?>" />//缩略图

<a href="<?php the_permalink() ?>" ><?php the_title(); ?></a>//标题
/*文章摘要开始*/

<?php
if ($post->post_excerpt) {
echo $post->post_excerpt;
}
else{
echo cut_str(strip_tags(apply_filters('the_content', $post->post_content)),40,"…");
}
?>
/*文章摘要结束*/

</li>
<?php endwhile; endif; ?>
</ul>
<?php wp_reset_query(); ?>追问

请问你这些哪里该放到Function.php 里 哪些是调用啊

温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2016-03-20
调用wordpress指定分类文章第一种方法
<?php $posts = get_posts( "category=4&numberposts=10" ); ?><?php if( $posts ) : ?><ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?><li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li><?php endforeach; ?></ul><?php endif; ?>

调用wordpress指定分类文章第二种方法
<h2><?php wp_list_categories('include=11&title_li=&style=none'); ?></h2> //输出 ID 为11的分类的标题
<?php echo category_description(11); ?> //输出 ID 为11的分类的描述
<?php query_posts('showposts=5&cat=11'); ?> //query_posts 给 The Loop 限定的条件是:显示5篇日志和分类 ID 为11
<?php while (have_posts()) : the_post(); ?> //The Loop 开始
<li><a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php the_title(); ?></a></li> //用列表的方式输出带有链接的文章标题
<?php endwhile; ?> //The Loop 结束追问

请问图片怎样同时调出来呢

本回答被网友采纳
第2个回答  2012-11-30
这个貌似比较难额。
你看看下面这段代码吧,不能完全解决你问题,但是应该有点帮助。
<?php $recentPosts = new WP_Query(); //新建一个wp_query类型的变量$recentPosts
$recentPosts->query('cat=1&showposts=999999); //设置该该变量含有的文章列表为分类ID=1且数量=999999,就可以输出一个分类下的所有文章了。
?>
<ul>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li><a href="<?php the_permalink() ?>"rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
当然上面的代码只能输出特定分类下的标题。
摘要的话下面这段代码可以输出文章前220个字符,勉强可以做摘要。
<?php echo mb_strimwidth(strip_tags(apply_filters('the_excerpt', $post->post_content)), 0, 220,"..."); ?>
缩略图我确实不会。。。
相似回答