您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程
Wordpress调用文章第一张图片的实现方法
含蕾2025-03-26WordPress教程已有人查阅
导读之前设计WordPress主题的时候调用图片一般都是用文章内附件图片,但是有些博主为了节约博客主机空间,大部分采用外联图片,这样就无法同过这种方式调用了,所以只能用下面的这种方
之前设计WordPress主题的时候调用图片一般都是用文章内附件图片,但是有些博主为了节约博客主机空间,大部分采用外联图片,这样就无法同过这种方式调用了,所以只能用下面的这种方式来调用文章的第一张图片,WordPress调用文章第一张图片代码如下:
1 在WordPress主题的功能函数function.php文件内添加以下代码,这些 代码主要是查找文章内有没有图片并调用第一张图片地址,其工作原理是查找文章内有没<img />这个标签,如果有就调出第一张图片,如果没有就用张设计好的图片代替,这个方式用来作为文章缩略图非常有用,具体代码如下,拷贝到 function.php <?php … ?>之间即可.
全文如下:
1 在WordPress主题的功能函数function.php文件内添加以下代码,这些 代码主要是查找文章内有没有图片并调用第一张图片地址,其工作原理是查找文章内有没<img />这个标签,如果有就调出第一张图片,如果没有就用张设计好的图片代替,这个方式用来作为文章缩略图非常有用,具体代码如下,拷贝到 function.php <?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)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
Wordpress 主题模板调用 catch_that_image()函数,方法很简单,在需要的地方插入
<img src="<?php echo catch_that_image() ?>" alt="<?php printf( esc_attr__( 'Permalink to %s', 'catchbox' ), the_title_attribute( 'echo=0' ) ); ?>.ebingou.cn 代码号" />
即可,例如我是在首页插入,我修改了Content.php全文如下:
<?php
/**
* The default template for displaying content
*
* @package Catch Themes
* @subpackage Catch_Box
* @since Catch Box 1.0
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if ( is_sticky() ) : ?>
<hgroup>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'catchbox' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<h3 class="entry-format"><?php _e( 'Featured', 'catchbox' ); ?></h3>
</hgroup>
<?php else : ?>
<h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'catchbox' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
<?php endif; ?>
<?php if ( 'post' == get_post_type() ) : ?>
<div class="entry-meta">
<?php catchbox_posted_on(); ?>
<?php if ( comments_open() && ! post_password_required() ) : ?>
<span class="sep"> - </span>
<span class="comments-link">
<?php comments_popup_link(__('No Comments ↓', 'catchbox'), __('1 Comment ↓', 'catchbox'), __('% Comments ↓', 'catchbox')); ?>
</span>
<?php endif; ?>
</div><!-- .entry-meta -->
<?php endif; ?>
</header><!-- .entry-header -->
<?php
$options = catchbox_get_theme_options();
$current_content_layout = $options['content_layout'];
?>
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php elseif ($current_content_layout=='excerpt' ) : // Only display Featured Image and Excerpts if checked in Theme Option ?>
<div class="entry-summary">
<?php if( has_post_thumbnail() ):?>
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'catchbox' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<?php the_post_thumbnail('featured-slider'); ?>
</a>
<?php endif; ?>
<img src="<?php echo catch_that_image() ?>" alt="<?php printf( esc_attr__( 'Permalink to %s', 'catchbox' ), the_title_attribute( 'echo=0' ) ); ?>.ebingou.cn 代码号" /> <?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'catchbox' ) ); ?>
<?php wp_link_pages( array(
'before' => '<div class="page-link"><span class="pages">' . __( 'Pages:', 'catchbox' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
) );
?>
</div><!-- .entry-content -->
<?php endif; ?>
<footer class="entry-meta">
<?php $show_sep = false; ?>
<?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
<?php
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( __( ', ', 'catchbox' ) );
if ( $categories_list ):
?>
<span class="cat-links">
<?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'catchbox' ), 'entry-utility-prep entry-utility-prep-cat-links', $categories_list );
$show_sep = true; ?>
</span>
<?php endif; // End if categories ?>
<?php
/* translators: used between list items, there is a space after the comma */
$tags_list = get_the_tag_list( '', __( ', ', 'catchbox' ) );
if ( $tags_list ):
if ( $show_sep ) : ?>
<span class="sep"> | </span>
<?php endif; // End if $show_sep ?>
<span class="tag-links">
<?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'catchbox' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list );
$show_sep = true; ?>
</span>
<?php endif; // End if $tags_list ?>
<?php endif; // End if 'post' == get_post_type() ?>
<?php if ( comments_open() ) : ?>
<?php if ( $show_sep ) : ?>
<span class="sep"> | </span>
<?php endif; // End if $show_sep ?>
<span class="comments-link"><?php comments_popup_link( '<span class="leave-reply">' . __( 'Leave a reply', 'catchbox' ) . '</span>', __( '<b>1</b> Reply', 'catchbox' ), __( '<b>%</b> Replies', 'catchbox' ) ); ?></span>
<?php endif; // End if comments_open() ?>
<?php edit_post_link( __( 'Edit', 'catchbox' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- #entry-meta -->
</article><!-- #post-<?php the_ID(); ?> -->
想要更好的显示效果就需要修改css样式来美化你的WordPress主题模板了。
本文标签:
很赞哦! ()
相关教程
图文教程
docker搭建WordPress的步骤方法教程
步骤一创建mysql的容器步骤二创建wordpress的容器并链接mysql容器的数据库创建mysql的容器docker run -d --name mysql -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PA
CentOS下搭建LNMP+WordPress+http2.0的步骤方法
此文是本人CentOS下搭建WordPress的一些笔记,环境搭建时间:201804;将看过的几篇文章总结下来,形成一条龙长文、不用大家再找来找去。本文大概分为此几部分:一、基础命令更新;
wordpress修改php文件的方法
在修改WordPress时,除了可以通过后台选项和添加样式定制外,还有一个很常用的方法就是通过PHP代码实现某些功能
Wordpress图片加注引的方法,Wordpress图片怎么加注引
WordPress 4.8 新增了几个媒体小工具,其中一个就是“图像”小工具。最近有朋友反馈说,这个图像小工具只能添加标题和图片
相关源码
-
(自适应响应式)装修装潢设计公司网站源码下载本模板为装修设计企业打造,采用PbootCMS内核开发,整体设计突出空间美学与功能性结合。首页采用大图轮播展示工程案例,服务项目模块支持三维效果展示,呈现装修设计企业的专业形象与技术实力。查看源码 -
帝国cms题库问答学习平台模板知识付费网站源码+数据采集为教育机构、培训平台及在线学习场景设计,提供完整的题库管理与问答服务解决方案。支持多种题型展示与答案查询,满足不同层次的学习需求。查看源码 -
(自适应多语言)WordPress开源主题MirageV资讯个人博客源码MirageV资讯类个人博客主题源码/WordPress主题/全开源MirageV 是一款开源的 WordPress 主题,支持自适应、暗黑模式、多语言等功能,查看源码 -
(自适应响应式)宠物经验资讯咨询博客pbootcms网站源码下载除宠物资讯领域外,通过内容替换可快速适配宠物用品商城、宠物医疗咨询平台、宠物训练教程网站、动物保护组织官网、水族爱好者社区等垂直领域。查看源码 -
帝国cms7.5品牌连锁店招商加盟商机网站模版源码本模板为招商加盟、创业投资、品牌连锁等商业领域设计,采用帝国CMS7.5内核构建,整体风格简洁大气,突出商业信任感与专业度,适合各类招商加盟项目展示、品牌连锁店宣传等商业应用场景。查看源码 -
(自适应)变压器电子元器件电器配件pbootcms网站模板源码为电子元器件企业打造的响应式网站模板,基于PbootCMS内核开发,助力企业快速构建专业级线上展示平台。支持页面独立设置标题、关键词和描述,内置SEO友好结构。PHP程序确保运行安全稳定,有助于提升搜索引擎收录效果。查看源码
| 分享笔记 (共有 篇笔记) |

