【お使いの Snow Monkey のバージョン】18.1.0
【お使いの Snow Monkey Blocks のバージョン】17.0.3
【お使いの Snow Monkey Editor のバージョン】9.0.3
【お使いのブラウザ】google chrome
【当該サイトのURL】
### 実現したいこと
カスタム投稿のアーカイブページで表示されるカードの左上に添付画像のように
「販売価格」を表示させたい
### 発生している問題
「Advanced Custom Fields」を利用して設定したカスタムフィールドの値(販売価格)が表示されない。
### 試したこと
表示されている画像の前に販売価格表示用のdivタグを追加しようと思い、過去ログを参考にして以下のコードを追加。
add_filter(
'snow_monkey_template_part_render',
function( $html, $slug, $name, $vars ) {
if ( 'inventory' !== get_post_type() ) {
return $html;
}
if ( 'template-parts/loop/entry-summary/title/title' === $slug ) {
// カスタムフィールドで設定したフィールド名を代入
$acf_price = get_field('h_price');
// 追加するdivタグを設定
$acf_price_info = "<div class='c-entry-summary__figure'><div class='ec-info'><p>" . esc_attr($acf_price) . "</p></div>";
// テンプレートパーツを変換
$html = str_replace(
'<div class="c-entry-summary__figure">',
$acf_price_info,
$html
);
}
return $html;
},
10,
4
);
よろしくお願いいたします。
ユーザーフォーラムは、ご自身が試されたことを共有していただくことが誰かのためになる場所なのでとてもありがたいです! このコードでも問題ありませんが、強いて言うなら snow_monkey_template_part_render
ではなくて snow_monkey_template_part_render_template-parts/loop/entry-summary
を使ったほうがコードがシンプルになるかなと思いました。
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary',
function( $html, $name, $vars ) {
if ( 'post' === $name ) {
if ( 'rich-media' === $vars['_entries_layout'] ) {
$html = str_replace( '<a ', '<a target="_top" ', $html );
}
}
return $html;
},
10,
4
);
トピックを読み漁りつつ投稿させていただいたのですが、近いものを発見し、改変して試してみたらできました。失礼いたしました。
挙動としては現在狙ったものができておりますが、私のような方のために追加したコードを載せますので、もし何かご指摘などございましたらご教示いただけますと幸いです。
特に問題なさそうでしたら閉じさせていただきます。
my-snow-monkeyに追加したコード
add_filter(
'snow_monkey_template_part_render',
function( $html, $slug, $name, $vars ) {
if ( 'template-parts/loop/entry-summary' === $slug && 'post' === $name ) {
if ( 'rich-media' === $vars['_entries_layout'] ) {
$html = str_replace( '<a ', '<a target="_top" ', $html );
}
}
return $html;
},
10,
4
);
snow_monkey_template_part_render_
フックの第2引数($name
)に投稿タイプ名が入ってくるのでそれで判定するのはどうでしょうか? 設定によっては意図した投稿タイプ名が入ってこない場合もありますが、とりあえず確認してみてください。
あと、アーカイブページでは記事タイトル名が h2 ですが、任意のタクソノミーブロックでは h3 になるので preg_replace()
を使う形に書き換えるのが良いと思います。下記サンプルコードです。
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary/title/title',
function ( $html, $name ) {
if ( 'news' === $name ) {
$html = preg_replace(
'|(</h[23]>)|ms',
'$1あいうえお',
$html
);
}
return $html;
},
10,
2
);
②カスタム投稿のアーカイブページの横一列の表示数を3つから4つに変更したい。
できれば任意のタクソノミー投稿の表示数も3つから4つに変更したい。
1トピック1課題でお願いしているので、お手数おかけして申し訳ありませんが↑の課題については別にトピックを立ててください!
【お使いの Snow Monkey のバージョン】17.1.0
【お使いの Snow Monkey Blocks のバージョン】16.0.1
【お使いの Snow Monkey Editor のバージョン】8.0.1
【お使いのブラウザ】firefox
【当該サイトのURL】local
### 実現したいこと
①任意のタクソノミーで表示されているc-entry-summary__title部分にカスタムフィールドを表示させたい。
②カスタム投稿のアーカイブページの横一列の表示数を3つから4つに変更したい。
できれば任意のタクソノミー投稿の表示数も3つから4つに変更したい。
### 発生している問題
①カスタム投稿のアーカイブページではカスタムフィールドの出力が行えたが、任意のタクソノミーで表示されているパネル?には同じようにカスタムフィールドを表示させることができない。
②どこを弄っていいかわからない。
### 試したこと
/*カスタム投稿 */
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary/title/title',
function ($html) {
if (get_post_type('seles_cats')) {
// カスタムフィールドで設定したフィールド名を代入
$acf_year = get_field('sex');
$acf_faculty = get_field('price');
$acf_birthday = get_field('birthday');
// テンプレートのh2タグの後に<div class='employee-info'>を追加
$acf_employee_info = "</h2>
<div class='employee-info'>
<p>" . esc_attr($acf_year) . "</p>" .
"<p>" . esc_attr($acf_faculty) . "</p>" .
"<p>" . esc_attr($acf_birthday) . "</p>" .
"</div>";
// テンプレートパーツのh2タグの後ろにdivタグを追加する
$html = str_replace(
'</h2>',
$acf_employee_info,
$html
);
}
return $html;
}
);
アーカイブの時はis_post_type_archive
を指定することで表示できたので、ここをget_post_type
に変更したが表示されなかった。
こちらも参考にしましたが、表示ができませんでした。
↓アーカイブページ
↓任意のタクソノミー表示
よろしくお願いします。
いつも参考にさせていただいております。
お手数ですが、下記回答いただけると助かります。
【お使いの Snow Monkey のバージョン】16.4.6
【お使いの Snow Monkey Blocks のバージョン】15.0.1
【お使いの Snow Monkey Editor のバージョン】 7.0.1
【お使いのブラウザ】Chrome
【当該サイトのURL】https://kodawari-gotenba.sakura.ne.jp/kodawari-gotenba.jp/category/shop/sweets/
### 実現したいこと
見出しとカスタムフィールド値を強制改行したい
### 発生している問題
タグが表示されてしまう
### 試したこと
下記の回答を参考に、my-snow-monkey.php
に
【見出し】
add_filter( 'snow_monkey_entry_summary_title_num_words', function( $num_words ) {
return false;
} );
【カスタムフィールド】
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary',
//entry-summary-postが対象
function( $html ) {
if ( in_category( array('meat','sweets','other','bread','drink') )): //指定カテゴリに限定する
// 以降の出力を変数に格納する
ob_start();
?>
<div class="title_shopname">
<p><?php echo esc_html (get_field('title_shop_name')); ?></p>
</div>
<?php
// 変数に格納する
$works_client = ob_get_clean();
// </header>を書き換える
return str_replace(
'</header>',
'</header>'.$works_client,
$html
);
endif;
return $html;
}
);
を追記したところ、強制改行ではなく、<br>
タグが表示されてしまう。
オレインさんの記事を読みながら色々とやってみたらできそうです。
確認していただけますか?
ただ、抜粋の長さに関わらずリンクボタンを右寄せにしたいのですが、イマイチできません。
そこだけお聞きしてもいいでしょうか?
my-snow-monkey.php
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary/content/content',
function ( $html ) {
$acf_link = get_field( 'link' );
$acf_reserve_link = "<div class="c-entry-summary__content">
<div class="reserve-link"><a class="reserve-btn" href="$acf_link" target="_blank" rel="noopener">チケット予約</a>";
$html = str_replace(
'<div class="c-entry-summary__content">',
$acf_reserve_link,
$html
);
return $html;
}
);
css
.reserve-link {
display:flex;
flex-direction:row-reverse;
justify-content:space-between;
}
.reserve-btn {
border: 1px solid #000;
padding:10px 50px;
border-radius: 10px;
}
.reserve-link a {
text-decoration:none;
color: #000;
}
【お使いの Snow Monkey のバージョン】15.11.1
【お使いのブラウザ】Chrome
いつもお世話になっております!
### 実現したいこと
カスタム投稿の記事一覧の画像上にMy Snow Monkeyを使ってリンクを追加したいです。
### 発生している問題
My Snow Monkeyで以下のようなコードを追加しました。
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary/figure/figure',
//entry-summary-postが対象
function( $html ) {
if (get_post_type() === 'products'): //カスタム投稿 productsに限定する
// 以降の出力を変数に格納する
ob_start();
?>
<div class="morebuy">
<a href="<?php the_permalink(); ?>" class="more">MORE</a>
<?php if(post_custom('base')): ?>
<a href="<?php the_field('base'); ?>" target="_blank" class="buy">BUY</a>
<?php endif; ?>
</div>
<?php
// 変数に格納
$morebuy = ob_get_clean();
// </div>を書き換える
return str_replace(
'</div>',
$morebuy.'</div>',
$html
);
endif;
return $html;
}
);
無事に表示はされているのですが、
<li class="c-entries__item">
<section class="c-entry-summary c-entry-summary--post c-entry-summary--type-products">
の後に
記事のaタグが入ってきてしまいます。
<li class="c-entries__item">
<a href="https://記事のパーマリンク">
</a><section class="c-entry-summary c-entry-summary--post c-entry-summary--type-products"><a href="https://記事のパーマリンク">
</a>
### 試したこと
My Snow Monkeyでdiv class=”morebuy”内のaタグを抜かして普通のテキストにしたところ
余分なaタグは入ってきませんでした。
aタグを入れると余分なaタグが入ってきてしまいます。
なんでaタグを入れるとこうなってしまうのかが調べてもわからず
フォーラムに投稿させていただきました。
どうぞよろしくお願いします。
ありがとうございます。
いただいたコードですが下記のようにするということでしょうか。これではカスタムフィールドも表示されませんでした。使い方が間違っていますでしょうか。
add_action(
'snow_monkey_get_template_part_template-parts/loop/entry-summary/content/content-{contract}',
function( $html ) {
if (get_post_type() === 'contract'): //カスタム投稿contractに限定する
// 以降の出力を変数に格納する
ob_start();
?>
<div class="contract-data">
<p>表示1:<?php echo esc_html (get_field('type')); ?></p>
<p>表示2:<?php echo esc_html (get_field('station')); ?></p>
<p>表示3:<?php echo esc_html (get_field('time')); ?></p>
</div>
<?php
// 変数に格納する
$type = ob_get_clean();
// </header>を書き換える
return str_replace(
'</header>',
'</header>'.$type,
$html
);
endif;
return $html;
}
);
【お使いの Snow Monkey のバージョン】15.1.0
【お使いのブラウザ】GoolgeChrome
【当該サイトのURL】非公開
カスタム投稿一覧でカスタムフィールドの情報を表示することはできたのですが、抜粋が表示されてしまいます。CSSで非表示にする方法もあるかと思いますが、記述の仕方で表示しない方法がありましたら教えていただきたいです。
また、表示方法はフォーラムを参考に作成してみたのですが、この表示方法で問題ないでしょうか。
ご教授いただけると幸いです。
### 実現したいこと
カスタム投稿一覧でカスタムフィールドの情報を表示し、抜粋部分は表示させない。
### 試したこと
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary',
//entry-summary-postが対象
function( $html ) {
if (get_post_type() === 'contract'): //カスタム投稿contractに限定する
// 以降の出力を変数に格納する
ob_start();
?>
<div class="contract-data">
<p>表示1:<?php echo esc_html (get_field('type')); ?></p>
<p>表示2:<?php echo esc_html (get_field('station')); ?></p>
<p>表示3:<?php echo esc_html (get_field('time')); ?></p>
</div>
<?php
// 変数に格納する
$type = ob_get_clean();
// </header>を書き換える
return str_replace(
'</header>',
'</header>'.$type,
$html
);
endif;
return $html;
}
);
以下を参考にしました。
add_filter(
'snow_monkey_template_part_render',
function( $html, $slug ) {
if ( 'template-parts/loop/entry-summary/meta/meta' === $slug ) {
return preg_replace(
'|
<ul>
<li class="c-meta__item c-meta__item--published">.*?</li>
</ul>
|ms',
'
<ul>
<li class="c-meta__item c-meta__item--modified">' . get_the_modified_time( get_option( 'date_format' ) ) . '</li>
</ul>
',
$html
);
}
return $html;
},
10,
2
);
shiさん
くわしくありがとうございます。
概ね以下のコードをmy-snow-monkey.php
に追加していただければ、ご希望の表示はできると思います。(もっと良いコードの書き方はあると思いますので参考までに(^^;)
使用しているget_field()
の部分は、設定しているフィールド名に変更してください。
また不要なものは消してください。
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary',
function( $html ) {
if ( 'event' === get_post_type() ):
ob_start();
?>
<?php
// カスタムフィールド:開催日 → event_day
// ACF デイトピッカー(返り値:Ymd)
$eDate = get_post_meta( get_the_ID(), 'event_day', true );
$date = date_create( $eDate );
// 曜日用
$week = array("日", "月", "火", "水", "木", "金", "土");
// 開催日の表示は 「月.日」に変更
$eventDay = date_format($date,'n.j');
$today = wp_date('Ymd');
// カスタムフィールド:開始時刻 → event_starttime
// ACF Time Picker(返り値:G:i)
$startTime = get_field( 'event_starttime' );
// カスタムフィールド:開始時刻 → event_endtime
// ACF Time Picker(返り値:G:i)
$endTime = get_field( 'event_endtime' );
// 開催日以降の判定
// 開催日よりも今日の日付が大きい場合
$preriod = strtotime($eDate) < strtotime($today);
// カスタムフィールド:概要欄 → event_txt
// ACF テキストエリア(改行:なにもしない)
$eventSummary = get_field( 'event_txt' )
?>
<div class="event_day_post">
<p class="event_date"><?php echo esc_attr( $eventDay) ; ?><span class="week"><?php echo esc_attr( $week[(int)date_format($date,'w')] ) ?></span></p>
<p class="event_time"><?php echo esc_attr( $startTime ) ; ?> - <?php echo esc_attr( $endTime ) ; ?></p>
</div>
<div class="event_txt">
<p><?php echo nl2br( esc_attr( $eventSummary ) ); ?></p>
</div>
<?php if( $preriod ): ?> // 開催日以降の条件分岐
<div class="event_finish">終了しました</div>
<?php else: ?>
<div class="event_open">これから開催です</div>
<?php endif; ?>
<?php
$event = ob_get_clean();
return str_replace(
'</header>',
'</header>'.$event , // 投稿タイトルの下に追加
$html
);
endif;
return $html;
}
);
結果 ↓
【お使いの Snow Monkey のバージョン】15.7.0
【お使いの Snow Monkey Blocks のバージョン】12.3.1
【お使いの Snow Monkey Editor のバージョン】6.0.1
【お使いのブラウザ】Firefox 92.0
【当該サイトのURL】http://gsr-jp.com/seminer-event/
### 実現したいこと
投稿日に曜日を追加したい。
### 発生している問題
### 試したこと
My Snow Monkeyに以下のコードを追加しましたが、エラーになりました。
add_filter(
'snow_monkey_template_part_render',
function( $html, $slug ) {
if ( 'template-parts/loop/entry-summary/meta/meta' === $slug ) {
return preg_replace(
'<time datetime="<?php the_time( 'c' ); ?>"><!--?php the_time( get_option( 'date_format' ) ); ?--></time>',
'<time datetime="<?php the_time( 'c(D)' ); ?>"><!--?php the_time( get_option( 'date_format' ) ); ?--></time>',
$html
);
}
return $html;
},
10,
2
);
名前と得意分野で、それぞれコードを分けて以下のような感じでどうでしょうか?
/**
* カスタム投稿タイプのアーカイブページにて、投稿一覧(リッチメディア型)のタイトルの前に、'名前:'を表示
*
* @param string $html はコンテンツの中身.
*/
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary/title/title',
function ( $html ) {
if ( 'banso-mate' !== get_post_type() ) {
return $html;
}
$html = str_replace( '<h3>', '<h3>名前:', $html );
return $html;
}
);
/**
* カスタム投稿タイプのアーカイブページにて、投稿一覧(リッチメディア型)に、タグ・ディスクリプションを表示
*
* @param string $html はコンテンツの中身.
*/
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary/content/content',
function ( $html ) {
if ( 'banso-mate' !== get_post_type() ) {
return $html;
}
$postid = get_the_ID();
$taxonomy = 'banso_tags';
$post_terms = get_the_terms( $postid, $taxonomy );
if ( ! empty( $post_terms ) ) {
$tags = '<div>';
$tags .= '<h3>得意分野:</h3>';
$tags .= '<div>';
$tags .= '<ul class="smb-taxonomy-terms__list">';
foreach ( $post_terms as $post_term ) {
$term_name = $post_term->name;
$tags .= '<li class="smb-taxonomy-terms__item">' . $term_name . '</li>';
}
$tags .= '</div>';
$tags .= '</div>';
}
$meta_description = get_post_meta( get_the_ID(), 'wp-seo-meta-description', true );
$html = $tags . '<div class="c-entry-summary__content">' .$meta_description. '</div>';
return $html;
}
);
ありがとうございます。
get_post_type()
に変更で、うまくいきました。
/**
* 各投稿一覧で taxonomy のラベルをタイトルの上に表示
*
* @param string $html はコンテンツの中身.
*/
add_filter(
'snow_monkey_template_part_render_template-parts/loop/entry-summary/title/title',
function ( $html ) {
$taxonomy = 'category';
$post_type = get_post_type();
switch ( $post_type ) {
case 'post':
$taxonomy = 'category';
break;
case 'news':
$taxonomy = 'news_category';
break;
case 'shop-info':
$taxonomy = 'shop_genre';
break;
case 'interview':
$taxonomy = '';
break;
}
$tags = '';
$postid = get_the_ID();
$post_terms = get_the_terms( $postid, $taxonomy );
if ( ! empty( $post_terms ) ) {
$tags = '<ul class="' .$taxonomy. '-tags taxonomy-tags">';
foreach ( $post_terms as $post_term ) {
$term_name = $post_term->name;
$slug = $post_term->slug;
$term_id = $post_term->term_id;
$tags .= '<li class="' .$taxonomy. '-' .$term_id.' ' .$slug.'">' . $term_name . '</li>';
}
$tags .= '</ul>';
}
$html = str_replace( '<h3', $tags . '<h3', $html );
return $html;
}
);