// --- ACF Flexible Content + Elementor Loop Grid連携コード START (最終完全版) ---
/**
* ACFのオプションページを有効にする
*/
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'サイト共通設定',
'menu_title' => 'サイト設定',
'menu_slug' => 'theme-general-settings',
));
}
/**
* ページの表示前に、ACFで使われるElementorテンプレートのCSSを強制的に読み込む関数
*/
function force_print_acf_loop_styles() {
if (!is_singular('post') || !function_exists('get_field')) { return; }
$layouts = get_field('blog_contents', get_the_ID());
if (empty($layouts) || !is_array($layouts)) { return; }
// ▼▼▼ ご自身のテンプレートIDに書き換えてください ▼▼▼
$template_map = [
'heading_layout' => 272, 'main_text_layout' => 281, 'image_gallery' => 288,
'image_single' => 278, 'youtube_video' => 298, 'link_content' => 301,
'code_block' => 304, 'affiliate_content' => 307, 'raw_html_table' => 504,
'post_list_layout' => 708,
];
// ▲▲▲ IDの書き換えここまで ▲▲▲
$unique_template_ids = [];
foreach ($layouts as $layout) {
if (isset($layout['acf_fc_layout']) && isset($template_map[$layout['acf_fc_layout']])) {
$unique_template_ids[] = $template_map[$layout['acf_fc_layout']];
}
}
if (empty($unique_template_ids)) { return; }
$unique_template_ids = array_unique($unique_template_ids);
$all_css = '';
foreach ($unique_template_ids as $template_id) {
$css_file = \Elementor\Core\Files\CSS\Post::create($template_id);
$all_css .= $css_file->get_content();
}
if (!empty($all_css)) {
echo '<style id="acf-flex-loop-styles">' . $all_css . '</style>';
}
}
add_action('wp_head', 'force_print_acf_loop_styles');
/**
* データを一時的に保持するためのヘルパークラス
*/
if (!class_exists('Flex_Content_Data_Helper')) {
class Flex_Content_Data_Helper {
private static $current_row = null;
public static function set_current_row($row) { self::$current_row = $row; }
public static function get_current_row() { return self::$current_row; }
}
}
/**
* flex_get ショートコード
*/
if (!function_exists('get_flex_sub_field_shortcode_final')) {
function get_flex_sub_field_shortcode_final($atts) {
$current_row = Flex_Content_Data_Helper::get_current_row();
$a = shortcode_atts(array('name' => '', 'output' => 'value'), $atts);
$field_name = $a['name'];
if (empty($field_name) || !is_array($current_row) || !isset($current_row[$field_name])) { return ''; }
$value = $current_row[$field_name];
if ($a['output'] === 'image' && is_array($value) && isset($value['url'])) {
$image_tag = '<img src="' . esc_url($value['url']) . '" alt="' . esc_attr($value['alt']) . '" style="max-width: 100%; height: auto;">';
$link_url = esc_url($value['url']);
return '<a href="' . $link_url . '" class="image-lightbox-link" data-fancybox="single-image">' . $image_tag . '</a>';
}
return wp_kses_post($value);
}
add_shortcode('flex_get', 'get_flex_sub_field_shortcode_final');
}
/**
* flex_get_raw ショートコード
*/
if (!function_exists('get_flex_sub_field_shortcode_raw')) {
function get_flex_sub_field_shortcode_raw($atts) {
$current_row = Flex_Content_Data_Helper::get_current_row();
$a = shortcode_atts(array('name' => ''), $atts);
$field_name = $a['name'];
if (empty($field_name) || !is_array($current_row) || !isset($current_row[$field_name])) { return ''; }
return $current_row[$field_name];
}
add_shortcode('flex_get_raw', 'get_flex_sub_field_shortcode_raw');
}
/**
* display_acf_gallery ショートコード
*/
if (!function_exists('display_acf_gallery_shortcode')) {
function display_acf_gallery_shortcode() {
$current_row = Flex_Content_Data_Helper::get_current_row();
$style_choice = $current_row['gallery_display_style'] ?? 'cover';
$columns = $current_row['gallery_columns'] ?? '2';
$caption = $current_row['gallery_caption'] ?? '';
$images = $current_row['gallery_images'] ?? [];
if ( empty($images) ) { return ''; }
$style_class = 'gallery-style-' . $style_choice;
$column_class = 'columns-' . esc_attr($columns);
$gallery_group_id = 'acf-gallery-' . uniqid();
$output = '<figure class="custom-acf-gallery-wrapper">';
$output .= '<div class="custom-acf-gallery ' . esc_attr($style_class) . ' ' . $column_class . '">';
foreach ($images as $image) {
$full_image_url = esc_url($image['url']);
$thumb_image_url = esc_url($image['sizes']['large'] ?? $full_image_url);
$output .= '<a href="' . $full_image_url . '" class="gallery-lightbox-link" data-fancybox="' . $gallery_group_id . '"><figure class="gallery-item"><div class="gallery-item-bg" style="background-image: url(' . $thumb_image_url . ');"></div></figure></a>';
}
$output .= '</div>';
if ( !empty($caption) ) {
$output .= '<figcaption class="gallery-caption">' . wp_kses_post( wpautop($caption) ) . '</figcaption>';
}
$output .= '</figure>';
return $output;
}
add_shortcode('display_acf_gallery', 'display_acf_gallery_shortcode');
}
/**
* display_acf_links ショートコード
*/
if (!function_exists('display_acf_links_shortcode')) {
function display_acf_links_shortcode() {
$current_row = Flex_Content_Data_Helper::get_current_row();
$repeater_field_name = 'link_list';
if (empty($current_row[$repeater_field_name]) || !is_array($current_row[$repeater_field_name])) { return ''; }
$links = $current_row[$repeater_field_name];
$output = '<ul class="custom-link-list">';
foreach ($links as $link) {
$link_name = $link['link_name'] ?? '';
$link_url = $link['link_url'] ?? '';
if ($link_name && $link_url) {
$output .= '<li><a href="' . esc_url($link_url) . '">' . esc_html($link_name) . '</a></li>';
}
}
$output .= '</ul>';
return $output;
}
add_shortcode('display_acf_links', 'display_acf_links_shortcode');
}
/**
* display_acf_affiliate ショートコード
*/
if (!function_exists('display_acf_affiliate_shortcode')) {
function display_acf_affiliate_shortcode() {
$current_row = Flex_Content_Data_Helper::get_current_row();
if (empty($current_row)) { return ''; }
$columns = $current_row['affiliate_columns'] ?? '1';
$column_class = 'columns-' . esc_attr($columns);
$affiliate_list = $current_row['affiliate_list'] ?? [];
if (empty($affiliate_list)) { return ''; }
$output = '<div class="custom-affiliate-list ' . $column_class . '">';
foreach ($affiliate_list as $ad_item) {
$output .= '<div class="custom-affiliate-item">';
if (!empty($ad_item['affiliate_image']) && !empty($ad_item['affiliate_link_path'])) {
$image_data = $ad_item['affiliate_image'];
$link_path = $ad_item['affiliate_link_path'];
$target_blank = $ad_item['affiliate_link_target'];
$target_attribute = $target_blank ? ' target="_blank" rel="noopener noreferrer"' : '';
$image_tag = '<img src="' . esc_url($image_data['url']) . '" alt="' . esc_attr($image_data['alt']) . '" style="max-width: 100%; height: auto;">';
$output .= '<a href="' . esc_url($link_path) . '"' . $target_attribute . '>' . $image_tag . '</a>';
}
if (!empty($ad_item['affiliate_code'])) {
$output .= '<div class="affiliate-raw-code">' . $ad_item['affiliate_code'] . '</div>';
}
$output .= '</div>';
}
$output .= '</div>';
return $output;
}
add_shortcode('display_acf_affiliate', 'display_acf_affiliate_shortcode');
}
/**
* display_acf_code_block ショートコード
*/
if (!function_exists('display_acf_code_block_shortcode')) {
function display_acf_code_block_shortcode() {
$current_row = Flex_Content_Data_Helper::get_current_row();
if (empty($current_row)) { return ''; }
$code_snippet = $current_row['code_snippet'] ?? '';
$language = $current_row['code_language'] ?? 'markup';
$escaped_code = esc_html($code_snippet);
$output = '<div class="custom-code-block-wrapper">';
$output .= ' <div class="code-block-header">';
$output .= ' <span class="code-language-label">' . esc_html($language) . '</span>';
$output .= ' <button class="copy-code-button">コピー</button>';
$output .= ' </div>';
$output .= ' <pre><code class="language-' . esc_attr($language) . '">' . $escaped_code . '</code></pre>';
$output .= '</div>';
return $output;
}
add_shortcode('display_acf_code_block', 'display_acf_code_block_shortcode');
}
/**
* display_posts_by_category ショートコード
*/
if (!function_exists('display_posts_by_category_shortcode')) {
function display_posts_by_category_shortcode() {
$current_row = Flex_Content_Data_Helper::get_current_row();
if (empty($current_row)) { return ''; }
$category_data = $current_row['target_category'] ?? null;
$posts_per_page = $current_row['posts_per_page'] ?? 3;
$args = [ 'post_type' => 'post', 'posts_per_page' => $posts_per_page, 'post_status' => 'publish' ];
if ( !empty($category_data) ) {
$category_id = is_array($category_data) ? $category_data[0] : $category_data;
if ( is_numeric($category_id) ) {
$args['cat'] = $category_id;
}
}
$the_query = new WP_Query($args);
$output = '';
if ($the_query->have_posts()) {
$output .= '<ul class="custom-post-list">';
while ($the_query->have_posts()) {
$the_query->the_post();
$output .= '<li><a href="' . get_the_permalink() . '">';
if (has_post_thumbnail()) {
$output .= get_the_post_thumbnail(null, 'thumbnail');
}
$output .= '<span>' . get_the_title() . '</span>';
$output .= '</a></li>';
}
$output .= '</ul>';
wp_reset_postdata();
}
return $output;
}
add_shortcode('display_posts_by_category', 'display_posts_by_category_shortcode');
}
/**
* flex_loop_display ショートコード
*/
if (!function_exists('display_flexible_content_loop_shortcode_final')) {
function display_flexible_content_loop_shortcode_final() {
if (!function_exists('get_field') || !have_rows('blog_contents')) { return ''; }
$template_map = [
'heading_layout' => 272, 'main_text_layout' => 281, 'image_gallery' => 288,
'image_single' => 278, 'youtube_video' => 298, 'link_content' => 301,
'code_block' => 304, 'affiliate_content' => 307, 'raw_html_table' => 504,
'post_list_layout' => 708,
];
$is_single_page = is_singular();
$is_logged_in = is_user_logged_in();
$layouts = get_field('blog_contents');
$has_read_more_in_post = false;
if (is_array($layouts)) {
foreach ($layouts as $layout) {
if ($layout['acf_fc_layout'] === 'read_more_divider') {
$has_read_more_in_post = true;
break;
}
}
}
$output_before = '';
$output_after = '';
$read_more_triggered_on_archive = false;
$divider_found_on_single = false;
while (have_rows('blog_contents')) {
the_row();
$layout = get_row_layout();
if (!$is_logged_in && $layout === 'read_more_divider') {
if (!$is_single_page) {
$read_more_triggered_on_archive = true;
break;
} else {
$divider_found_on_single = true;
continue;
}
}
Flex_Content_Data_Helper::set_current_row(get_row(true));
$block_html = '';
if (isset($template_map[$layout])) {
$template_id = $template_map[$layout];
$block_html = \Elementor\Plugin::instance()->frontend->get_builder_content_for_display($template_id);
}
if ($divider_found_on_single && $is_single_page && !$is_logged_in) {
$output_after .= $block_html;
} else {
$output_before .= $block_html;
}
}
$final_html = '';
if ($is_single_page && $has_read_more_in_post && !$is_logged_in) {
$final_html .= $output_before;
$final_html .= '<div id="read-more-trigger"><button id="show-ad-btn">続きを読む</button></div>';
$final_html .= '<div id="hidden-content" style="display: none;">' . $output_after . '</div>';
} else {
$final_html .= $output_before;
}
$final_html = '<div class="flexible-content-wrapper">' . $final_html . '</div>';
if (!$is_single_page && $read_more_triggered_on_archive) {
$final_html .= '<a href="' . esc_url(get_permalink()) . '" class="read-more-link">続きを読む</a>';
}
if ($is_single_page && $has_read_more_in_post && !$is_logged_in) {
$ad_code = get_field('ad_gate_ad_code', 'option');
if ($ad_code) {
$final_html .= '
<div id="ad-overlay" style="display: none;"><div class="ad-overlay-content">' . $ad_code . '<button id="close-ad-btn">広告を閉じて続きを読む</button></div></div>
<script>
document.addEventListener("DOMContentLoaded", function() {
const showAdBtn = document.getElementById("show-ad-btn");
const closeBtn = document.getElementById("close-ad-btn");
const adOverlay = document.getElementById("ad-overlay");
const hiddenContent = document.getElementById("hidden-content");
const triggerWrapper = document.getElementById("read-more-trigger");
if (showAdBtn && adOverlay) { showAdBtn.addEventListener("click", function() { adOverlay.style.display = "flex"; }); }
if (closeBtn && adOverlay && hiddenContent && triggerWrapper) {
closeBtn.addEventListener("click", function() {
adOverlay.style.display = "none";
hiddenContent.style.display = "block";
triggerWrapper.style.display = "none";
});
}
});
</script>';
}
}
return $final_html;
}
add_shortcode('flex_loop_display', 'display_flexible_content_loop_shortcode_final');
}
// --- ACF Flexible Content + Elementor Loop Grid連携コード END ---