简体   繁体   中英

Wordpress mapping URL to custom files

I've got a search.php and stuff.php files within a template directory on my wordpress install. When I do http://example.com/search.php I want it to resolve to my custom search.php file. I am currently getting a 404.

The file resides in siteroot/wp-contents/themes/theme-name/

I need the same behavior for another file.

Thanks!

Having PHP files in your theme directory will not automatically be picked up unless you were to navigate to www.yourblog.com/wp-content/themes/your-theme/stuff.php, which actually bypasses WordPress altogether (not good!). By default, WordPress looks for specific files that handle page content of your site, here's a handy picture that shows you how it breaks down: http://codex.wordpress.org/images/1/18/Template_Hierarchy.png

Besides that, if you want to get your own search page going, you'd need to make a template file and then let WordPress know it exists and when to use it. Using the picture I linked above, you can tell that if you have a file called search.php (lowercase) in your theme's directory, WordPress should pick it up and use it as a search page automatically. Besides that, you can also have many different search result pages based on what you need searched. Look into the filter template_include - more info here: http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include

Here's a quick example from the above page which shows how to define a template PHP file for a given use case:

add_filter( 'template_include', 'portfolio_page_template', 99 );

function portfolio_page_template( $template ) {

if ( is_page( 'portfolio' )  ) {
    $new_template = locate_template( array( 'portfolio-page-template.php' ) );
    if ( '' != $new_template ) {
        return $new_template ;
    }
}

return $template;
}

Notice how it says "is_page" a the top? That's a conditional tag, there's many that WordPress comes with to help determine what kind of page is being requested by the visitor. For a list of template tags (including is_search ), check out: http://codex.wordpress.org/Conditional_Tags

Hopefully that helps, reply if you need more help.

Edit - Sorry, I didn't see your title, if you're trying to map a URL to a custom file you'd need to create a rewrite rule. Here's a great walkthrough: http://matty.co.za/2009/11/custom-url-rewrites-in-wordpress/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM