自成立以来,WordPress附带了很多模板标签。 WordPress中的这些模板标记是PHP函数,可用于输出以及检索数据 。
如果您一直在开发WordPress主题,则您可能对其中的一些模板标签很熟悉,例如显示帖子标题的the_title
,显示帖子作者姓名的the_author
以及帖子的链接。
WordPress不断发展。 每个新版本通常都会引入一些新的模板标签。 如此之多,以至于跟不上所有这些模板标记(无论是新的还是新的)都可能会非常具有挑战性。 查看您可能忽略的这20个模板标签。
WordPress ,根据其指南和标准, 必须使用大写P表示 。 正确的方法是将其拼写为WordPress 。
大写字母“ P”对于扩展的范围非常重要,以至于Matt Mullenweg(WordPress的创始人) 早在2009年就将其包括在其决议中 。 该capital_p_dangit()
引入了capital_p_dangit()
函数。
// Using it straightforwardly
$footer_text = get_theme_mod( "footer_text", "" );
$footer_text = captial_p_dangit( $footer_text ); // Any WordPress text is turned with capital P.
// Or, using it in a WordPress Filter.
add_filter( "the_excerpt", function( $text ) {
return captial_p_dangit( $text );
} );
在4.5中,WordPress引入了通过Customizer上传主题徽标的功能 。 此新功能需要主题支持:通过添加add_theme_support( 'site-logo' )
,徽标将出现在定制程序中。
此功能导致使用了一些新的模板标签,它们可以处理主题上的徽标图像输出,分别是: has_custom_logo()
, get_custom_logo()
和the_custom_logo()
。
// 1. Output includes the image logo and the link back to home.
the_custom_logo();
// 2. Get the custom logo output "string".
$logo = get_custom_logo();
// 3. Conditional
if ( has_custom_logo() ) {
$logo = get_custom_logo();
}
// 4. Using the 'get_custom_logo' to wrap the logo with a div;
add_filter( "get_custom_logo", function( $html ) {
return '
'. $html .'
';
} );
WordPress具有长期集成的本机实用程序,可以添加缩略图或特色图像。 模板标签the_post_thumbnail()
展示了图像标签及其属性。
但是,如果您想通过CSS将图像缩略图显示为背景怎么办? 使用模板标记get_the_post_thumbnail_url()
。
<?php echo get_the_post_thumbnail_url(); // e.g. "http://example.com/path/to/image/thumbnail.jpg"?>
<div class="image-thumbnail" style="background-image; url(<?php echo get_the_post_thumbnail_url() ?>)"></div>
此模板标签将为您提供基于指定范围的随机数。 WordPress内部使用此功能来生成随机密码。 您可能可以使用它为WooCommerce网站生成随机优惠券号。
// Generate a number from 1 to 200
$rand_number = wp_rand( 1, 200 ); // output will not be below 0 or 201 above.
当前,大多数主题都使用the_comments_navigation()
,这将提供“下一个”和“上一个”类型的导航链接。 如果要显示带编号的导航(分页),请用the_comments_pagination()
代替标签。
请记住,模板标记仅在WordPress 4.4.0以上版本中可用。 确保在部署前进行检查。
<?php
// Replace the `the_comments_navigation()`
if ( function_exists( 'the_comments_pagination' ) ) {
the_comments_pagination();
} else {
the_comments_navigation();
}
<ol class="comment-list">
<?php
wp_list_comments( array(
'style' => 'ol',
'short_ping' => true,
'avatar_size' => 42,
) );
?>
</ol>
<!-- .comment-list -->
此模板标签将缩短网址长度。 如此长的URL不会在正文内容中插入新的一行 。 您可以采取2种选择:添加overflow-wrap: break-word;
在您CSS中,或使用url_shorten()
模板标记修剪URL的长度。
$link = get_the_permalink();
$url_text = url_shorten( $link ); // e.g. www.hongkiat.com/blog/css...
echo '<a href="'. $link .'">'. $url_text .'</a>';
我们一直使用wp_enqueue_script
进行注册,加载脚本及其依赖项。 但是, 加载内部脚本并不是wp_add_inline_script
,直到引入了此模板标签wp_add_inline_script
。
添加内联脚本需要将其附加到一个已知的排队脚本。 该处理程序作为脚本的第一个参数传递,类似于wp_localize_script()
函数。 第二个参数应传递脚本的内容。 第三个参数指定内联应该在“之前”还是“之后”输出。
function enqueue_script() {
wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20160412', true );
wp_add_inline_script( 'twentysixteen-script', 'window.hkdc = {}', 'before' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_script' );
// Output:
// <script type='text/javascript'>window.hkdc = {}</script>
// <script type='text/javascript' src='http://local.wordpress.dev/wp-content/themes/twentysixteen/js/functions.js?ver=20160412'></script>
wp_dropdown_languages
模板标记将输出一个HTML选项,该选项显示WordPress网站中的语言列表。 如果需要本地化网站,您会发现此模板标签很有用。 您可以使用它在“ 用户编辑器”屏幕或网站的前端中显示语言选项,以允许用户选择其语言首选项。
wp_dropdown_languages( array(
'id' => 'lang_options',
'name' => 'lang_options',
'languages' => get_available_languages(),
'translations' => array( 'id_ID', 'ja' ), // Indonesia, and Japan
'selected' => 'en_US',
'show_available_translations' => false,
)
);
顾名思义,此模板标签get_avatar_url()
将检索用户头像的图像路径 。 它允许您以自己喜欢的任何方式显示和塑造头像,而不是简单地通过HTML图像标签显示它。
$avatar = get_avatar_url( 'admin@domain.com' );
<div class="avatar-url" style="background-image: url(<?php echo $avatar; ?>)">
</div>
此函数检索包含当前活动主题信息的对象。 这些信息包括主题Slug,名称,版本,文本域,作者等。
在以下代码片段中,我们使用它来检索版本并将其作为脚本版本传递。
$theme = wp_get_theme();
define( 'THEME_SLUG', $theme->template ); //twentysixteen
define( 'THEME_NAME', $theme->get( 'Name' ) ); // Twenty Sixteen
define( 'THEME_VERSION', $theme->get( 'Version' ) ); //1.2
function load_scripts() {
wp_enqueue_script( 'script-ie', $templateuri .'js/ie.js', array( "jquery" ), THEME_VERSION );
wp_script_add_data( 'script-ie', 'conditional', 'lt IE 9' );
}
add_action( 'wp_enqueue_scripts', 'load_scripts' );