【6.shortcode】
shortcode適合應用在post/page中需要在后臺修改的特定部分。
在function.php上加上:
`require get_parent_theme_file_path( '/inc/shortcode.php' );`
在shortcode.php上加上:
~~~
<?php
function dwwp_sample_shortcode1(){
return '
<section id="gallery-1" class="gallery-1 page_section">
<div class="container">
<div class="row">
<div id="isotope-gallery-container">
';
}
add_shortcode('start', 'dwwp_sample_shortcode1');
function dwwp_sample_shortcode2($template, $content=null){
$template=shortcode_atts(
array(
// image_url不能大寫
'image_url' => 'Default title',
'demo_url' => '12345678',
'title_name' => '12345678'
), $template
);
return '
<div class="col-md-4 col-sm-6 col-xs-12 gallery-item-wrapper artwork creative">
<div class="gallery-item">
<div class="gallery-thumb">
<img src=" ' . $template['image_url'] . ' " class="img-responsive" class="img-responsive" alt="1st gallery Thumb">
<div class="image-overlay"></div>
<a href=" ' . $template['image_url'] . ' " class="gallery-zoom"><i class="fa fa-eye"></i></a>
<a href=" ' . $template['demo_url'] . ' " target="_blank" class="gallery-link" target="_blank"><i class="fa fa-link"></i></a>
</div>
<div class="gallery-details">
<div class="editContent">
<h5><a href=" ' . $template['demo_url'] . ' " target="_blank"> ' . $template['title_name'] . ' </a></h5>
</div>
</div>
</div>
</div>
';
}
add_shortcode('template_code', 'dwwp_sample_shortcode2');
function dwwp_sample_shortcode3(){
return '
</div>
</div>
</div><!-- /.container -->
</section>
';
}
add_shortcode('end', 'dwwp_sample_shortcode3');
~~~
在post/page上加上:
~~~
[start]
[template_code image_url="https://s.tmimgcdn.com/scr/62400/julie-bernerro-photographer-portfolio-responsive-wordpress-theme_62452-original.jpg" demo_url="https://www.templatemonster.com/wordpress-themes/62043.html" title_name="Template 1"]
[end]
~~~
效果如下:

【在php文件顯示shortcode】
`<?php echo do_shortcode('[name_of_shortcode]'); ?>`
參考:
https://developer.wordpress.org/reference/functions/do_shortcode/
- WordPress平臺的網站開發
- 電商主題開發
- WooCommerce主題開發優化部分
- 首頁開發
- WooCommerce
- 判斷用戶是否登錄
- WordPress Menu
- WooCommerce PayPal Checkout Gateway
- 頁面和文章
- 調用產品和文章
- 判斷屬于哪個頁面
- 相關文章
- 消除文章分享按鈕集底部的文字
- wordpress主題模板和主題開發
- wordpress主題準備
- wordpress主題文件結構
- 豪源主題
- WooCommerce SEO
- 插件開發
- wordpress二次開發
- theme基本顯示
- menu調用
- 分拆為header.php和footer.php
- 頁面、文章樣式選擇顯示
- 面包屑導航 Breadcrumb
- 特色圖
- 閱讀次數統計
- 分頁功能
- Advanced Custom Fields
- Custom Post Type UI
- post type
- 小工具
- 小工具調用
- shortcode
- 文章循環輸出
- 標題和文章限制字數輸出顯示
- WordPress主題theme1開發
- wordpress搭建多站點
- wordpress常用函數
- wordpress循環代碼
- Woocommerce
- Woocommerce支持
- WordPress插件開發
- wordpress會員插件
- WordPress插件使用
- WordPress插件集
- WordPress的核心
- Wordpress原理
- Wordpress要點
- WordPress網站搬家
- WPML
- 服務器
- Cloud 9
- test
- 網站