Support » Themes and Templates » Per Page CSS Selection

  • I think this should be easy to implement but I’ve been searching, probably using the wrong set or words in the query.

    My site uses the HemingwayEx theme, which has multiple CSS files included. All of these are fine. What I’d like to do is make it so I can select which CSS file to format a page with when I post.

    When I post it uses abc.css and when my wife posts it uses xyz.css. It doesn’t need to be automatic; I’m fine with manually selecting it on the fly.

    Is this doable? Thanks in advance!

    -Jake

Viewing 4 replies - 1 through 4 (of 4 total)
  • This might come close to what you want.

    Add the below code inside your theme functions.php. Then create a new css file, the filename is base on your user “login” name (i.e, login-name.css, jake.css, admin.css) then upload it within your template root directory (same location with the functions.php).

    /**
     * Auto include author stylesheet on WP single and custom page view.
     * @author Avice D (ChaosKaizer)
     * @see WP_Styles
     */
    function wp_auto_include_author_css()
    { global $wp_query;
    
    	$css_path = TEMPLATEPATH.DIRECTORY_SEPARATOR;
    	$css_url = get_bloginfo('template_url').'/';
    
    	if ($wp_query->is_single || $wp_qeury->is_page){
    		$po = $wp_query->get_queried_object();
    
    		$author_name = sanitize_title_with_dashes(strtolower(get_author_name($po->post_author)));
    
    		unset($po);		
    
    		$filename = $author_name.'.css';
    		$handle = $author_name.'-stylesheet';
    
    		if (file_exists($css_path.$filename)){
    
    			wp_register_style($handle, $css_url.$filename);
    			wp_enqueue_style($handle);
    			wp_print_styles();
    		}
    	}
    }
    
    add_action('wp_head','wp_auto_include_author_css');

    If your log-in name is “John Doe” it will look for john-doe.css. Space is replace with dash and all characters is in lowercase.

    Very close, indeed. It seems to work properly for blog posts but not for pages. Any ideas? Much thanks!

    true, my mistake ;(

    find

    if ($wp_query->is_single || $wp_qeury->is_page){

    replace with

    if ($wp_query->is_single || $wp_query->is_page){

    ..

    /**
     * Auto include author stylesheet on WP single and custom page view.
     * @author Avice D (ChaosKaizer)
     * @see WP_Styles
     */
    function wp_auto_include_author_css()
    { global $wp_query;
    
    	$css_path = TEMPLATEPATH.DIRECTORY_SEPARATOR;
    	$css_url = trailingslashit(get_bloginfo('template_url'));
    
    	if ($wp_query->is_single || $wp_query->is_page){
    		$po = $wp_query->get_queried_object();
    
    		$author_name = sanitize_title_with_dashes(strtolower(get_author_name($po->post_author)));
    
    		unset($po);		
    
    		$filename = $author_name.'.css';
    		$handle = $author_name.'-stylesheet';
    
    		if (file_exists($css_path.$filename)){
    
    			wp_register_style($handle, $css_url.$filename);
    			wp_enqueue_style($handle);
    			wp_print_styles();
    		}
    	}
    }
    
    add_action('wp_head','wp_auto_include_author_css');

    Ha Ha… I didn’t even catch that myself. Thank you so much for your code and time… does EXACTLY what I wanted. You F’n ROCK!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Per Page CSS Selection’ is closed to new replies.