“Creating a Static Front Page”的版本间的差异

来自WordPress中文文档
跳转至: 导航搜索
(Reverted edits by Bluverdesk (Talk); changed back to last version by Stevish
(1个修订)
(没有差异)

2009年3月14日 (六) 09:23的版本

UPDATE: With the release of WordPress Version 2.1, the option to set your own front page can be accomplished via your Administration Settings Reading panel. However unlike the old explanation that follows, you must NOT name your home page template file home.php. If you do, this will cause a conflict with the WordPress 2.1 system. The approach that follows on this page is no longer necessary with WordPress 2.1 or newer

includeonlydiv style=clear:both; background-color:#FFFFFF; border:1px solid #FFFFFF; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;span style=float:left; margin:4px/spanThis article, written to accord with WordPress 2.0, is now obsolete. Please take caution when following its contents, as many things may have changed./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude

Known as a static front page or splash page, this is the first page seen by users entering your site. Unlike a traditional WordPress front page, featuring the WordPress Loop which generates a list of your most recent posts, the static front page is a customized page that lacks The Loop and displays static information that can either invite people to click through to the real good stuff or offer highlighted post or article features and information that direct users to different areas of your WordPress site. It can have static information and links that do not change, or it can incorporate some automatically changing information.

This page contains a technique that requires editing several files. There's a different, shorter technique to get a static front page while maintaining a separate blog area, that is based on WordPress's static pages, and another one that works entirely with template files. Both of these techniques, unlike WordPress 2.1's front page setting, lend themselves to creating a richer, more interactive front page.

Using WordPress as CMS

If your goal is to have a WP-created Page with static information called Home at example.com/, and the standard blog index at example.com/news/, the method below may cause the more tag to fail.

Use a plugin such as Filosofo Home-Page Control instead. It works fine with WP installed in root.

Create and Upload a Static Front Page

By default, most websites feature their main page as one of the following, and you can set yours to be any of these:

  • ttindex.html/tt
  • tthome.html/tt
  • ttindex.php/tt
  • tthome.php/tt
  • ttdefault.htm/tt

To create your static front page:

  1. Using a text editor, open a blank document.
  2. Design the inside of your Static Front Page.
  3. Save it with any name listed above (Example: tthome.php/tt).
  4. If you have WordPress in another folder, upload your static page to your root HTML folder.

Please note that your static page may need to have the file extension .php in order for the PHP code to be processed. If your static page is index.html, then the PHP code may not be executed.

Install WordPress in its own folder or, if you have already installed it, consider moving WordPress to its own folder (or follow instructions below if you do not want to move entire WordPress core files -- but you might have some problems when upgrading).

If WordPress is in Root

If you do not want to move your WordPress installation to another folder, or you are in a hurry, you can use these instructions or look below.

  1. Create a new folder on your site server for WordPress ttindex.php/tt file.
  2. Download ttindex.php/tt WordPress file and open it in a text editor.
  3. Change the ttrequire('./wp-blog-header.php');/tt line to ttrequire('../wp-blog-header.php');/tt
  4. Save and upload the modified ttindex.php/tt file into the new folder.
  5. Login into your WordPress Administration Panel and go to Settings General.
  6. Change your Blog URI pointing to the new folder. For example, if your site is ttnowikihttp://example.com/nowiki/tt, and the new folder is called ttblog/tt, enter ttnowikihttp://example.com/blog/nowiki/tt.

Now, upload your splash page to your root server folder.

Change Apache Configuration File

There is another form to upload your static front page without moving all your WordPress files to another folder.

When you request a folder in a Apache web server (for example the folder called tt/chouette/tt), the core of Apache will search for a file with any of the names in a list saved in tthttpd.conf/tt. If your server lets you use tt.htaccess/tt files for change the original configuration, you can follow these steps:

  1. Open the tt.htaccess/tt file from your root web folder in a text editor.
  2. At the top of the file, add:

preDirectoryIndex home.php index.php/pre

Save and upload the modified tt.htaccess/tt file.

You need to mention ttindex.php/tt as a second choice, otherwise you will not be able to access the ttindex.php/tt in directories below the wordpress root directory unless you explicitly mention it in the URL. For instance, the url:

http://wordpress root URL/wp-admin/

would not work.

Integrating WordPress

To incorporate or integrate WordPress with your static front page, you can include the header, sidebar, footer, and even a mini-version of the WordPress Loop.

Include the Header

To include the header from your WordPress Theme to maintain a consistent look, add this to the top of the page, using the folder path name you installed WordPress into:

pre?php define('WP_USE_THEMES', false); require('./wordpress/wp-blog-header.php'); get_header(); ?/pre

The purpose of requiring or including wp-blog-header.php is to let you make use of any WordPress function or template tag. When you include it in your page a function such as get_header() becomes available, just as it is in the standard WordPress templates.

Note: In rare cases defining the codeWP_USE_THEMES/code constant to false may produce non-CSS formatted page output. To workaround this you can try replacing code('WP_USE_THEMES', false)/code with code('WP_USE_THEMES', true)/code.

Include The Sidebar or Footer

To add the sidebar and/or footer from your WordPress Theme, include one or both of the following at the bottom of the page, using the same folder path:

pre?php get_sidebar(); ?

?php get_footer(); ?/pre

Adding a Mini-Loop

You can incorporate a customized-version of the WordPress Loop in a static page to generate a short list of the latest posts on your WordPress blog. This mini-Loop acts like a feed, displaying your posts in a non-WordPress, PHP document. Change the value for $how_many to reflect the number of posts you want to show, and make sure the path to your wp-config.php file is correct.

pre ?php $how_many=5; //How many posts do you want to show require_once('wp-config.php'); // Change this for your path to wp-config.php file ?

ol id=whats-new ? $news=$wpdb-get_results(SELECT `ID`,`post_title` FROM $wpdb-posts WHERE `post_type`=\post\ AND `post_status`=\publish\ ORDER BY post_date DESC LIMIT $how_many); foreach($news as $np){ printf (lia href=\index.php?p=%s\%s/a/li, $np-ID,$np-post_title); }? /ol /pre

Also, change index.php in the printf statement to include the path to the index.php in your installation directory.

Alternatively, if using custom Permalinks on your blog, you can set the code to retrieve the permalink for each article and use this as the link to the post. This helps with indexing of your site by search engines such as Google, which can blacklist your site if it considers that pages duplicate content (the same content would be accessible both by using the address tt{YourWordPressURL}/?p=x/tt and tt{YourWordPressURL}/permalink-address//tt). To use permalinks on your static page: pre ?php $how_many=5; //How many posts do you want to show require_once('wp-config.php'); // Change this for your path to wp-config.php file ? ol id=whats-new ? $news=$wpdb-get_results(SELECT `ID`,`post_title` FROM $wpdb-posts WHERE `post_type`=\post\ AND `post_status`=\publish\ ORDER BY post_date DESC LIMIT $how_many); foreach($news as $np){ printf (lia href=\%s\%s/a/li, get_permalink($np-ID),$np-post_title); } ? /ol /pre

In either case, posts will appear in an ordered list with the CSS ID ttwhats-new/tt. With CSS you can change the look of the mini-Loop to suit your own site design.

Using Feedburner?

If you are already using Feedburner to manage your feeds, have a look at their BuzzBoost feature under Publicize.

With just a small piece of javascript you can add your recent posts to any html page. Very customizable as well.

(More on Using_FeedBurner)

Examples

What you do with your static front page is up to you and your imagination. Here are a few examples to help you see the possibilities.

Typical Page

文件:Wpdefault-without-dbcall.jpg
Look of the code on your left

A typical welcome static page may feature a greeting that explains what the site is about and welcomes users to click on various links to enter the site. It could include summaries or links to articles or posts within, changed manually by the site administrator.

Here is an example of a simple, typical page structure:

pre ?php

  define('WP_USE_THEMES', false);
  require('./wp-blog-header.php'); 
  get_header(); 

? div id=content class=narrowcolumn h1Welcome to my site/h1 pHello and welcome to my site. You can go to my web page about how to improve your digital photography techniques with your new digital camera; or you can a title=Go to my Blog href=/wordpress/index.phpgo to my blog./a/p/div

?php get_sidebar(); ? ?php get_footer(); ?/pre

Static Page with Mini-Loop

To incorporate the mini-Loop into your static page review this example of a possible structure:

pre ?php

 define('WP_USE_THEMES', false);
 require('./wp-blog-header.php'); 
 get_header();

? div id=content class=narrowcolumn h1Welcome to my site/h1 pHello and welcome to my site. You can go to my web page about how to improve your shoots with your digital camera; or you can go to my blog/p

h2Latest posts in my blog:/h2

?php

 # Here starts Mini Loop trick: You can use this code, but _at your own risk_
 # If you want to improve this code, you're welcome ;) 
 $how_many=7; //How many posts do you want to show 

? ul class=whats-new ?php

 $news=$wpdb-get_results(SELECT ID,post_title FROM $wpdb-posts
   WHERE `post_type`=\post\ AND `post_status`= \publish\ ORDER BY `post_date` DESC LIMIT $how_many);
 foreach($news as $np){
   printf (lia href=\index.php?p=%s\%s/a/li, $np-ID,$np-post_title);
 }

?

/ul/div ?php

 get_sidebar(); 
 get_footer(); 

? /pre includeonlydiv style=clear:both; background-color:#EEEEFF; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;This article is marked as in need of editing. You can help Codex by editing it./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude