小冬SEO

wordpress调用文章图片做缩略图

2020-03-05 23:51:55 3699 WordPress笔记

为了更好的体验,我们需要在wordpress程序的首页或者列表页中,实现文档有图片就显示图片,无图片就显示默认图片,具体方法如下:

1. 获取图片路径,无则输出默认图片

找到主题模板文件中的functions.php文件,在文件中加入以下代码:

//判断文章中是否有图片
if ( function_exists('add_theme_support') )
add_theme_support('post-thumbnails');
function catch_first_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 = bloginfo('template_url'). '/images/default.jpg';
}
return $first_img;
}

代码说明:改代码是判断调用的文档中是否有图片,如果没有图片,则输入默认的一张图片/images/default.jpg,默认的图片名称路径自己可以修改。

在需要调用图片的位置添加如下代码:

<?php echo catch_first_image() ?>

2.获取图片路径,无则随机输出图片

还是在functions.php文件中插入如下代码:

//支持外链缩略图
if ( function_exists('add_theme_support') )
 add_theme_support('post-thumbnails');
function catch_first_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)){
		$random = mt_rand(1, 10);
		echo get_bloginfo ( 'stylesheet_directory' );
		echo '/images/random/'.$random.'.jpg';
		}
  return $first_img;
}

然后在该/images/random/目录中放置10张图片,这样就可以随机展示出图了。

3. 判断内容中有无图片,输出含html的图片代码

将以下代码贴入主题的function.php文件:

//thumbnails
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 200, 140 ,true );
function don_the_thumbnail() {
    global $post;
    // 判断该文章是否设置的缩略图,如果有则直接显示
    if ( has_post_thumbnail() ) {
        echo '<a href="'.get_permalink().'">';
        the_post_thumbnail();
        echo '</a>';
    } else { //如果文章没有设置缩略图,则查找文章内是否包含图片
        $content = $post->post_content;
        preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
        $n = count($strResult[1]);
        if($n > 0){ // 如果文章内包含有图片,就用第一张图片做为缩略图
            echo '<a href="'.get_permalink().'"><img src="'.$strResult[1][0].'" /></a>';
        }else { // 如果文章内没有图片,则用默认的图片。
            echo '<a href="'.get_permalink().'"><img src="'.get_bloginfo('template_url').'/img/thumbnail.jpg" /></a>';
        }
    }
}

调用代码

<?php echo don_the_thumbnail() ?>

4.当有画廊的时候,调用文章中的第一张图片做缩略图

将以下代码贴入主题的function.php文件:

//兼容gallery获取首图
function get_img_src($str,$num) {
    preg_match_all('/(src)=("[^"]*")/i', $str, $matches);
    $ret = array();
    foreach($matches[0] as $i => $v) {
        $ret[] = trim($matches[2][$i],'"');
    }
    return $ret[$num];
};

调用代码如下

<?php echo get_img_src(get_the_content(),0) ?>

5.另外一种调用缩略图的方式

在functions.php函数中添加如下代码

//获取文章第一张图片
function get_content_first_image($content){

if ( $content === false ) $content = get_the_content();

preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $content, $images);

if($images){

return $images[1][0];

}else{

return false;

}

}
//end

调用代码如下

<?php echo get_content_first_image(get_the_content()); ?>

6.wordpress获取文章第一张图,输出含有html的缩略图

在functions.php函数中添加如下代码

//获取指定图片并判断输出框架
function get_post_this_img($index) {
    global $post, $posts;
    $content = $post->post_content;
    preg_match_all('/(src)=("[^"]*")/i', $content, $matches);
    $ret = array();
    foreach($matches[0] as $i => $v) {
        $ret[] = trim($matches[2][$i],'"');
    };
    if(count($ret)>=1){
        echo '<div class="viewimg">< a href="'.get_permalink().'" class="ipic">< img class="thumbnail" src="'.$ret[$index].'"><span class="shine" style="background-position: 160px center;"></span></ a></div>';
    }
};

用如下代码来调用

<?php get_post_this_img(0) ?>

版权保护: 本文由小冬SEO编辑发布,转载请保留链接: http://www.cdseoyh.cn/shuo/111.html

小冬SEO 草根seoer,从事搜索引擎关键词优化,喜欢学习专研各类SEO优化技巧—小冬SEO博客
  • 138文章总数
  • 3700本页访问
  • 运营时间
  • 标签

    友情链接

      {dede:sql sql="select url,webname from dede_flink where typeid = (select id from dede_flinktype where typename=~typename~)"}
    • [field:webname/]
    • {/dede:sql}