Switched query attribute from "name" to "pagename"
Added empty check for wp_query->query to suppress warnings on the home page