• Resolved Jelly92

    (@jelly92)


    I’m creating a WordPress theme based off a static web page design I’ve done. This is the code I originally have for stylesheets:

    <link rel="stylesheet" type="text/css" href="styles.css" media="screen and (min-width:730px)" >
    <link rel="stylesheet" type="text/css" href="mstyles.css" media="screen and (max-width:729px)">

    How do I write this in WordPress terms? Currently I only see a way to use a single stylesheet without media queries.

Viewing 15 replies - 1 through 15 (of 16 total)
  • You can use wp_register_style() for this purpose, and then call the registered styles with wp_enqueue_style().

    wp_register_style( 'mystyle', get_template_directory_uri() . '/styles.css', null, null, 'screen and (min-width: 730px)' );
    wp_register_style( 'mymobilestyle', get_template_directory_uri() . '/mstyles.css', null, null, 'screen and (max-width: 729px)' );
    
    wp_enqueue_style( 'mystyle' );
    wp_enqueue_style( 'mymobilestyle' );
    Thread Starter Jelly92

    (@jelly92)

    Okay – where would I put that code? The page for wp_register_style seems to imply it can only be used in a plugin?

    You should put the code in your functions.php and call it like this:

    function load_scripts() {
    wp_register_style( 'mystyle', get_template_directory_uri() . '/styles.css', null, null, 'screen and (min-width: 730px)' );
    wp_register_style( 'mymobilestyle', get_template_directory_uri() . '/mstyles.css', null, null, 'screen and (max-width: 729px)' );
    
    wp_enqueue_style( 'mystyle' );
    wp_enqueue_style( 'mymobilestyle' );
    }
    add_action( 'wp_enqueue_scripts', 'gemstones_scripts' );
    Thread Starter Jelly92

    (@jelly92)

    Not sure I understand… So I put the code you originally posted in the functions.php file, and then the next block you posted, well, in the header, or somewhere else? Either way I’ve tried a few combinations and can’t seem to get it to work. Sorry, could you explain? Very grateful for the help by the way – I’m very new to working with WordPress and don’t know any PHP to help me…

    You should put the second block of code I posted yesterday in your functions.php and you don’t need to change your header.php at all.

    Thread Starter Jelly92

    (@jelly92)

    Okay, done that, but it still won’t work :/ In case it’s relevant, if I take the stylesheet link out of the header file it doesn’t load any stylesheets at all, either.

    Do you call wp_head() in your header.php file? It should be the very last thing before the closing </head> tag:

    <head>
    ...stuff...
    <?php wp_head(); ?>
    </head>
    Thread Starter Jelly92

    (@jelly92)

    Ah – no, I didn’t know about that.

    I’ve put it in and am getting this warning:
    call_user_func_array() expects parameter 1 to be a valid callback, function ‘gemstones_scripts’ not found or invalid function name

    That’s my fault; I copied some code from a theme I’m working on. The last line should be:

    add_action( 'wp_enqueue_scripts', 'load_scripts' );

    Thread Starter Jelly92

    (@jelly92)

    Okay, I’ve changed that. It still doesn’t seem to be working.

    Can you post the text of your functions.php? If it’s really long, post it to Pastebin and post the link here instead.

    Thread Starter Jelly92

    (@jelly92)

    It only has what you told me to put in it, should there be more?

    Not necessarily, but sometimes an extra pair of eyes can catch a typo.

    Thread Starter Jelly92

    (@jelly92)

    Well, it’s just this at the moment:

    <?php
    function load_scripts() {
    wp_register_style( 'mystyle', get_template_directory_uri() . '/style.css', null, null, '?screen and (min-width: 730px)' );
    wp_register_style( 'mymobilestyle', get_template_directory_uri() . '/mstyles.css', null, null, '?screen and (max-width: 729px)' );
    
    wp_enqueue_style( 'mystyle' );
    wp_enqueue_style( 'mymobilestyle' );
    }
    add_action( 'wp_enqueue_scripts', 'load_scripts' );
     ?>

    And the code for my Header page, in case the problem is there:

    <!doctype html>
    	<html lang="en">
    	<head>
    	<meta charset="utf-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    	<title>Title</title>
    	<?php wp_head(); ?>
    	</head>
    
    	<body>
    	<header><img src="<?php bloginfo('template_directory'); ?>/images/picture.jpg" alt="pretty picture"/></header>

    You don’t need the question marks before screen and (min-width: 730px) and screen and (max-width: 729px).

Viewing 15 replies - 1 through 15 (of 16 total)

The topic ‘Using multiple stylesheets with media queries’ is closed to new replies.