Embedding a project from PHP
In the PHP templates of your theme, you can print a project where it should appear, show it only on some pages, change its settings for that place, or get its HTML as a string.
On this page
Call layerslider() in a PHP template file of your theme, at the place where you want the project to appear. The function exists only while LayerSlider is active. Guard every call with function_exists( 'layerslider' ), so that the template keeps working when LayerSlider isn’t running.
You need the project’s ID or its slug. In the LayerSlider dashboard, each project’s card shows its ID after its name, for example #12. The slug is set in Project Settings › Publish › Project Slug & Keywords. To find either one from your own code, query the projects.
layerslider( $id = 0, $filters = '', $options = [] )Parameters
The function takes 3 parameters, and the last 2 can be left out.
| Parameter | Type | What it is |
|---|---|---|
$id |
integer or string | The project’s ID or its slug |
$filters |
string | The places to print the project on, as a comma-separated list of items: homepage for the front page, a page’s ID or slug, or a category, for the posts in it. Without $filters, the project is printed wherever the function is called |
$options |
array | Project settings to change for this call only, keyed by the setting’s key. Without $options, the project uses its own settings |
With homepage,contact in $filters, the function prints the project on the front page and on the page whose slug is contact. Write the items without spaces after the commas: a slug after a space doesn’t match its page.
[ 'autostart' => false ] in $options turns off the Auto-Start Slideshow switch. Set a switch with true or false: the string 'false' doesn’t turn a switch off.
The [layerslider] shortcode takes the same project, places, and settings in its attributes. The article on the shortcode gives everything that an item of $filters can match, how to write a setting’s key, and how to turn a switch off in an attribute.
If your code was written for LayerSlider 8, see which changes in LayerSlider 9 affect it.
Getting the HTML as a string
layerslider() prints the project’s HTML at the place of the call and returns nothing. To get the HTML as a string, call the WordPress function do_shortcode() with the [layerslider] shortcode. do_shortcode() returns the same HTML that layerslider() prints.
To change a project’s HTML while LayerSlider builds it, use the layerslider_slider_markup filter. See how to wrap or change the HTML with that filter.
Examples
Each example uses the slug summer-sale. Change summer-sale to your project’s slug or ID.
The first example prints the project where the template calls layerslider().
<?php
if ( function_exists( 'layerslider' ) ) {
layerslider( 'summer-sale' );
}
?>The second example prints the project on the front page and on the page with the ID 42.
<?php
if ( function_exists( 'layerslider' ) ) {
layerslider( 'summer-sale', 'homepage,42' );
}
?>The third example sets the Start with Slide field to 2 for this call only.
<?php
if ( function_exists( 'layerslider' ) ) {
layerslider( 'summer-sale', '', [ 'firstslide' => 2 ] );
}
?>The fourth example puts the project’s HTML into a variable, to use in your own code. It checks that the shortcode exists, because without LayerSlider, do_shortcode() returns the shortcode’s text unchanged.
<?php
$html = '';
if ( shortcode_exists( 'layerslider' ) ) {
$html = do_shortcode( '[layerslider id="summer-sale"]' );
}
?>