2
\$\begingroup\$

I just started to use Wordpress to start building a website, but as I need to use the get_header() function to retrieve the <head> content and I have a very large project, I wanted to put a css file depending on the page that is being displayed. After a few tries I came up with a solution, but I think that I can do it in a better way and I need your help to optimize it.

Here is my php snippet that goes into the functions.php:

function website_files_per_page() {
    $pagelist = get_pages();
    $index = sizeof($pagelist);
    while ($index --) {
        $pagename = $pagelist[$index]->post_name;
        if (is_page($pagename)){
            $styleName = "website_".$pagename."_styles";
            $styleDir = get_theme_file_uri("/style/".$pagename.".css");
            wp_enqueue_style($styleName, $styleDir);
            break;
        }
    }
}
add_action("wp_print_styles", "website_files_per_page");
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! I notice the performance tag is used - is there a concern about execution time here being higher than desired? \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Oct 19 at 21:31
  • \$\begingroup\$ I'd like my code to be O(1) not O(n) \$\endgroup\$ – maneroto Oct 19 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.