如何将 php 放入 php
How to place php inside php
是的,这与 Can you put PHP inside PHP with echo? 相似,但不完全相同,我想真正找到一个解决方案或方法来完成它。
当然,必须有一种方法可以在模板中向特定用户组显示内容...
我有一个 wordpress 菜单,我只想向具有设定角色的用户显示。
我使用此代码显示我的菜单:
<?php wp_nav_menu( array( 'theme_location' => 'max_mega_menu_1' ) ); ?>
但问题是我需要使用像下面这样的代码片段来执行打开和关闭短代码以对其他人隐藏菜单:
<?php
$menu = "my menu code here";
echo do_shortcode("[um_show_content roles='um_efa-pack'] ". $menu ." [/um_show_content]");
?>
我怎样才能想出一种方法来在 PHP 变量中获取我的原始菜单代码?我一直在寻找,但我找不到好的解决方案,我什至不知道该搜索什么。
如果有帮助,菜单还有一个简码,但这也需要 PHP 才能执行,不是吗?
[maxmegamenu location=max_mega_menu_1]
看看Output Control, specifically Output Buffering。
如果您需要 PHP 输出作为变量,这是一个非常容易使用的宝贵工具。
<?php
/* Turn on Output Buffering, effectively 'pausing'
* all PHP output, putting it in a buffer instead.
*/
ob_start();
/* Output/Display the menu. Since we started Output
* Buffering, it won't actually display yet - instead
* it gets added to the buffer.
*/
wp_nav_menu( array( 'theme_location' => 'max_mega_menu_1' ) );
/* Dump the buffer into a variable, and turn off
* the Output Buffer so we can start actually sending
* output to the client again.
*/
$menu = ob_get_clean();
/* Now use the shortcode as you would normally, since
* $menu contains the HTML markup from the nav menu and
* Output Buffering has been turned off with ob_get_clean()
*/
echo do_shortcode( '[um_show_content roles="um_efa-pack"]'. $menu .'[um_show_content]' );
?>
我无法谈论在短代码中嵌入短代码,因为这取决于每个短代码的构建方式。有些不能很好地整合在一起,尤其是 enclosed content。不过,尝试简单地使用它们可能不会有什么坏处:
echo do_shortcode( '[um_show_content roles="um_efa-pack"][maxmegamenu location="max_mega_menu_1"][/um_show_content]' );
您好像在使用插件(嗯?)来处理这个问题。在 WordPress 中,它就像使用 current_user_can()
or comparing against the current User's Roles
检查用户角色一样简单
$user = wp_get_current_user();
if( in_array( 'whatever-role', (array) $user->roles ) ){
// Has the role, show first menu
wp_nav_menu( array( 'theme_location' => 'max_mega_menu_1' ) );
} else {
// Doesn't have the role, show second menu
wp_nav_menu( array( 'theme_location' => 'max_mega_menu_2' ) );
}
是的,这与 Can you put PHP inside PHP with echo? 相似,但不完全相同,我想真正找到一个解决方案或方法来完成它。
当然,必须有一种方法可以在模板中向特定用户组显示内容...
我有一个 wordpress 菜单,我只想向具有设定角色的用户显示。
我使用此代码显示我的菜单:
<?php wp_nav_menu( array( 'theme_location' => 'max_mega_menu_1' ) ); ?>
但问题是我需要使用像下面这样的代码片段来执行打开和关闭短代码以对其他人隐藏菜单:
<?php
$menu = "my menu code here";
echo do_shortcode("[um_show_content roles='um_efa-pack'] ". $menu ." [/um_show_content]");
?>
我怎样才能想出一种方法来在 PHP 变量中获取我的原始菜单代码?我一直在寻找,但我找不到好的解决方案,我什至不知道该搜索什么。
如果有帮助,菜单还有一个简码,但这也需要 PHP 才能执行,不是吗?
[maxmegamenu location=max_mega_menu_1]
看看Output Control, specifically Output Buffering。
如果您需要 PHP 输出作为变量,这是一个非常容易使用的宝贵工具。
<?php
/* Turn on Output Buffering, effectively 'pausing'
* all PHP output, putting it in a buffer instead.
*/
ob_start();
/* Output/Display the menu. Since we started Output
* Buffering, it won't actually display yet - instead
* it gets added to the buffer.
*/
wp_nav_menu( array( 'theme_location' => 'max_mega_menu_1' ) );
/* Dump the buffer into a variable, and turn off
* the Output Buffer so we can start actually sending
* output to the client again.
*/
$menu = ob_get_clean();
/* Now use the shortcode as you would normally, since
* $menu contains the HTML markup from the nav menu and
* Output Buffering has been turned off with ob_get_clean()
*/
echo do_shortcode( '[um_show_content roles="um_efa-pack"]'. $menu .'[um_show_content]' );
?>
我无法谈论在短代码中嵌入短代码,因为这取决于每个短代码的构建方式。有些不能很好地整合在一起,尤其是 enclosed content。不过,尝试简单地使用它们可能不会有什么坏处:
echo do_shortcode( '[um_show_content roles="um_efa-pack"][maxmegamenu location="max_mega_menu_1"][/um_show_content]' );
您好像在使用插件(嗯?)来处理这个问题。在 WordPress 中,它就像使用 current_user_can()
or comparing against the current User's Roles
$user = wp_get_current_user();
if( in_array( 'whatever-role', (array) $user->roles ) ){
// Has the role, show first menu
wp_nav_menu( array( 'theme_location' => 'max_mega_menu_1' ) );
} else {
// Doesn't have the role, show second menu
wp_nav_menu( array( 'theme_location' => 'max_mega_menu_2' ) );
}