A. 函数 get_template_part() :
假设您在主题的index.php文件中想要加载content.php,您可以这样做:
/* Environment: We're in index.php */
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'content' );
endwhile;
endif;
除了基本的使用方法之外,get_template_part函数还有一些常见的用法:
get_template_part( 'content', 'single' );
<?=get_template_part('modules/trusty-part') ?>
<?=get_template_part('partials/common/js2') ?>
<?=get_template_part( 'template-parts/content', get_post_format() ); ?>
B. 理解和创建短代码:
/*
注册 [wpkt] 短代码
在主题的 functions.php 中添加如下代码
作用是将正文中的 [wpkt] 替换成 <h2>WordPress课堂, www.wordpressKT.com</h2>
*/
function wpkt_shortcode_handler($atts = array(), $content = null, $tag = '') {
$content = '<h2>WordPress课堂, www.wordpressKT.com</h2>';
return $content;
}
function wpkt_shortcode_register() {
// 短代码
add_shortcode('wpkt', 'wpkt_shortcode_handler');
}
add_action('init', 'wpkt_shortcode_register');
```
- index.php 使用短代码
```php
<head>
<?php wp_head();?>
</head>
<body>
<?php
if (have_posts()) {
while(have_posts()) {
the_post();
// 将正文中的 [wpkt] 替换成 <h2>WordPress课堂, www.wordpressKT.com</h2>
the_content();
// 如果直接获取,会直接返回 [wpkt] 不会进行替换
echo get_the_content();
// 执行替换
echo do_shortcode('[wpkt]');
}
}
?>
<?php wp_footer();?>
</body>
```
Custom Woocommerce products list shortcode:(自选商品组-短代码)
function custom_product_list_shortcode( $atts, $content = null ) {
$_atts = shortcode_atts( [
'ids' => '',
], $atts );
$ids_arr = array_filter( array_map( function( $id ){
return trim( $id );
}, explode( ',', $_atts['ids'] ) ) );
$products = wc_get_products( [
'post_status' => 'publish',
// can't remember if it's 'orderby' or 'order_by''order_by' => [
'title' => 'ASC',
'post_date' => 'DESC',
],
'posts_per_page' => -1,
// you can probably just pass in the comma sep string instead of array but maybe not.
// you need to check that post__in is correct. look at the docs for WP_Query, or WC_Query, or wc_get_products()
'post__in' => $ids_arr,
]);
// you could write your own sorting function like this if you want but you probably shouldn't need to// rsort( $products, function( $p1, $p2 ) {});// the html is for you to completeob_start();
?>
<div class="products-list">
<?php foreach ( $products as $product ) { ?>
<div class="product">
<pre>
<?= print_r( $product, true ); ?>
<?= get_title( $product->ID ); ?>
</pre>
</div>
<?php } ?>
</div>
<?phpreturn ob_get_clean();
}
add_shortcode( 'custom_product_list', 'custom_product_list_shortcode' );
模板上使用shortcode:
[custom_products_list ids='32,21,44,56']
php模板上使用shortcode:
$shortcode_content = storefront_do_shortcode(
'products ',
apply_filters(
'storefront_recent_products_shortcode_args ',
array(
'orderby ' => esc_attr( $args[ 'orderby '] ),
'order ' => esc_attr( $args[ 'order '] ),
'per_page ' => in tval( $args[ 'limit '] ),
'columns ' => in tval( $args[ 'columns '] ),
)
)
);
WooCommerce自带短代码:
[products limit="12" columns="4" orderby="popularity"]
[woocommerce_checkout]
[woocommerce_my_account] 等等.....
参考:https://quadlayers.com/woocommerce-shortcodes-the-ultimate-guide/
D. 自定义邮件模板:
参考:https://kinsta.com/topic/wordpress-plugins/#lms