duaijiao0648 2017-08-18 03:12
浏览 29
已采纳

如何在PHP中声明变量而不在我的html中显示其值?

I am working in a Wordpress website and I need to pass the url of a page as a argument of a function. In this case I am using

bloginfo('url');

to retrieve the base url of the site.

So my code is

<?php 

$portfolio_link = bloginfo('url');

if ( ! function_exists( 'ow_nav' ) ) {
    function ow_nav( $nav_position ) {
        ?>
        <nav id="ow-nav-<?php echo $nav_position; ?>" class="ow-nav clearfix ow-nav-<?php echo $nav_position; ?>">
            <ul class="ow-nav-list">
                <?php ow_nav_item( '#home',         'Home'                ); ?>
                <?php ow_nav_item( '#about',        'About'               ); ?>
                <?php ow_nav_item( '#criteria',     'Investment Criteria' ); ?>
                <?php ow_nav_item( '#approach',     'Approach'            ); ?>
                <?php ow_nav_item( '#team',         'Team'                ); ?>
                <?php ow_nav_item( $portfolio_link, 'Portfolio'           ); ?>
            </ul>
        </nav>
        <?php
    }
}

if ( ! function_exists( 'ow_nav_item' ) ) {
    function ow_nav_item( $link, $text ) {
        ?>
        <li>
            <a href="<?php echo $link; ?>">
                <span class="ow-txt"><?php echo $text; ?></span>
                <span class="ow-dot"></span>
            </a>
        </li>
        <?php
    }
}

But when I do that, that variable gets leaked in my HTML, displaying the url in a random place in the DOM.

Any ideas?

  • 写回答

1条回答 默认 最新

  • duanfu6160 2017-08-18 03:16
    关注

    bloginfo() automatically echoes the returned value, as you have seen. To return the value and store it in a variable, you need to use get_bloginfo() instead, e.g.:

    $portfolio_link = get_bloginfo('url');
    

    bloginfo ref: https://developer.wordpress.org/reference/functions/bloginfo/
    get_bloginfo ref: https://developer.wordpress.org/reference/functions/get_bloginfo/

    Note: You will come across many Wordpress functions are like this, where one version will automatically display the value and a second version (usually preceded by get_) return the value e.g. the_title / get_the_title

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示