Php wordpress中特定url下所有页面的自定义css文件

Php wordpress中特定url下所有页面的自定义css文件,php,wordpress,Php,Wordpress,我有一个网站,它有两个部分,每个部分都有不同的css和特定的URL(即testsite.com/one/和testsite.com/two/),我需要将特定的样式表应用到这些URL中的每个URL以及这些URL下的所有路径(即testsite.com/one/testsite.com/one/test2等等)因此,对于testsite.com/one/*all将获得一个名为one.css的css文件,对于testsite.com/two/*all将获得一个名为two.css的css文件 我还没有

我有一个网站,它有两个部分,每个部分都有不同的css和特定的URL(即testsite.com/one/和testsite.com/two/),我需要将特定的样式表应用到这些URL中的每个URL以及这些URL下的所有路径(即testsite.com/one/testsite.com/one/test2等等)因此,对于testsite.com/one/*all将获得一个名为one.css的css文件,对于testsite.com/two/*all将获得一个名为two.css的css文件


我还没有测试过这一点,但我的想法是找到顶级祖先并与文章/页面标题进行比较

$post = get_post();
if ($post->post_parent) {
    $ancestors=get_post_ancestors($post->ID);
    $root=count($ancestors)-1;
    $parent = $ancestors[$root];
} else {
    $parent = $post->ID;
}

if(strtolower(get_the_title($parent)) == 'two') {
    wp_enqueue_style('two', get_template_directory_uri().'/two.css',false,'1','screen' );
}

我还没有对此进行测试,但我的想法是定位顶级祖先,并与帖子/页面标题进行比较

$post = get_post();
if ($post->post_parent) {
    $ancestors=get_post_ancestors($post->ID);
    $root=count($ancestors)-1;
    $parent = $ancestors[$root];
} else {
    $parent = $post->ID;
}

if(strtolower(get_the_title($parent)) == 'two') {
    wp_enqueue_style('two', get_template_directory_uri().'/two.css',false,'1','screen' );
}

您可以尝试检查请求的URI是否包含标识“子站点”的字符串

代码现在扩展到demo else、elesif和“or”,以涵盖注释中的后续问题。评论中的问题


您可以尝试检查请求的URI是否包含标识“子站点”的字符串

代码现在扩展到demo else、elesif和“or”,以涵盖注释中的后续问题。评论中的问题


这可能取决于您是否指定子页面具有父页面。或者如果他们只是在这条道路上

如果子页面具有指定为父页面的页面,则可以执行以下操作:

// for the number, use the ID of the parent page

if ( is_page( 'one' ) || '1' == $post->post_parent ) {    
    // the page is "one", or the parent of the page is "one"
    wp_enqueue_style('one', get_template_directory_uri().'/one.css',false,'1','screen' );
}

if ( is_page( 'two' ) || '2' == $post->post_parent ) {    
    // the page is "two", or the parent of the page is "two"
    wp_enqueue_style('two', get_template_directory_uri().'/two.css',false,'1','screen' );
}

如果没有指定父页面,您将需要检查上面由scytale提到的永久链接。

这可能取决于您是否指定子页面具有父页面。或者如果他们只是在这条道路上

如果子页面具有指定为父页面的页面,则可以执行以下操作:

// for the number, use the ID of the parent page

if ( is_page( 'one' ) || '1' == $post->post_parent ) {    
    // the page is "one", or the parent of the page is "one"
    wp_enqueue_style('one', get_template_directory_uri().'/one.css',false,'1','screen' );
}

if ( is_page( 'two' ) || '2' == $post->post_parent ) {    
    // the page is "two", or the parent of the page is "two"
    wp_enqueue_style('two', get_template_directory_uri().'/two.css',false,'1','screen' );
}

如果没有指定父级,您需要检查上面由scytale提到的永久链接。

没有运气。它只适用于第一页。这跟用“post”代替“page”有关系吗?运气不好。它只适用于第一页。这与使用“post”而不是“page”有关吗?首先,您是如何创建/one和/two的?这些是自定义的文章类型、类别或标签吗?是的,这些是页面。嵌套在它们下面的是子页面。二>子1>子2等等。首先,你是如何创建/1和/2的?这些是自定义的文章类型、类别或标签吗?是的,这些是页面。嵌套在它们下面的是子页面。两个>子1>子2等等…好吧,如果我想添加另一个字符串怎么办?例如/one/?感谢您接受ans。对于多个css选项,您可以将if语句扩展为“if(one)else(two)”或“if(three)elseif(two)else(one)”等。您可以在问题示例中使用“if else”。是否添加其他字符串以显示相同的css文件<代码>如果(strpos($perma,/two/'| | |'/other/')!==false){wp_enqueue_style('two',get_template_directory_uri()。/two.css',false,'1','screen');}不起作用这里的“或”语句是什么?我修改了原始答案以涵盖| | | |,elseif和其他内容。PHP将整数0和字符串“0”计算为FALSE所有其他字符串/int值都将计算为true如果您不在其自身的strps函数中放置“/other/”,它将始终导致or子句计算为true。希望这有帮助。好吧,如果我想添加另一个字符串呢?例如/one/?感谢您接受ans。对于多个css选项,您可以将if语句扩展为“if(one)else(two)”或“if(three)elseif(two)else(one)”等。您可以在问题示例中使用“if else”。是否添加其他字符串以显示相同的css文件<代码>如果(strpos($perma,/two/'| | |'/other/')!==false){wp_enqueue_style('two',get_template_directory_uri()。/two.css',false,'1','screen');}不起作用这里的“或”语句是什么?我修改了原始答案以涵盖| | | |,elseif和其他内容。PHP将整数0和字符串“0”计算为FALSE所有其他字符串/int值都将计算为true如果您不在其自身的strps函数中放置“/other/”,它将始终导致or子句计算为true。希望这有帮助。