要在WordPress首頁調用ID為2的頁面中的相冊,你可以使用以下幾種方法:
方法一:使用短代碼和自定義查詢
首先,在你的主題的functions.php文件中添加以下代碼:
function display_page_gallery($atts) {$atts = shortcode_atts(array('page_id' => 0), $atts);$page = get_post($atts['page_id']);if (!$page) return '';// 提取相冊短代碼$pattern = get_shortcode_regex(array('gallery'));if (preg_match("/$pattern/", $page->post_content, $matches)) {return do_shortcode($matches[0]);}return '';
}
add_shortcode('page_gallery', 'display_page_gallery');
然后,在首頁模板(通常是front-page.php或home.php)中添加:
<?php echo do_shortcode('[page_gallery page_id="2"]'); ?>
方法二:直接修改首頁模板
如果你熟悉主題開發,可以直接在首頁模板中添加:
<?php
$page = get_post(2);
if ($page) {$content = $page->post_content;$pattern = get_shortcode_regex(array('gallery'));if (preg_match("/$pattern/", $content, $matches)) {echo do_shortcode($matches[0]);}
}
?>
方法三:使用插件
安裝并激活”Shortcode in Widgets”插件(如果需要在小工具中使用)
創建一個自定義HTML小工具,添加:
[page_gallery page_id="2"]
注意事項
確保ID為2的頁面確實包含相冊短代碼()
相冊的顯示樣式可能受到主題CSS的影響
如果使用緩存插件,可能需要清除緩存才能看到效果
修改主題文件前建議先創建子主題
根據上面的方法,熟悉wordpress朋友可以自行解決問題。如果你需要更具體的實現方式,我可以提供更針對性的解決方案。
原文
https://www.wpniu.com/article/6631.html