I want to set up CSS for each fixed page and post page in WordPress.In addition, I would like to set CSS for each slag on the fixed page.

Asked 2 months ago, Updated 2 months ago, 5 views

Purpose and Current State

Whenever the expected output changes to a fixed page or a post page, I want you to read each css and design the site.
The completion is in a beautifully styled condition.

Fixed Page Status

Currently, the CSS of each site is not loaded.

Tried

I looked into various parts.But it didn't work.
What I looked up was how to write using the if syntax to branch off between a fixed page and a post page.
I couldn't solve it, so please let me know.

Thank you for your cooperation.

PHP

<?php if(is_single():?>
    <!-- On the post page -->
    <link rel="stylesheet" href="<?php echo get_template_directory_uri();?>/asset/css/blog.css">

    <!--// On a fixed page -->
    <?phpelseif(is_page('contact')): ?>
        <link rel="stylesheet" href="<?php echo get_template_directory_uri();?>/asset/css/conatct.css">
    <?phpelseif(is_page():?>
        <link rel="stylesheet" href="<?php echo get_template_directory_uri();?>/asset/css/blog.css">
    <?phpelse:?>
        <link rel="stylesheet" href="<?php echo get_template_directory_uri();?>/asset/css/style.css">
    <?php endif;?>
}

php wordpress

2022-09-30 14:21

2 Answers

I think some theme has been modified, but

I think the theme file that shows each page in the theme folder will not be reflected unless you set CSS etc. (If the theme folder only has index.php, I will specify it as a conditional branch, but)

)

For example,
If it's a post article, the single.php theme file
Archive.php theme file
for a list of contributed articles Make additional modifications to the .

See below for each theme file.
https://wpdocs.osdn.jp/wiki/images/wp-template-hierarchy.jpg
https://ja.wordpress.org/support/article/using-themes/

P.S. Incorrect modification of inactive themes will not be reflected.

I think I'm debugging it myself, but
Partial excerpts At the bottom of the php code

}

There is , but is it nested in the definition of style as below?
The link tag may be interpreted as the definition of style and ignored.
It is recommended that you check how html is printed in chrome's "Validation of Elements" etc.

<style>
   What class {
       display: none;
<?php if(is_single():?>
    <!-- On the post page -->
    <link rel="stylesheet" href="<?php echo get_template_directory_uri();?>/asset/css/blog.css">

    <!--// On a fixed page -->
    <?phpelseif(is_page('contact')): ?>
        <link rel="stylesheet" href="<?php echo get_template_directory_uri();?>/asset/css/conatct.css">
    <?phpelseif(is_page():?>
        <link rel="stylesheet" href="<?php echo get_template_directory_uri();?>/asset/css/blog.css">
    <?phpelse:?>
        <link rel="stylesheet" href="<?php echo get_template_directory_uri();?>/asset/css/style.css">
    <?php endif;?>
              }
  </style>


2022-09-30 14:21

Slags can be retrieved using $post->post_name instead of is_page(), so they should be able to branch.
However, I think it is more appropriate to use wp_enqueue_scripts in function.php and wp_enqueue_style instead of printing the link tag directly in the head.

You may not understand why, but you should first look at wp_enqueue_scripts and wp_enqueue_style, as well as $post->post_name.

You should be able to branch $post->post_name within a function that causes wp_enqueue_scripts to add_action.

However, it is easier to branch by guessing the page-by-page selectors such as class and id rather than separating the output of the CSS file.
Wordpress gives the body a per-page class.Of course, we can give it to you, so please try it.


2022-09-30 14:21

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.