如何将此内容包装在我的短代码中?它的内容驻留在模板 php 文件中
How to wrap this content inside my shortcode? It´s content that resides in a template php file
我正在尝试将模板 php 文件的内容包装在一个带有结束标记的短代码中。短代码如下所示:
[hide for="!administrator, !subscriber"]'content goes here??'[/hide]
除了两个用户角色管理员和订户之外,此代码为任何人隐藏内容。
我遇到的问题是,如何将短代码包裹在整个列表项元素周围?
上面提供的短代码中需要包含的代码是:
<?php
/**
* `Call now` quick action.
*
* @since 2.0
*/
if ( ! ( $phone = $listing->get_field('phone') ) ) {
return;
}
$link = sprintf( 'tel:%s', $phone );
?>
<li id="<?php echo esc_attr( $action['id'] ) ?>" class="<?php echo esc_attr( $action['class'] ) ?>">
<a href="<?php echo esc_url( $link ) ?>" rel="nofollow">
<?php echo c27()->get_icon_markup( $action['icon'] ) ?>
<span><?php echo $action['label'] ?></span>
</a>
</li>
我试过用各种方式包装它,但它使页面无法加载。
我试过这样的代码:
?php echo do_shortcode('[hide for="!administrator, !subscriber"]'All the content is put inside here'[/hide]');
但它不起作用,最后看起来很乱。我不是任何类型的程序员,所以我发现这非常困难,但我认为这是可能的。我也知道这样的事情可能应该通过我的子主题中的 functions.php 文件来完成,而不是在主主题中的这个模板文件中完成,但我只是不知道如何为此编写一个函数。
将不胜感激任何形式的帮助!
由于您已经编辑了 PHP,因此您不需要使用隐藏任何内容的简码。您可以简单地检查当前用户角色。
<?php
/**
* `Call now` quick action.
*
* @since 2.0
*/
if ( ! ( $phone = $listing->get_field('phone') ) )
return;
// if you want to not display anything for guests
if ( !is_user_logged_in() )
return;
// if you want to not display anything for anyone but administrator or subscriber
$user = wp_get_current_user();
if ( !in_array( 'administrator', (array) $user->roles ) && !in_array( 'subscriber', (array) $user->roles ) )
return;
$link = sprintf( 'tel:%s', $phone );
?>
<li id="<?php echo esc_attr( $action['id'] ) ?>" class="<?php echo esc_attr( $action['class'] ) ?>">
<a href="<?php echo esc_url( $link ) ?>" rel="nofollow">
<?php echo c27()->get_icon_markup( $action['icon'] ) ?>
<span><?php echo $action['label'] ?></span>
</a>
</li>
在您的 Wordpress functions.php 文件中制作自定义简码功能。
不确定您要做什么,但也许这有助于...
所见即所得:
[hide] Your content...[/hide]
在PHP中:
<?= do_shortcode('[hide]'.$yourcontent.'[/hide]' );?>
在functions.php
function hide_func($atts, $content=""){
return '<span style="display:none">'.$content.'</span>';
}
add_shortcode( 'hide', 'hide_func');
我正在尝试将模板 php 文件的内容包装在一个带有结束标记的短代码中。短代码如下所示:
[hide for="!administrator, !subscriber"]'content goes here??'[/hide]
除了两个用户角色管理员和订户之外,此代码为任何人隐藏内容。
我遇到的问题是,如何将短代码包裹在整个列表项元素周围?
上面提供的短代码中需要包含的代码是:
<?php
/**
* `Call now` quick action.
*
* @since 2.0
*/
if ( ! ( $phone = $listing->get_field('phone') ) ) {
return;
}
$link = sprintf( 'tel:%s', $phone );
?>
<li id="<?php echo esc_attr( $action['id'] ) ?>" class="<?php echo esc_attr( $action['class'] ) ?>">
<a href="<?php echo esc_url( $link ) ?>" rel="nofollow">
<?php echo c27()->get_icon_markup( $action['icon'] ) ?>
<span><?php echo $action['label'] ?></span>
</a>
</li>
我试过用各种方式包装它,但它使页面无法加载。
我试过这样的代码:
?php echo do_shortcode('[hide for="!administrator, !subscriber"]'All the content is put inside here'[/hide]');
但它不起作用,最后看起来很乱。我不是任何类型的程序员,所以我发现这非常困难,但我认为这是可能的。我也知道这样的事情可能应该通过我的子主题中的 functions.php 文件来完成,而不是在主主题中的这个模板文件中完成,但我只是不知道如何为此编写一个函数。
将不胜感激任何形式的帮助!
由于您已经编辑了 PHP,因此您不需要使用隐藏任何内容的简码。您可以简单地检查当前用户角色。
<?php
/**
* `Call now` quick action.
*
* @since 2.0
*/
if ( ! ( $phone = $listing->get_field('phone') ) )
return;
// if you want to not display anything for guests
if ( !is_user_logged_in() )
return;
// if you want to not display anything for anyone but administrator or subscriber
$user = wp_get_current_user();
if ( !in_array( 'administrator', (array) $user->roles ) && !in_array( 'subscriber', (array) $user->roles ) )
return;
$link = sprintf( 'tel:%s', $phone );
?>
<li id="<?php echo esc_attr( $action['id'] ) ?>" class="<?php echo esc_attr( $action['class'] ) ?>">
<a href="<?php echo esc_url( $link ) ?>" rel="nofollow">
<?php echo c27()->get_icon_markup( $action['icon'] ) ?>
<span><?php echo $action['label'] ?></span>
</a>
</li>
在您的 Wordpress functions.php 文件中制作自定义简码功能。
不确定您要做什么,但也许这有助于...
所见即所得:
[hide] Your content...[/hide]
在PHP中:
<?= do_shortcode('[hide]'.$yourcontent.'[/hide]' );?>
在functions.php
function hide_func($atts, $content=""){
return '<span style="display:none">'.$content.'</span>';
}
add_shortcode( 'hide', 'hide_func');