douti9253 2017-11-03 13:46
浏览 12
已采纳

切换不起作用的WordPress功能

I have a WordPress-theme that has to be used on multiple sites. So I wrote this function, to determine which site the theme is on:

/*
 * Check which site
*/
function determine_site() {

  $the_site = '';
  $URL = $_SERVER['HTTP_HOST'];

  switch( $URL ){
    case 'domain1.com':
    case 'develop1.ment.com':
      $the_site = 'domain1_com';
      break;
    case 'domain2.com':
    case 'develop2.ment.com':
      $the_site = 'domain2_com';
      break;
    default;
      $the_site = 'domain_undetermined';
      break;
  }
  return $the_site;
}

And this function is then called from another function, which adds it to the body-class on the site, as such:

/*
 * Add body class
 */
function add_custom_body_class( $classes ) {
  $which_language = determine_site();
  $classes[] = $which_language;
  return $classes;
}
add_filter( 'body_class', 'add_custom_body_class' );

But it reaches the default-case and sets domain_undetermined as the body-class (which it should match on one of the first cases). If I print out the $URL-var in the top of the determine_site-function, as such:

function determine_site() {

  $the_site = '';
  $URL = $_SERVER['HTTP_HOST'];
  echo '<pre>;
  print_r($URL);
  echo '</pre>;
  ...
  ...
  ...

Then it outputs this (!?):

develop1.ment.comclass="home page-template-default page page-id-11 logged-in admin-bar no-customize-support wp-custom-logo domain_undetermined">

Why does it print the end of the body-classes, as well?

Another thing that might help figure out where the hatchet is burried... If I run sanitize_title (a WordPress-function) on $URL, as such:

function determine_site() {

  $the_site = '';
  $URL = $_SERVER['HTTP_HOST'];
  $URL = sanitize_title( $URL );
  echo '<pre>';
  print_r($URL);
  echo '</pre>';
  ...
  ...
  ...

... Then it returns this (even though I am on the develop1.ment.com-domain:

www-domain1-comclass="home page-template-default page page-id-11 logged-in admin-bar no-customize-support wp-custom-logo domain_undetermined">

The development-server does some proxy-stuff, which explains this last bit, - but it doesn't explain all of these mysteries.

  • 写回答

1条回答 默认 最新

  • doufei5537 2017-11-06 17:40
    关注

    Would something like this work in place of your own "add body class" function?

    /**
     * Add domain to body class for each site.
     */
    function my_multisite_body_classes( $classes ) {
      $current_blog = get_blog_details( get_current_blog_id() );
      $classes[] = $current_blog->domain;
      return $classes;
    }
    add_filter('body_class', 'my_multisite_body_classes');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路