I need the php code to create a static front page for my blog. How do I do it with php in WordPress?
From @Ray Mitchel's answer the tutorial shows you how to set a specific page in your blog to be the front page.
Assuming you're using the TwentyEleven theme, you need to learn about page templates. The WordPress site has an article: http://codex.wordpress.org/Pages#Page_Templates.
So you can create a page template by starting by copying the home.php file and renaming it to my-page.php and put the following at the top of the page:
<?php /* Template Name: Snarfer */ ?>
Once you have saved the file, in any page you can switch the page template, and in the drop down you should now be able to select 'Snarfer'.
Now the correct name for the front page is front-page.php. If you call your file that, the page that you have set to be the front page will automatically use the front-page.php file without you having to set the page template.
Even further to this once you've named your file front-page.php you don't have to bother setting a specific page to be the front page as the front-page.php file will get called instead of home.php (the standard home page). Below you can see why, which is thanks to the WordPress Template Hierarchy the front-page.php file comes above the home.php file if you leave it to display posts on the front page (which is the default):
You don't have to make any template or other programmatic changes just to use a static Page as your site front page:
- Create a static Page, give it any arbitrary name (e.g. "Front Page", but it can be anything), and add whatever content to it that you need/want
- If you need to display a blog posts index, create a second static Page, again give it any arbitrary name (e.g. "Blog", but it can be *anything). No need to add any content to this page.
- Go to
Dashboard -> Settings -> Reading
- Change "Front page displays" from "your latest posts" to "a static page"
- Using the "Front Page" select dropdown, select the static Page you created in Step 1
- If you need to display a blog posts index, using the "Posts page" select dropdown, select the static page you created in Step 2
That's all you need to do. Your site now displays a static Page as the front page.
Is this for a theme you are developing, or are you using an existing theme? Quoting from the WordPress codex "Creating a virtual static front page does not require editing or coding of files or templates." In most cases, you can have a static front page by changing the settings in the administration area. Here is the link to that section http://codex.wordpress.org/Creating_a_Static_Front_Page
There are a few right answers to your question. It just depends on how custom you want that home page. What I do is create a home.php file and start with what is contained in my page.php in my theme. Then, I customize it the way I want. If the theme has a home.php file, it uses it for your home page.
For most of my sites I like to start with a slider of some kind, then have maybe three "action" boxes with a photo, headline, link and some text. I accomplish this with a custom post type. Some times I may want to list some of my posts in a small box, sometimes I would like a sidebar, others not.
The point is that I can customize my home page just by customizing this home.php. I will give you one of my standard home.php files below and you can dissect it to see how I use it.
<?php get_header(); ?> <!--start of home.php --> <div id="feature" class="clearfix"> <div id="feature-center" class="clearfix"> <!-- The shortcode for the slider I am using --> <?php echo do_shortcode('[royalslider id="2"]'); ?> </div> </div> <div id="page-wrap"><!-- FEATURE SLIDER AREA --> <div id="full-content-top"></div> <!-- end of content-top --> <div id="full-content-center" class="clearfix"> <!-- MAIN FOCUS BOXES --> <div id="main-focus-boxes" class="clearfix"> <ul id="post-columns" class="group"> <?php $recent = new WP_Query("post_type=homebox&showposts=3"); while($recent->have_posts()) : $recent->the_post();?> <li class="one-third clearfix"> <div class="boxtitle"><h3><?php the_title(''); ?></h3></div> <div class="boximage"><a href="<?php echo get_post_meta($post->ID, 'ecpt_link', $single=true) ?>" ><img align="middle" src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo get_post_meta($post->ID, 'ecpt_boxphoto', $single = true); ?>&w=230&h=150" alt="<?php echo get_post_meta($post->ID, 'ecpt_titleofbox', $single=true) ?>" /></a></div> <div class="boxtext"><p><?php echo get_post_meta($post->ID, 'ecpt_boxtext', $single=true) ?></p></div> <div class="boxlink"><a class="more-link" href="<?php echo get_post_meta($post->ID, 'ecpt_link', $single=true) ?>" ><?php echo get_post_meta($post->ID, 'ecpt_linklabel', $single=true); ?></a></div> </li> <?php endwhile; ?> </ul> </div> <!-- end of Main focus boxes --> <!-- Start of the home page welcome content --> <?php $recent = new WP_Query("post_type=welcome"); while($recent->have_posts()) : $recent->the_post();?> <h1><?php the_title(); ?></h1> <?php the_content(''); ?> <!-- end of post excerpt --> <?php endwhile; ?> </div> <!-- end of content-center --> <div id="full-content-bottom"></div> <!-- end of content-bottom --><!-- end of page-content --> </div> <?php get_footer(); ?>
For that you have to create one PHP file is known as page template in wordpress. page template always start with :
<?php /* Template Name:Your Home Page Template Name */ ?>
After that you have to write all other code in page template.
after creating page template file, create one page and switch on page attributes box in this box one drop down list with the name template and select your page template after that publish your page.
for create a static front page read this