您’我可能在您的文件中看到了如下代码:
打印('%s World', $var );
This code includes a function named 打印()
which in most cases is more efficient than using the 回声 要么 打印 functions.
要了解有关在WordPress中使用PHP代码的更多信息& Genesis, you’ll need to understand how the 打印
function works.
影片指南
快来了!
Printf generally includes a %
percentage sign with an s
for string like this:
%s
Printf can be used in replace of 打印
要么 回声
so rather than use this:
回声 "Hello World";
要么
打印( 'Hello World' );
相反,您可以像这样使用printf:
打印('%s World', "Hello" );
These %
percentage signs with a s
work like placeholders which are converted when the code is executed. In the above case %s
is replaced with the text Hello.
这里’您可以在子主题函数文件中测试的完整工作代码:
add_action('loop_start','printf_example1' );
function printf_example1() {<br />
printf('%s World', 'Hello' );
}
您 can then replace your text ‘Hello’ with a 变量 so the value for your $text
变量 is now equal to Hello:
$text = 'Hello';
您可以在子主题功能文件中测试的完整工作代码如下:
add_action('loop_start','printf_example2');
function printf_example2() {<br />
$text = 'Hello';
printf('%s World', $text );
}
在WordPress主题中,您’ll find code which uses 打印
which you can paste into your child themes functions.php file to see how it works:
add_action('loop_start','printf_example3');
function printf_example3() {<br />
printf( '<a href="%s">' . esc_html__( 'Your link text here', 'text-domain' ) . '</a>', 'http://example.com' ) );
}
The code above uses 打印
with one instance of %s
which is replaced by the URL http://example.com
when the function is executed.
上面的代码也可以这样写:
add_action('loop_start','printf_example4');
function printf_example4() {
<pre><code>$link = esc_url( get_permalink() );
$text = "Value for Your Text Variable";
printf( '<a href="%s">%s</a>', $link, $text );
</code></pre>
}
In this case, we have replaced the URL with a variable named $link
. We have also replaced the text with a variable named $text
.
The value for the $link
变量 is equal to the posts permalink rather than the example.com URL and the value for the $text
变量 is equal to the text.
您’ll还注意到字符串中的每个参数都用逗号分隔,如下所示:
$link, $text
When the code executes, the 1st placeholder %s
is replaced with the 1st parameter which in this case is the $link
变量 which equals get_permalink();
替换完第一个占位符后,将按照它们在字符串中出现的顺序(由逗号分隔)逐步替换第二个,第三个等。
您 can also write the code as follows:
add_action('loop_start','printf_example5');
function printf_example5() {
<pre><code>$text = "Value for Your Text Variable";
printf( '<a href="%s">%s</a>', esc_url( get_permalink() ), $text );
</code></pre>
}
This way the 1st parameter is equal to the get_permalink
function rather than the value for the $link
变量 as in example 4.
打印& sprintf
Printf
用于输出格式化的字符串。例:
add_action('loop_start','printf_example6' );
function printf_example6() {<br />
printf('%s World', 'Hello' );
}
斯普林特 is returned as the value for a variable to be used later then the variable can be output using 回声
要么 打印
. Example:
add_action('loop_start','printf_example7' );
function printf_example7() {<br />
$output = sprintf('%s World', 'Hello' );
echo $output;
}
问问题
会员 can ask questions & get answers.
发表评论
您 must be 登录 to post a comment.