Skip to main content

Loading Assets

The template has functions to load the CSS, JavaScript and images in the theme. Each adds ?ver= (or ?v=) with the version number from config.php, so raise that number when you release changes to the assets.

// config.php
function get_file_version() {
return '1.0.0';
}

Common CSS / JavaScript​

data/css/style.css and data/js/script.js are loaded on every page by include/template.php. Add other files used on every page there too.

// include/template.php
add_action('wp_enqueue_scripts', function () {
loadStyle('common-style', 'data/css/style.css');
loadScript('common-script', 'data/js/script.js', array('jquery'), null, array('strategy' => 'defer'));
});

CSS for one page​

Call loadStyle() in the page template, before get_header().

<?php
loadStyle('company', 'data/css/company.css');

get_header();

JavaScript for one page​

Call loadScript() in the page template, before get_header(). The fifth argument is the $args of wp_enqueue_script().

<?php
// Load slider.js after jQuery, with defer
loadScript('slider', 'data/js/slider.js', array('jquery'), null, array('strategy' => 'defer'));

get_header();

Images​

Get the URL of an image in the theme with get_asset_url(), or print it with the_asset_url().

<img src="<?php the_asset_url('data/img/company/photo.jpg'); ?>" alt="">

Write the .jpg / .png path. The WebP version created on commit is served automatically to browsers that support it.

In the content entered on the admin screens, "data/img/... is replaced with the theme's URL and %home% with the site URL.

<img src="data/img/company/photo.jpg" alt="">
<a href="%home%company/">会社案内</a>