Custom CSS per page in Wordpress

后端 未结 4 1380
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 03:35

Good Morning All,

I have created a different background and a few other images for certain pages within our site. Currently the below code is working well with the \"kid

相关标签:
4条回答
  • 2021-01-21 04:10

    Figured out i needed the ' ' around them

    <body<?php if ( is_page(array('newsletter-1', 'newsletter-2', 'newsletter-3'))) { echo '   class="myclass" '; } ?>>
    
    0 讨论(0)
  • 2021-01-21 04:12

    Note: I know the 3 current answers are a common solution to this problem for WP, but in practice, I hate it. It produces bloated HTML and monolithic CSS :/

    Answer: You could use a template file assigned to any number of pages to add specific CSS/Javascript like so:

    <?php
    function my_scripts_method() {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
        wp_enqueue_script( 'jquery' );
    }    
    
    add_action('wp_enqueue_scripts', 'my_scripts_method');
    ?>
    

    Source: http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    0 讨论(0)
  • 2021-01-21 04:13

    Another option is simply replacing

    <body>
    

    with

    <body <?php body_class(); ?>>
    

    Then you can target it it via css as follows:

    body.page-id-2, body.parent-pageid-2 { background-color: red; }
    

    Where the ID is the ID of the page/parent-page you are targeting. This will keep your template clear of logic, and allow you the same customization options your current method is using.

    0 讨论(0)
  • 2021-01-21 04:22

    I may have over thought this one, but here's my solution.

    The script below automatically gets all the children of a specific parent page and prints the class you need. This solution works great if you don't want to have to update the list of children pages manually.

    This goes in your header.php file:

    <?php 
        //set page id 
                $page_id = '98';
    
               //get page's children 
        $children = get_pages('child_of='.$page_id);
        foreach($children as $child){
           $children_ids = $child->ID;
        }
    
        if(is_page($page_id) || is_page($children_ids)){
           echo 'class="kids';
        }
    

    ?>

    Thanks, and I hope this helps!

    0 讨论(0)
提交回复
热议问题