【问题标题】:Trying to add two different strings in PHP尝试在 PHP 中添加两个不同的字符串
【发布时间】:2014-06-03 12:31:28
【问题描述】:

我想出了一个解决方案,当它没有填充小部件时,在 Wordpress 中不显示侧边栏。我通过调用当前页面中的 slug 来做到这一点,并将我的侧边栏命名为与我页面中的 slug 相同的名称。

我使用自定义边栏插件来添加边栏。此插件在侧边栏名称前的侧边栏 ID 中添加“cs-”。例如,我将侧边栏命名为“Test”,而侧边栏的 ID 将是“cs-test”。

现在我希望我的 php 代码检查侧边栏的名称是否与页面中的 slug 相同。所以我打电话给the_slug,在我想添加cs-的蛞蝓之前。在一些帮助下,我想出了这个:

<?php if ( is_active_sidebar( 'cs-'.the_slug(false) ) ) { ?>
    <?php dynamic_sidebar( is_active_sidebar( 'cs-'.the_slug(false) ) ) ?>
    <?php } else { ?>
<?php } ?>

但这似乎没有帮助。任何想法如何解决这个问题?已经谢谢了!

【问题讨论】:

  • 这与jQuery无关。我错过了什么?
  • 对不起。你完全正确!我的错。我想我没有醒着添加那个标签。
  • 嗨,尼克,请检查 the_slug(false) 函数是否正确返回 cs-test。
  • @renishkhunt 是的。当我使用is_active_sidebar( print 'cs-' . the_slug(false) ) 时它可以工作,但是侧边栏旁边也会出现slug。所以这不是正确的解决方案。

标签: php wordpress plugins sidebar


【解决方案1】:

您正在将字符串 cs- 与函数 the_slug(false) 连接起来。这可能会导致错误,具体取决于函数返回的内容。

解决方案 1:

您可以事先调用并保存函数并连接两个变量:

<?php 
$slug = the_slug(false); //Get return value
$slug = (string)$slug; //Force string conversion
$slug = 'cs-'.$slug; //Concate

if ( is_active_sidebar( $slug ) ) { ?>
    <?php dynamic_sidebar( is_active_sidebar( $slug ) ) ?>
    <?php } else { ?>
<?php } ?> 

.

解决方案 2:

如果您需要这种类型的功能,通常尝试将字符串参数添加到 the_slug() 函数(应该是您的 functions.php 中的自定义函数)并在函数本身内进行连接:

function the_slug($boolen, $string='') {
    //Your code

    return $string.$your_var;
}

您的侧边栏代码如下所示:

<?php if ( is_active_sidebar( the_slug(false, 'cs-') ) ) { ?>
    <?php dynamic_sidebar( is_active_sidebar( the_slug(false, 'cs-') ) ) ?>
    <?php } else { ?>
<?php } ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    相关资源
    最近更新 更多