• nspjp

    (@nspjp)


    Smart Custom Fieldsだけで作成したページの任意のテキストエリアをmeta descriptionとして出力したい。

    リンクしたページは、ソースを見るとtitleのみ出力され、descriptionが出力されてません。
    「試してみよう歩行力」または、下の「本文」をdescriptionとして出力したいのです。

    こんなことできますでしょうか?
    質問が稚拙ですみません。

    • このトピックはnspjpが5年前に変更しました。

    ヘルプの必要なページ: [リンクを見るにはログイン]

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • ishitaka

    (@ishitaka)

    こんな感じではどうでしょうか?

    functions.php に、

    function add_description_meta() {
    	global $post;
    	if ( isset( $post ) ) {
    		if ( is_single() ) {
    			$description = get_post_meta( $post->ID, 'カスタムフィールド名', true );
    			$description = mb_strimwidth( preg_replace( '/[\n\r]/', '', strip_tags( $description ) ), 0, 120, '...', 'UTF-8' );
    			echo '<meta name="description" content="' . esc_attr( $description ) . '" />' . "\n";
    		}
    	}
    }
    add_action( 'wp_head', 'add_description_meta', 1 );
    ishitaka

    (@ishitaka)

    All in One SEO Pack プラグインを使用している場合は、aioseop_description フィルターフックを使用するのもいいかもしれません。

    例:

    function my_aioseop_description( $description ){
    	global $post;
    	if ( isset( $post ) ) {
    		if ( is_single() ) {
    			$description = get_post_meta( $post->ID, 'カスタムフィールド名', true );
    			$description = mb_strimwidth( preg_replace( '/[\n\r]/', '', strip_tags( $description ) ), 0, 120, '...', 'UTF-8' );
    		}
    	}
    	return $description;
    }
    add_filter( 'aioseop_description', 'my_aioseop_description' );
    トピック投稿者 nspjp

    (@nspjp)

    ありがとうございます!
    やってみます。

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • トピック「Smart Custom Fieldsのテキストエリアをdescriptionとして出力」には新たに返信することはできません。