• ayakokuroda0814

    (@ayakokuroda0814)


    自作テーマで、行き詰まっております。
    ご指導お願いします。

    <article class=”post”>
    <header>
    <h1 class=”post-title”><?php the_title(); ?></h1>
    </header>
    <!– ここからアイキャッチ –>
    <figure class=”thumbnail”>
    <?php
    if(is_home()||is_category() ||is_tag () ||is_date() ||is_archive() ||is_search()) {
    if(has_post_thumbnail()) {
    the_post_thumbnail();
    } else {
    echo ‘<img src=”‘.get_template_directory_uri().’/images/no_image.png” width=”160″ height=”120″/>’;
    }
    } ?>
    </figure>
    <!– アイキャッチここまで –>
    <div class=”post-content”>
    <?php
    if(is_home() ||is_category() ||is_tag () ||is_date() ||is_archive() ||is_search()) {
    the_excerpt(); //抜粋を表示する
    } else {
    the_content(); //全文を表示する
    }
    ?>
    </div>
    </article>

    上記のようなコーディングで、抜粋を表示するところまでは満足しているのですが、
    「抜粋記事の画面のみ」、タイトルとアイキャッチにリンクをつけたいのですが、どのような方法があるでしょうか?

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • こんにちは、

    値を変数に格納するという方法を覚えるといいです。

    提示されたコードが、シングル投稿、固定ページ以外の場合 概要を表示するという趣旨だとすると、以下のように記述する事が出来ると思います。

    <article class="post">
    	<header><h1 class="post-title"><?php the_title(); ?></h1></header>
    
    	<figure class="thumbnail">
    	<?php
    		if(has_post_thumbnail()) {
    			// @see get_the_post_thumbnail , get_the_id
    			$thumbnail_image = get_the_post_thumbnail( get_the_ID(), array( 160, 120 ) );
    		} else {
    			$thumbnail_image = '<img src="'.get_template_directory_uri().'/images/no_image.png" width="160" height="120"/>';
    		}
    	?>
    	</figure>
    
    	<div class="post-content">
    	<?php
    		if( ! is_singular() ) {
    			echo $thumbnail_image;
    			the_excerpt();
    		} else {
    			the_content();
    		}
    	?>
    	</div>
    </article>

    get_the_post_thumbnail

    https://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/get_the_post_thumbnail

    get_the_ID

    http://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/get_the_ID

    上記の書き換えた関数は、値を echo せず return します。

    returnする関数を利用する場合は、フィルターが通っていない事がよくありますので、その点を注意しながら、場合によっては、コード内にフィルタを追加する等して記述する必要が出ることがあります。

    フィルターについては、検索してみてください。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「抜粋記事を表示するthe_excerpt(); とリンクについて」には新たに返信することはできません。