在文章標題添加圖片

Author: Eric  //  Category: Free Teaching

1. 如果想要讓圖片顯示在首頁的文章標題

  a. 打開 ./wp-content/themes/Your_Theme_Name/index.php
  b. 找到

<?php the_title(); ?> 

  c. 選擇下面任一種 code 加上

2. 如果想要讓圖片顯示在內文的文章標題

  a. 打開 ./wp-content/themes/Your_Theme_Name/single.php
  b. 找到

<?php the_title(); ?> 

  c. 選擇下面任一種 code 加上

3. 上面所說的 code

  a. 每篇文章都要顯示圖片

<img src="圖片網址" title="" alt=""/>

  b. 只要最近 24 Hours 所發表的文章才要顯示圖片

<?php
  $time_Post = $post->post_date;
  $time_Now = date( "Y-m-d H:i:s" );
  $diff = ( strtotime( $time_Now ) - strtotime( $time_Post ) ) / 3600;
  if( $diff < 24 ) {
    echo "<img src='圖片網址' title='24小時內最新發表' alt=''>";
  }
?>

4. 如果不想自己做 new 圖片, 可以用 Google Search 一下。

5. HTML <img> Tag 屬性中 title、alt 的差異
The alt attribute is meant to be used as an alternative text if the image is not available, not as a mouse-over text. To show a mouse-over text on images or image-maps, use the title attribute. From w3schools

<img src="圖片網址" title="滑鼠放在圖片上顯示的文字" alt="圖片掛掉時顯示的文字"/>

6. 囉嗦的我再多寫一點
// 還故意分三小點, 跟老師說我們只考一題, 考卷發下來卻有 20 小題, 有異曲同工之處

  a. 原 :

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

  b. 改 : // 下面有空一格是為了不要讓文字和圖片你儂我儂, 黏在一起

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> 上面的code 放在這</h2>

  c. 您可以 index.php 和 single.php 分別放不同的圖片喔

Tags: ,

2 Responses to “在文章標題添加圖片”

  1. Eric Says:

    eg: 如果是想在文章標題前面加上圖片

    原本
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

    修改
    <h2><img src="圖片網址" title="" alt=""/> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

  2. Eric Says:

    如果您加入圖片會讓標題分成兩行或您用的主題是 WordPress LightWord Theme 可以參考下面更詳細的說明。

    原本
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

    修改
    <h2>Put_the_Code_Here_A <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">Put_the_Code_Here_C <?php the_title(); ?> Put_the_Code_Here_D</a> Put_the_Code_Here_B</h2>

    Put_the_Code_Here_A, Put_the_Code_Here_B : 會讓標題變成2行
    Put_the_Code_Here_C, Put_the_Code_Here_D : 會讓連結包含圖片

    所以, 看來主題作者對文章標題有做特別的設定, 所以只好來去修改主題的 style.css, 因為它被 h2 包養, 所以應該可以很順利的找到下面那行

    h2 a{font-weight:700;border:0;text-decoration:none;color:#2C2C29;display:block;}

    a. 不管標題長短, 放 A, 放 B, 直接變2行
    display:block The element will generate a block box (a line break before and after the element)

    b. 圖片會正常顯是在標題前後, 另外這是預設值, 所以如果沒有特別設定就是 display:inline。
    display:inline The element will generate an inline box (no line break before or after the element). This is default.

    c. 這個就留給有興趣的試
    display:inline-block The element will generate a block box, laid out as an inline box.

    每種方法各有優缺點, 衡量它的代價和它所帶來的效益, 選一種最適合自己的方法吧。

    更多 CSS display 的用法可以參考 CSS display Property ,CSS display Declaration.

Leave a Reply