T
T
thehighhomie2018-02-12 09:36:20
WordPress
thehighhomie, 2018-02-12 09:36:20

Wordpress: competent connection of scripts and styles for different pages?

Is it possible to connect scripts and styles somehow more competently for different pages and not in functions.php via if ( is_page ) ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2018-02-12
@thehighhomie

Binding through is_page works, but on large sites, life will become much more complicated when there are many custom post types, archive pages, etc., when, in addition to including js / css files, you also need to do localize_script ... In general, this code will quickly turn into pasta, which difficult to maintain.
I would do it the other way. What prevents adding a metabox to editing pages, in which you can additionally specify the path to arbitrary assets that are unique to this page? Store them, respectively, in meta or in options. Well, add a function that will check this value and, if it is not empty, load the necessary files.
Profit:
- the code is written once and it is universal for this task
- the code does not depend on IDs or page slugs at all
- when adding/removing assets, it is not necessary to maintain/update the code, it is enough to change the binding in the admin panel

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question