简体   繁体   中英

WordPress : If Statement with HTML and Includes

I want to include a different inline <style> tag that contains a php include in my wordpress header.php file based on wether it's my blog or it's a page.

in simple words i want my blog to have this in the <head>

<style type="text/css">
        <?php include '../css/1.css.php';?>
</style>

and i want my pages to have this other one in the <head>

<style type="text/css">
        <?php include '../css/2.css.php';?>
</style>

I've found this snippet and the distinction between blog and pages works, the problem is the include line

<?php
if(is_page()){
    echo 'Foo';
    include ('../css/above-the-fold-contact.css.php');
    echo 'Example: one';
}
else{
    echo 'Foo something else';
    include ('../css/above-the-fold-blog.css.php');
    echo 'Example: two';
}
?>

it does inject the right file in the right page, but I need to wrap the include around <style> tags, not doing so cause the content of the file to be injected as plain text. how do I do that? what's the syntax? any help is really appreciated, thanks so much!

<?php
    echo '<style type="text/css">';

    if( is_page() )  { include '../css/above-the-fold-contact.css.php'; }
    else             { include '../css/above-the-fold-blog.css.php'; }

    echo '</style>';

EDIT : Just a quick mention - if your CSS is not generated dynamically, then there are better ways of doing this ( like wp_enqueue_style , for example )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM