[英]If ($image) and is_page_template
我想采用现有功能并根据所使用的页面模板(WordPress)更改其输出。 下面看是否存在图像,然后先输出文本,然后再输出图像,因此图像在视觉上位于右侧。 在所有模板上的行为相同。
我已经做过一些研究,尝试了一些尝试,但似乎无法破解。 我需要的是说如果页面模板为'template_left.php'则在内容之前输出cta图像的内容。 同样,如果页面模板为'template_right.php',则在内容后输出cta图像。
感谢所有编辑以下内容的帮助和建议。
<?php
function call_to_action_shortcode($attrs, $content = null) {
$image = reset(rwmb_meta( "avenir_cta_image", 'type=image&size=large' ));
if ($image) {
$styles = "<style>
.cta-image {
background-image: url('{$image['url']}');
}
</style>";
$output = '<div class="page-cta row">' .
'<div class="content">' .
$content .
'</div>' .
'<div class="cta-image pull-right"></div>' .
'</div>';
return $styles . $output;
}
}
add_shortcode('call-to-action', 'call_to_action_shortcode');
我认为您正在寻找的WordPress函数是get_page_template_slug($post_id)
这是在您的情况下如何使用它的示例:
function call_to_action_shortcode($attrs, $content = null) {
$image = reset(rwmb_meta( "avenir_cta_image", 'type=image&size=large' ));
if ($image) {
$template_name = get_page_template_slug(get_the_ID());
if('template_left.php' === $template_name) {
// your code for this template goes here
} else {
// code for other templates here
}
}
}
add_shortcode('call-to-action', 'call_to_action_shortcode');
编辑:
根据get_page_template_slug的文档 ,在循环中使用时,post_id默认为当前帖子,因此您可以省略该参数。 短代码通常在WordPress循环中调用。 如果没有页面模板,此函数将返回空值;如果不是页面模板,则此函数将返回false(对于帖子和其他自定义帖子类型,则返回false)。 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.