メインコンテンツへ移動

Search results of "entry-summary $html replace"

15件の結果を表示中 - 1 - 15件目 (全55件中)
  • 投稿者
    検索結果
  • アバター画像キタジマ タカシ
    参加者
    2593

    has_category( 'demae', $post ) となっていますが $post が存在しないためエラーがでちゃうのではないかなと思います。global $post を入れて、グローバル変数の $post を参照するとどうでしょうか?

    add_filter(
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary/',
    	function( $html ) {
    		global $post;
    
    		if ( has_category( 'demae', $post ) ) {
    			$html = str_replace(
    				'<a',
    				'<a target="_blank" rel="noreferrer noopener"',
    				$html
    			);
    		}
    		return $html;
    	}
    );
    1
    Who liked:
    林淳一
    参加者
    29

    キタジマ様

    ご確認いただきありがとうございます。

    やりたいことは、 「任意のタクソノミーの投稿」ブロックでカテゴリーが「demae」の場合、 新規ウィンドウで開きたいです。

    下記コードをmy-snow-monkey.phpに追加しましたが、 新規ウィンドウでは開きませんでした。

    add_filter(
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary/',
    	function( $html ) {
    		if ( has_category( 'demae', $post ) ) {
    			$html = str_replace(
    				'<a',
    				'<a target="_blank" rel="noreferrer noopener"',
    				$html
    			);
    		}
    		return $html;
    	}
    );
    0
    Who liked: No user
    #144140
    アバター画像キタジマ タカシ
    参加者
    2593

    リプライするときに整形されるから?なのかコードが崩れているので整形してみました。

    あと、コードの途中にちゃんとタグの情報が取得できているか確認するために var_dump(...); を追加してみました。ちゃんと取得できているならタグの情報が画面上に表示されるはずです。表示されないなら紐づけができていないか、名前が間違っている可能性がありそうです。

    add_filter(
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary/title/title',
    	function ($html) {
    		// カスタムフィールドで設定したフィールド名を代入
    		$acf_name_area = get_field('name_area');
    		$acf_region_area = get_field('region_area');
    		$acf_specialty_genre = get_field('specialty_genre');
    
    		// テンプレートのh3タグの後に <div class="property-info">を追加
    		$acf_property_info = '</h3><div class="property-info">' .
    			'<div class="property-body">' .
    			'<div class="property-content">' .
    			'<dl class="name_area"><span>【代表者名】</span> <dt>'  . esc_html($acf_name_area) . '<dt></dl>' .
    			'<dl class="region_area"><span>【地域】</span> <dt>'  . esc_html($acf_region_area) . '<dt></dl>' .
    			'<dl class="specialty_genre"><span>【取扱業務】</span> <dt>'  . esc_html($acf_specialty_genre) . '<dt></dl>' .
    			'</div>' .
    			'</div>' .
    			'</div>';
    
    		// テンプレートパーツのh3タグの後ろにdivタグを追加する
    		$html = str_replace(
    			'</h3>',
    			$acf_property_info,
    			$html
    		);
    
    		return $html;
    
    		// テンプレートパーツのh3タグの後ろにdivタグを追加する
    		$html = str_replace(
    			'</h3>',
    			$acf_property_info,
    			$html
    		);
    
    		// 記事に紐づいている post_tag の各タームのアーカイブページへのリンクを表示する
    		$post_tag_terms = get_the_terms(get_the_ID(), 'administrative_scrivener_tag');
    		var_dump( $post_tag_terms ); // テスト用
    		ob_start();
    		if (is_array($post_tag_terms)) {
    			foreach ($post_tag_terms as $post_tag_term) {
    				?>
    				<a href="<?php echo esc_url( get_term_link( $post_tag_term ) ); ?>"><?php echo esc_html( $post_tag_term->name ); ?></a>
    				<?php
    			}
    		}
    		$administrative_scrivener_tags = ob_get_clean();
    		var_dump( esc_html( $administrative_scrivener_tags ) ); // テスト用
    
    		$html = $html . $administrative_scrivener_tags;
    		return $html;
    	}
    );
    1
    Who liked:
    #144139
    YM
    参加者
    8

    キタジマ様

    早速のご教授ありがとうございます。

    申し訳ありません、return $html;追加と
    タクソノミーnameが「administrative_scrivener_tag」の為
    下記の様にpost_tagの箇所を「administrative_scrivener_tag」に変更いたしましたが表示されない様です。

    
    add_filter(
    'snow_monkey_template_part_render_template-parts/loop/entry-summary/title/title',
    function ($html) {
    // カスタムフィールドで設定したフィールド名を代入
    $acf_name_area = get_field('name_area');
    $acf_region_area = get_field('region_area');
    $acf_specialty_genre = get_field('specialty_genre');
    
    // テンプレートのh3タグの後に
    <div class="property-info">を追加
    $acf_property_info = '
    <div class="property-info">' .
    '
    <div class="property-body">' .
    '
    <div class="property-content">' .
    '
    <dl class="name_area">【代表者名】
     	<dt>' . esc_html($acf_name_area) . '</dt>
     	<dt></dt>
    </dl>
    ' .
    '
    <dl class="region_area">【地域】
     	<dt>' . esc_html($acf_region_area) . '</dt>
     	<dt></dt>
    </dl>
    ' .
    '
    <dl class="specialty_genre">【取扱業務】
     	<dt>' . esc_html($acf_specialty_genre) . '</dt>
     	<dt></dt>
    </dl>
    ' .
    '
    
    </div>
    ' .
    '
    
    </div>
    ' .
    '
    
    </div>
    ';
    
    // テンプレートパーツのh3タグの後ろにdivタグを追加する
    $html = str_replace(
    '
    
    ',
    $acf_property_info,
    $html
    );
    
    return $html;
    
    // テンプレートパーツのh3タグの後ろにdivタグを追加する
    $html = str_replace(
    '
    
    ',
    $acf_property_info,
    $html
    );
    
    return $html;
    
    // 記事に紐づいている post_tag の各タームのアーカイブページへのリンクを表示する
    ob_start();
    $post_tag_terms = get_the_terms(get_the_ID(), 'administrative_scrivener_tag');
    if (is_array($post_tag_terms)) {
    foreach ($post_tag_terms as $post_tag_term) {
    ?>
    <!--?php echo esc_html($post_tag_term->name); ?-->
    <!--?php <br ?--> }
    }
    $html = $html . ob_get_clean();
    return $html;
    }
    );
    

    ご確認いただけますと幸いです。

    お手数おかけいたしますが何卒よろしくお願い申し上げます。

    0
    Who liked: No user
    #144132
    YM
    参加者
    8

    キタジマ様

    早速のご教授ありがとうございます。

    下記コードを追加いたしましたが表示されませんでした。

    ちなみにですが、CPT UIでタクソノミーを追加して表示させている記事に紐付けしております。

    記事自体の表示は、「最近の投稿」で投稿タイプを選んでおります。

    
    
    add_filter(
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary/title/title',
    	function ($html) {
    		// カスタムフィールドで設定したフィールド名を代入
    		$acf_name_area = get_field('name_area');
    		$acf_region_area = get_field('region_area');
    		$acf_specialty_genre = get_field('specialty_genre');
    
    		// テンプレートのh3タグの後に <div class="property-info">を追加
    		$acf_property_info = '</h3><div class="property-info">' .
    			'<div class="property-body">' .
    			'<div class="property-content">' .
    			'<dl class="name_area"><span>【代表者名】</span> <dt>'  . esc_html($acf_name_area) . '<dt></dl>' .
    			'<dl class="region_area"><span>【地域】</span> <dt>'  . esc_html($acf_region_area) . '<dt></dl>' .
    			'<dl class="specialty_genre"><span>【取扱業務】</span> <dt>'  . esc_html($acf_specialty_genre) . '<dt></dl>' .
    			'</div>' .
    			'</div>' .
    			'</div>';
    
    		// テンプレートパーツのh3タグの後ろにdivタグを追加する
    		$html = str_replace(
    			'</h3>',
    			$acf_property_info,
    			$html
    		);
    
    		return $html;
    
    		// テンプレートパーツのh3タグの後ろにdivタグを追加する
    		$html = str_replace(
    			'</h3>',
    			$acf_property_info,
    			$html
    		);
    
    		// 記事に紐づいている post_tag の各タームのアーカイブページへのリンクを表示する
    		ob_start();
    		$post_tag_terms = get_the_terms(get_the_ID(), 'post_tag');
    		if (is_array($post_tag_terms)) {
    			foreach ($post_tag_terms as $post_tag_term) {
    	?>
    			<a href="<?php echo esc_url( get_term_link( $post_tag_term ) ); ?>"><?php echo esc_html( $post_tag_term->name ); ?></a>
    <?php
    			}
    		}
    		$html = $html . ob_get_clean();
    	}
    );

    `

    お手数おかけいたしますが、ご確認の程よろしくお願い申し上げます。

    0
    Who liked: No user
    #143726
    まーちゅう
    参加者
    388

    snow_monkey_template_part_render_<slug>
    snow_monkey_pre_template_part_render_<slug>
    のどちらかのフィルターフックを使用されてはどうでしょうか?

    /**
     * @param $html テンプレートパーツの出力HTML
     * @param $name テンプレートパーツの名前
     * @param $vars テンプレートパーツのリクエスト配列
     */
    add_filter(
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary/title/title',
    	function( $html ) {
    		$html = str_replace(
    			'書き換え前の文字列',
    			'書き換え後の文字列',
    			$html
    		);
    		return $html;
    	},
    	10
    );
    /**
     * @param $html テンプレートパーツの出力HTML
     * @param $name テンプレートパーツの名前
     * @param $vars テンプレートパーツのリクエスト配列
     */
    add_filter(
    	'snow_monkey_pre_template_part_render_template-parts/loop/entry-summary/title/title',
    	function( $html ) {
    		return 'New HTML';
    	},
    	10
    );
    2
    Who liked:
    アバター画像キタジマ タカシ
    参加者
    2593

    気になった点がいくつかあるので羅列します。

    – 「PDFのURLを取得できず」というのは、get_field('pdf') で値が取れていないということですか?
    str_replace( 'src="' . get_the_post_thumbnail_url($filefiled) . '"', 'src="' . '"', $html ) だと、サムネイルの URL を空にしようとしているように見えます。(ただし、「ファイルのフィールド名を元にサムネイル URL を取得」しようとしているのでおそらくサムネイルの URL は取得できず、置換には失敗していると思います)
    template-parts/loop/entry-summary/figure/figure を対象としてされていますが、ここにはリンクの HTML は無いと思うので、template-parts/loop/entry-summary を対象にして a 要素の href の値を書き換えるのが正しいのではと思うのですがどうでしょうか?

    1
    Who liked:
    GONSY
    参加者
    846

    「新しく投稿されたものに関してはカテゴリラベルのところにNewマークがつくようにする」は以下のコードを加えました。

    すでに「NEW」が表示されているということですね。
    私がやるとすれば、サムネイル上の「NEW」以外のカテゴリー名はCSS( display: none )で消します。
    ご提示のコードの場合、該当の要素に my-new がついていると思いますので、そこだけ強制的に表示させます。

    .c-entries .c-entry-summary__term {
    	display: none;
    }
    
    .c-entries span[class*="my-new"] {
        display: block!important;
    }

     
     
    カテゴリーの表示は、位置をCSSで移動させるのがちょっと嫌なので、フィルターフック snow_monkey_template_part_render_template-parts/loop/entry-summary</header> の後に replace を使ってリンクなしのカテゴリーを表示するようにしてあげれば、それっぽい感じになると思います。

    add_filter(
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary',
    	function( $html ) {
    		$category = get_the_category();
    		$cat_name = $category[0]->cat_name;
    		$html = str_replace( 
    			'</header>',
    			'</header><p class="cat_name_origin"><span>'. $cat_name. '</span></p>',
    			$html
    			);
    			return $html;
    	}
    );

    もっとスマートな方法もあるかと思いますが、1つの例として参考にしていただければと思います。

    1
    Who liked:
    アバター画像キタジマ タカシ
    参加者
    2593

    どこまでがうまくいっていて、どこからがうまくいっていないのかがわからないので、適当なところで var_dump() して問題を把握すると良いかもしれませんね。

    add_filter(
    	// template-parts/loop/entry-summary を書き換える
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary',
    	function( $html, $name, $vars ) {
    		// 「シンプル」のときだけ実行
    		var_dump( $vars['_entries_layout'] );
    		if ( 'simple' === $vars['_entries_layout'] ) {
    			// カスタムフィールドを取得して、それをもとに加工した HTML をバッファリングする
    			// ACF や実際に保存されているデータの構造がわからないので実際の状況にあわせて書き直してください。
    			$course_types = get_field( 'course_type' );
    			var_dump( $course_types );
    			ob_start();
    			foreach ( $course_types as $course_type ) {
    				echo '<span>' . $course_type . '</span>';
    			}
    			$course_type_html = ob_get_clean();
    
    			var_dump( esc_html( $html ) );
    			return str_replace(
    				'</section>',
    				$course_type_html . '</section>',
    				$html
    			);
    		}
    		return $html;
    	},
    	10,
    	3
    );
    1
    Who liked:
    akosan
    参加者
    33

    ありがとうございます。その後いろいろ調べながらやってみたのですが実現できず、とん挫しています。

    エラーメッセージが出たので

    }
    return $html;

    を追加してエラーは出なくなったのですが表示はできずでした。

    Advanced Custom Fieldsの関数get_fieldを使えばいいのでは?と思い変更したのですがそれではダメでした。※get_post_metaでも出来るようでしたが

    https://lucy.ne.jp/bazubu/advanced-custom-fields-36452.html

     

    add_filter(
    	// template-parts/loop/entry-summary を書き換える
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary',
    	function( $html, $name, $vars ) {
    		// 「シンプル」のときだけ実行
    		if ( 'simple' === $vars['_entries_layout'] ) {
    			// カスタムフィールドを取得して、それをもとに加工した HTML をバッファリングする
    			// ACF や実際に保存されているデータの構造がわからないので実際の状況にあわせて書き直してください。
    			$course_types = get_field( 'course_type' );
    			ob_start();
    			foreach ( $course_types as $course_type ) {
    				echo '<span>' . $course_type . '</span>';
    			}
    			$course_type_html = ob_get_clean();
    
    			return str_replace(
    				'</section>',
    				$course_type_html . '</section>',
    				$html
    			);
    		}
    		return $html;
    	},
    	10,
    	3
    );
    0
    Who liked: No user
    アバター画像キタジマ タカシ
    参加者
    2593

    \Framework\Helper::get_template_part( 'template-parts/loop/entry-summary/term/term' ... ); はターム(カテゴリー等)の一覧を取得するものなので、「ここをカスタムフィールドを取得して HTML を生成・加工する」というコードに書き換えればよいのかなと思います。

    僕は ACF に詳しくないのと、実際に course_type にどのような値が入っているかがわからないのであくまでサンプルですが、だいたい下記のような感じになるのかなと思います。

    add_filter(
    	// template-parts/loop/entry-summary を書き換える
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary',
    	function( $html, $name, $vars ) {
    		// 「シンプル」のときだけ実行
    		if ( 'simple' === $vars['_entries_layout'] ) {
    			// カスタムフィールドを取得して、それをもとに加工した HTML をバッファリングする
    			// ACF や実際に保存されているデータの構造がわからないので実際の状況にあわせて書き直してください。
    			$course_types = get_post_meta( get_the_ID(), 'course_type' );
    			ob_start();
    			foreach ( $course_types as $course_type ) {
    				echo '<span>' . $course_type . '</span>';
    			}
    			$course_type_html = ob_get_clean();
    
    			return str_replace(
    				'</section>',
    				$course_type_html . '</section>',
    				$html
    			);
    	},
    	10,
    	3
    );
    1
    Who liked:
    sususu
    閲覧者
    10

    お世話になっております。

    お忙しい中、検証いただき誠にありがとうございます。

    
    
    add_filter(
    'snow_monkey_template_part_render_template-parts/loop/entry-summary/title/title',
    function ( $html, $name ) {
    if ( is_page( 47 ) ) {
    
    // カスタムフィールドで設定したフィールド名を代入
    $acf_image = get_field('property_image');
    $acf_type = get_field('property_type');
    $acf_location = get_field('property_location');
    $acf_price = get_field('property_price');
    $acf_land_area = get_field('property_land_area');
    
    // テンプレートのh3タグの後に<div class='property-info'>を追加
    $acf_property_info = '</h3>
    <div class="property-info">' .
    '<div class="property-img"><img src="' . esc_url($acf_image) . '" alt="Image"></div>' .
    '<div class="property-body">' .
    '<div class="property-tag">' .
    '<p>' . esc_html($acf_type) . '</p>' .
    '</div>' .
    '<div class="property-content">'.
    '<p class="location">' . esc_html($acf_location) . '</p>' .
    '<p class="price">' . esc_html($acf_price) . '</p>' .
    '<p class="land_area">' . esc_html($acf_land_area) . '</p>' .
    '</div>' .
    '</div>' .
    '</div>';
    
    // テンプレートパーツのh3タグの後ろにdivタグを追加する
    $html = str_replace(
    '</h3>',
    $acf_property_info,
    $html
    );
    }
    return $html;
    },
    10,
    2
    );
    
    

    このように記述しました。

    アイキャッチとタイトルが表示されてしまいますが、こちらはdisplay: none;で消してしまえばいけそうです。

     

    同じページに投稿ページの最新の投稿もブロックで使っていたので

     

    こちらもimageが出てきてしまっていますが、ページ指定で非表示にすればいけそうです。

     

    また試してみて報告させていただきます。

    アドバイスいただき、ありがとうございます。

    0
    Who liked: No user
    sususu
    閲覧者
    10

    キタジマさん、早速のご返信ありがとうございます。

    カスタム投稿アーカイブの一覧部分と全く同じデザインの一覧を、特定の固定ページにも表示させたいということでしょうか?

    はい、そのように表示したいです。

    //////////////////////////////////////
    // カスタム投稿ページにカスタムフィールドを表示
    //////////////////////////////////////
    add_filter(
    	'snow_monkey_template_part_render_template-parts/content/entry/content/content',
    	function ($html) {
    		if (get_post_type() === 'propertys') {
    			// カスタムフィールドで設定したフィールド名を代入
    			$acf_image = get_field('property_image');
    			$acf_type = get_field('property_type');
    			$acf_location = get_field('property_location');
    			$acf_price = get_field('property_price');
    			$acf_land_area = get_field('property_land_area');
    			$acf_property_info =
    '<div class="property-info">' .
    '<div class="property-img"><img src="' . esc_url($acf_image) . '" alt="Image"></div>' .
    '<div class="property-body">' .
    '<div class="property-tag">' .
    '<p>' . esc_html($acf_type) . '</p>' .
    '</div>' .
    '<div class="property-content">'.
    '<p class="location">' . esc_html($acf_location) . '</p>' .
    '<p class="price">' . esc_html($acf_price) . '</p>' .
    '<p class="land_area">' . esc_html($acf_land_area) . '</p>' .
    '</div>' .
    '</div>' .
    '</div>';
    
    			$html = $acf_property_info;
    		}
    
    		return $html;
    	}
    );
    
    ////////////////////////////////////////////
    // カスタム投稿のアーカイブページにカスタムフィールドを表示
    ////////////////////////////////////////////
    add_filter(
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary/title/title',
    	function ( $html ) {
    		if( is_post_type_archive('propertys') ){
    			// カスタムフィールドで設定したフィールド名を代入
    			$acf_image = get_field('property_image');
    			$acf_type = get_field('property_type');
    			$acf_location = get_field('property_location');
    			$acf_price = get_field('property_price');
    			$acf_land_area = get_field('property_land_area');
    
    			// テンプレートのh2タグの後に <div class="property-info">を追加
    			$acf_property_info = '</h2><div class="property-info">' .
    '<div class="property-img"><img src="' . esc_url($acf_image) . '" alt="Image"></div>' .
    '<div class="property-body">' .
    '<div class="property-tag">' .
    '<p>' . esc_html($acf_type) . '</p>' .
    '</div>' .
    '<div class="property-content">'.
    '<p class="location">' . esc_html($acf_location) . '</p>' .
    '<p class="price">' . esc_html($acf_price) . '</p>' .
    '<p class="land_area">' . esc_html($acf_land_area) . '</p>' .
    '</div>' .
    '</div>' .
    '</div>';
    
    			// テンプレートパーツのh2タグの後ろにdivタグを追加する
    			$html = str_replace(
    				'</h2>',
    				$acf_property_info,
    				$html
    			);
    		}
    		return $html;
    	}
    );

    アーカイブページの見た目がこちらで(この画像の上にセクションのタイトルがあります)

    ・実際に他の固定ページで「最近の投稿」ブロックを使用した見た目が

    ↓↓↓

    アイキャッチ画像を仮に抜くと上記添付画像の左上のように何も画像が表示されません。
    画像に関してはアイキャッチ画像とカスタムフィールドの画像を合わせれば良いのですがテキストなどをこちらにも紐付けさせることは可能でしょうか。

    お忙しい中大変恐れ入りますが、よろしくお願いいたします。

    0
    Who liked: No user
    アバター画像キタジマ タカシ
    参加者
    2593

    確かに position 調整する方式だと逆にややこしくなりそうですね…。一応フックを使ったやり方も考えてみたので共有します。

    add_filter(
    	// template-parts/loop/entry-summary を書き換える
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary',
    	function( $html, $name, $vars ) {
    		// 「シンプル」のときだけ実行
    		if ( 'simple' === $vars['_entries_layout'] ) {
    			// カテゴリーラベルを取得して、各項目の最後に追加する。
    			ob_start();
    			\Framework\Helper::get_template_part(
    				'template-parts/loop/entry-summary/term/term',
    				$name,
    				array(
    					'_context' => $vars['_context'],
    					'_terms'   => $vars['_terms'],
    				)
    			);
    			$terms = ob_get_clean();
    
    			return str_replace(
    				'</section>',
    				$terms . '</section>',
    				$html
    			);
    		}
    		return $html;
    	},
    	10,
    	3
    );

    これで画像に重なって表示されるやつ(デフォルト)と、項目の最後に追加されたやつの2箇所にタームが表示されるようになります。この状態で画像上のタームは CSS で消して、最後に追加されたタームは CSS で位置調整すると position だけでやる場合よりも調整しやすいかなと思います。

    1
    Who liked:
    #119815
    アバター画像キタジマ タカシ
    参加者
    2593

    あ、ここデフォルトだと ID 出力されてないですね…。デフォルトで入るようにしたほうが良い気がしてきました。
    とりあえず現状は無いので、追加するコードを書いてみました。

    add_filter(
    	'snow_monkey_template_part_render_template-parts/loop/entry-summary',
    	function( $html ) {
    		$html = str_replace( 'class="c-entry-summary ', 'class="c-entry-summary post__' . esc_html( get_the_ID() ) . ' ', $html );
    		return $html;
    	}
    );

    li.c-entries__item のところには追加できなかったので、.c-entry-summary に追加してみました。

    0
    Who liked: No user
15件の結果を表示中 - 1 - 15件目 (全55件中)

ドキュメント

Snow Monkey の設定方法やマニュアルを掲載しています。

ドキュメント

フォーラム

Snow Monkey の使い方やカスタマイズについてのご質問・ご要望等はサポートフォーラムで行っています。サポートフォーラムは誰でも閲覧できますが、書き込みできるのは Snow Monkey 購入者のみとなります。

サポートフォーラム

よくあるご質問

Snow Monkey のサービスについて不明な点がある場合は、まずはよくあるご質問をご確認ください。

よくあるご質問

お問い合わせ

よくあるご質問を見ても解決しなかった場合、試用版の申請については問い合わせフォームからお願いいたします。

お問い合わせ

Snow Monkey は Gutenberg ブロックエディターに対応した 100%GPL の WordPress テーマです。拡張性を意識した開発をおこなっており、カスタマイザーとブロックでスピーディーにサイトを立ち上げるだけでなく、CSS やフックを駆使した高度なカスタマイズにも柔軟に対応できます。