Php WordPress按域查询变量
我想为来自某个域的所有查询添加一个查询变量 例如,mydomain.com和proxydomain.com都显示相同的WordPress站点,但是对于通过proxydomain.com访问的用户,我希望能够以不同的方式处理他们的查询 此外,我想为通过proxydomain.com访问的访问者应用一些不同的CSS样式Php WordPress按域查询变量,php,css,wordpress,query-variables,Php,Css,Wordpress,Query Variables,我想为来自某个域的所有查询添加一个查询变量 例如,mydomain.com和proxydomain.com都显示相同的WordPress站点,但是对于通过proxydomain.com访问的用户,我希望能够以不同的方式处理他们的查询 此外,我想为通过proxydomain.com访问的访问者应用一些不同的CSS样式 我在想我可以检查查询变量并根据该变量的存在应用类。这是要添加到函数中的代码。php文件: add_filter( 'body_class', 'domain_as_body_clas
我在想我可以检查查询变量并根据该变量的存在应用类。这是要添加到
函数中的代码。php
文件:
add_filter( 'body_class', 'domain_as_body_class' );
function domain_as_body_class( $classes ) {
$classes[] = sanitize_title( $_SERVER['SERVER_NAME'] );
return $classes;
}
它将站点的净化域(即mydomain com
或proxydomain com
)添加为页面的body
标记的类,因此您可以针对自定义样式的相对类
更新
对于查询,您可以在functions.php
中再次添加函数,如:
function is_proxydomain() {
return 'proxydomain.com' == $_SERVER['SERVER_NAME'];
}
然后在查询中需要时使用它:
if( is_proxydomain() ) {
$args = array(
// arguments for proxydomain.com
);
} else {
$args = array(
// arguments for mydomain.com
);
}
$query = new WP_Query( $args );
我喜欢第一部分的答案
对于查询,我认为最好扩展WP_查询类(即WP_Query_Custom)并为每个域提供一个副本。然后,您可以根据functions.php文件中的域加载所需的文件,这样以后您就不需要在使用WP\u Query\u Custom的任何地方更改调用,即使您需要添加更多域和不同版本的WP\u Query\u Custom
//in functions.php
$mydomain = str_replace('.', '_', $_SERVER['SERVER_NAME']);
require_once("path/to/my/classes/$mydomain/WP_Query_Custom.php");
//In each path/to/my/classes/$mydomain/WP_Query_Custom.php
class WP_Query_Custom extends WP_Query {
function __construct( $args = array() ) {
// Force these args
$args = array_merge( $args, array(
'post_type' => 'my_custom_post_type',
'posts_per_page' => -1, // Turn off paging
'no_found_rows' => true // Optimize query for no paging
) );
add_filter( 'posts_where', array( $this, 'posts_where' ) );
parent::__construct( $args );
// Make sure these filters don't affect any other queries
remove_filter( 'posts_where', array( $this, 'posts_where' ) );
}
function posts_where( $sql ) {
global $wpdb;
return $sql . " AND $wpdb->term_taxonomy.taxonomy = 'my_taxonomy'";
}
}
示例类是从复制而来的,问题到底是什么?它不是您要求的(因此我将其作为注释而不是答案),但是您可以轻松地使用$\u SERVER['REMOTE\u HOST']获取请求并在其上运行比较。如果($\u SERVER['REMOTE\u HOST']=='proxydomain.com'){//执行一些特定于proxydomain的操作或分配一个变量}您显然应该测试文件是否存在,比如
code
if(文件存在(“path/to/my/classes/$mydomain/WP\u Query\u Custom.php”){//load}否则{//handle error}code