一、自動(dòng)顯示文章第一張圖片
在當(dāng)前使用的主題模板的functions.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(emptyempty($first_img)){ //Defines a default image
- $first_img = "/images/default.jpg";
- }
- return $first_img;
- }
在當(dāng)前主題模板的index.php文件的內(nèi)容代碼前或后添加以下代碼:
<?php echo catch_that_image() ?>
二、文章列表頁自動(dòng)調(diào)用文章縮略圖
在外觀–編輯里頭找到functions.php,加入以下這個(gè)函數(shù):
- function emtx_auto_thumbnail($pID,$thumb='thumbnail') {
- $blogimg = FALSE;
- if (has_post_thumbnail()) {// 判斷該文章是否已經(jīng)設(shè)置了“特色圖像”,如果有則直接顯示該特色圖像的縮略圖
- $blogimg = wp_get_attachment_image_src(get_post_thumbnail_id($pID),$thumb);
- $blogimg = $blogimg[0];
- } elseif ($postimages = get_children("post_parent=$pID&post_type=attachment&post_mime_type=image&numberposts=0")) {//如果文章沒有設(shè)置特色圖像,則查找文章內(nèi)是否有上傳圖片
- foreach($postimages as $postimage) {
- $blogimg = wp_get_attachment_image_src($postimage->ID, $thumb);
- $blogimg = $blogimg[0];
- }
- } elseif (preg_match('/<img [^>]*src=["|']([^"|']+)/i', get_the_content(), $match) != FALSE) {
- $blogimg = $match[1];
- }
- if($blogimg) {
- $blogimg = '<a href="'. get_permalink().'"><img src="'.$blogimg.'" alt="'.get_the_title().'" class="alignleft wp-post-image" /></a>';
- }
- return $blogimg;
- }
然后在相應(yīng)的模板文件里面調(diào)用縮略圖的地方做個(gè)修改,把原來調(diào)用the_post_thumbnail的地方按照實(shí)際需求改為諸如下面這樣的代碼即可:
- <?php if(emtx_auto_thumbnail($post->ID) ) {
- echo emtx_auto_thumbnail($post->ID);
- } ?>
三、后臺(tái)所有文章列表顯示縮略圖
打開你主題的functions.php文件添加如下代碼:
- if ( !function_exists('fb_AddThumbColumn') && function_exists('add_theme_support') ) {
- // 在文章列表頁與頁面列表頁添加縮略圖列表
- add_theme_support('post-thumbnails', array( 'post', 'page' ) );
- function fb_AddThumbColumn($cols) {
- $cols['thumbnail'] = __('Thumbnail');
- return $cols;
- }
- function fb_AddThumbValue($column_name, $post_id) {
- $width = (int) 35;
- $height = (int) 35;
- if ( 'thumbnail' == $column_name ) {
- // thumbnail of WP 2.9
- $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
- // image from gallery
- $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') ); www.111Cn.net
- if ($thumbnail_id)
- $thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
- elseif ($attachments) {
- foreach ( $attachments as $attachment_id => $attachment ) {
- $thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
- }
- }
- if ( isset($thumb) && $thumb ) {
- echo $thumb;
- } else {
- echo __('None');
- }
- }
- }
- // 文章頁調(diào)用
- add_filter( 'manage_posts_columns', 'fb_AddThumbColumn' );
- add_action( 'manage_posts_custom_column', 'fb_AddThumbValue', 10, 2 );
- // 頁面調(diào)用
- add_filter( 'manage_pages_columns', 'fb_AddThumbColumn' );
- add_action( 'manage_pages_custom_column', 'fb_AddThumbValue', 10, 2 );
- }
新聞熱點(diǎn)
疑難解答
圖片精選