Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于类别将css文件添加到单个帖子模板_Php_Css_Wordpress_Wordpress Theming - Fatal编程技术网

Php 基于类别将css文件添加到单个帖子模板

Php 基于类别将css文件添加到单个帖子模板,php,css,wordpress,wordpress-theming,Php,Css,Wordpress,Wordpress Theming,我使用这个函数(在function.php)为特定类别的slug添加模板,但是我也希望使用特定的css文件,但是我不知道如何将其正确嵌入过滤器中。我在下面尝试过,但这只是呈现一个空白页面(因此php错误),感谢您的帮助: 原始代码,用于在Function.php中创建帖子类别模板(即“single categoryname.php”): add_filter('single_template', create_function( '$the_template', 'foreach

我使用这个函数(在
function.php
)为特定类别的slug添加模板,但是我也希望使用特定的css文件,但是我不知道如何将其正确嵌入过滤器中。我在下面尝试过,但这只是呈现一个空白页面(因此php错误),感谢您的帮助:

原始代码,用于在Function.php中创建帖子类别模板(即“single categoryname.php”):

add_filter('single_template', create_function(
    '$the_template',
    'foreach( (array) get_the_category() as $cat ) {
        if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") )
        return TEMPLATEPATH . "/single-{$cat->slug}.php";
        }
    return $the_template;' )
);
我的atempt包含外部css失败:

        add_filter('single_template', create_function(
            '$the_template',
            'foreach( (array) get_the_category() as $cat ) {
                if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") )
                return TEMPLATEPATH . "/single-{$cat->slug}.php",

       //my code:
       wp_enqueue_style( 'single-{$cat->slug}', get_template_directory_uri() . '/css/single-{$cat->slug}.css', array(), '1.1', 'all');  
      }
            return $the_template;' )
      });

我还没有为特定的模板添加CSS,所以我已经从我自己的一个项目(简化)和函数中混合了以下内容

add_action('wp_enqueue_scripts', 'mcd_theme_styles');

if( !function_exists("mcd_theme_styles") ) {
    function mcd_theme_styles() {
        wp_register_style( 'some-css', get_template_directory_uri() . '/assets/dist/css/some-css.css', array(), '1.4', 'all' );

        if ( is_page_template( 'templates/about.php' ) ) {
            wp_enqueue_style( 'some-css' );
        }

        if ( is_singular() ) {
            wp_enqueue_style( 'different-css' );
        }
    }
}

它回答了问题的第一部分,这对第二部分有帮助吗?

因此,在谷歌搜索之后,我找到了一个非常简单的答案:

add_filter('single_template', create_function(
    '$the_template',
    'foreach( (array) get_the_category() as $cat ) {
        if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") )
        return TEMPLATEPATH . "/single-{$cat->slug}.php";
        return STYLESHEETPATH . "css/single-{$cat->slug}.css";
        }
    return $the_template;' )
);

请务必说明使用wp_排队_样式时会发生什么情况。短语“//My尝试包含css文件失败”中几乎没有嵌入任何信息。由于尝试执行代码,是否记录了任何输出或堆栈跟踪?您是否尝试设置日志记录或调试?抱歉,我遇到了一个PHP错误,因此整个页面呈现为空白。谢谢,这是朝着正确方向迈出的一步。如果您可以复制该错误的文本,那么这对于帖子和故障排除过程都是很好的。您可能需要使用PHP日志来解决如何完成这类工作,但这是有用的,我在调试时会得到这样的结果:“解析错误:语法错误,在/www/webvol18/td/5azsfnodhbyqngs/utt.se/public_html/wp-content/themes/utt.se/functions.PHP的第49行出现意外的‘single’(T_STRING)”我注意到,在您的回答中,if的返回以分号结尾,而在您的问题中,它以逗号结尾。出于好奇,你原来帖子的第49行是哪一行?