【问题标题】:Edit site_url with filter使用过滤器编辑 site_url
【发布时间】:2013-03-05 12:50:10
【问题描述】:

使用 WordPress,调用 site_url() 会返回完整的站点 URL (http://www.example.com) 我要做的是在带有过滤器的 URL 末尾添加一些东西 (add-something-here)。

我期待的结果是: http://www.example.com/add-something-here

有人知道如何用过滤器做到这一点吗?

我尝试了以下但没有成功:

function custom_site_url($url) {
    return get_site_url('/add-something-here');
}
add_filter('site_url', 'custom_site_url');

【问题讨论】:

    标签: wordpress filter


    【解决方案1】:

    未经测试,但这是我经常使用的sn-p:

    $constant = 'add-something-here';
    return '<a href="'. esc_url(site_url().'/'.$constant.'/'">'. esc_html( $name ).'</a>'; 
    

    或者您可能只是想获取页面 ID,这样会更好。

     $page_id = '2014';
     return  <a href="'. get_page_link ($page_id) .'">'.esc_html__( 'pagename', 'text-domain' ).'</a>
    

    【讨论】:

      【解决方案2】:

      问题是您正在使用此过滤器生成一个循环。函数get_site_url 正是调用过滤器site_url 的位置。

      你需要:

      add_filter( 'site_url', 'custom_site_url' );
      
      function custom_site_url( $url )
      {
          if( is_admin() ) // you probably don't want this in admin side
              return $url;
      
          return $url .'/something';
      }
      

      请记住,这可能会对依赖真实 URL 的脚本产生错误。

      【讨论】:

        【解决方案3】:

        未经测试,但既然是 PHP,你可以试试...

        function custom_site_url($url) {
            return get_site_url() . '/add-something-here';
        }
        add_filter('site_url', 'custom_site_url');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-02-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-09
          相关资源
          最近更新 更多