Wordpress调用文章第一张图片

之前设计WordPress主题的时候调用图片一般都是用文章内附件图片,但是有些博主为了节约博客主机空间,大部分采用外联图片,这样就无法同过这种方式调用了,所以只能用下面的这种方式来调用文章的第一张图片,WordPress调用文章第一张图片代码如下:

1 在WordPress主题的功能函数function.php文件内添加以下代码,这些 代码主要是查找文章内有没有图片并调用第一张图片地址,其工作原理是查找文章内有没这个标签,如果有就调出第一张图片,如果没有就用张设计好的图片代替,这个方式用来作为文章缩略图非常有用,具体代码如下,拷贝到 function.php 之间即可.

----------------------------------------------------------------------------------------------------------------

function catch_that_image() {

      global $post, $posts;

      $first_img = '';

      ob_start();

      ob_end_clean();

      $output = preg_match_all('//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()函数,方法很简单,在需要的地方插入 <?php printf( esc_attr__( 'Permalink to %s', 'catchbox' ), the_title_attribute( 'echo=0' ) ); ?> www.banysky.net 帮你资源网即可,例如我是在首页插入,我修改了Content.php

全文如下:

php 
/** 
* The default template for displaying content 
* 
* @package Catch Themes 
* @subpackage Catch_Box 
* @since Catch Box 1.0 
*/ 
?>

    
> <header class="entry-header"> if ( is_sticky() ) : ?>

class="entry-title">

class="entry-format">

else : ?>

class="entry-title">

endif; ?> if ( 'post' == get_post_type() ) : ?>
class="entry-meta"> if ( comments_open() && ! post_password_required() ) : ?> class="sep"> — class="comments-link"> endif; ?>
endif; ?> header> php $options = catchbox_get_theme_options(); $current_content_layout = $options['content_layout']; ?> if ( is_search() ) : // Only display Excerpts for Search ?>
class="entry-summary">
elseif ($current_content_layout=='excerpt' ) : // Only display Featured Image and Excerpts if checked in Theme Option ?>
class="entry-summary"> if( has_post_thumbnail() ):?> endif; ?> <?php printf( esc_attr__( 'Permalink to %s', 'catchbox' ), the_title_attribute( 'echo=0' ) ); ?> www.banysky.net 帮你资源网
else : ?>
class="entry-content"> →', 'catchbox' ) ); ?> array( 'before' => '', 'link_before' => '', 'link_after' => '', ) ); ?>
endif; ?>
class="entry-meta"> $show_sep = false; ?> 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 ): ?> class="cat-links"> printf( __( 'Posted in %2$s', 'catchbox' ), 'entry-utility-prep entry-utility-prep-cat-links', $categories_list ); $show_sep = true; ?> 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 ) : ?> class="sep"> | endif; // End if $show_sep ?> class="tag-links"> printf( __( 'Tagged %2$s', 'catchbox' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); $show_sep = true; ?> endif; // End if $tags_list ?> endif; // End if 'post' == get_post_type() ?> if ( comments_open() ) : ?> if ( $show_sep ) : ?> class="sep"> | endif; // End if $show_sep ?> class="comments-link">' . __( 'Leave a reply', 'catchbox' ) . '', __( '1 Reply', 'catchbox' ), __( '% Replies', 'catchbox' ) ); ?> endif; // End if comments_open() ?> ', '' ); ?>

 

想要更好的显示效果就需要修改css样式来美化你的WordPress主题模板了。

 

声明:调用文章第一张图片如果是外联图片就无法支持timthumb图片剪切功能,如果你是WordPress主题设计高手,建议用phpthumb,phpthumb是可以支持外联图片的,大家不放试试,good luck!

转载于:https://www.cnblogs.com/Bany/archive/2012/11/23/2783865.html

你可能感兴趣的:(Wordpress调用文章第一张图片)