我需要php代码为我的博客创建一个静态首页。如何在WordPress中使用php?
如何在WordPress中为我的博客创建静态首页
根据@RayMitchel的回答,本教程将向您展示如何将博客中的特定页面设置为首页。
假设您使用的是TwentyEleven主题,那么您需要了解页面模板。WordPress网站上有一篇文章:http://codex.wordpress.org/Pages#Page_Templates.
因此,您可以通过复制主页开始创建页面模板。php文件并将其重命名为我的页面。php并将以下内容放在页面顶部:
<?php
/*
Template Name: Snarfer
*/
?>
保存文件后,您可以在任何页面中切换页面模板,并在下拉列表中选择“Snarfer”。现在头版的正确名称是FrontPage。php。如果这样调用文件,则设置为首页的页面将自动使用首页。php文件,无需设置页面模板。
一旦您将文件命名为头版,就更进一步了。php您不必费心将特定页面设置为首页作为首页。php文件将被调用,而不是主文件。php(标准主页)。下面您可以看到原因,这是由于WordPress Template Hierarchy 头版。php文件位于主页上方。php文件,如果您让它在首页上显示帖子(默认设置):
仅使用静态页面作为网站首页,不必进行任何模板或其他编程更改:
创建一个静态页面,给它任意名称(例如“首页”,但它可以是任何内容),并向其中添加您需要/想要的任何内容。如果您需要显示博客帖子索引,请创建第二个静态页面,再次给它任意名称(例如“博客”,但它可以是*任何内容)。无需向此页面添加任何内容Dashboard -> Settings -> Reading
在主题目录中,您可以添加或修改首页。php文件和您想要的任何代码。
这是针对您正在开发的主题,还是使用现有主题?引用自WordPress codex “创建虚拟静态首页不需要编辑或编码文件或模板。”在大多数情况下,您可以通过更改管理区域中的设置来获得静态首页。这是该部分的链接http://codex.wordpress.org/Creating_a_Static_Front_Page
你的问题有几个正确答案。这取决于您希望主页的自定义程度。我所做的就是创建一个家。php文件,并从我的页面中包含的内容开始。php在我的主题中。然后,我按照自己的方式进行定制。如果主题有一个家。php文件,它将其用于您的主页。
对于我的大多数网站,我喜欢从某种滑块开始,然后有三个“动作”框,其中包括照片、标题、链接和一些文本。我通过自定义帖子类型来实现这一点。有时我可能想在一个小框中列出我的一些帖子,有时我想要一个侧边栏,其他的则不需要。
关键是我可以通过定制这个主页来定制我的主页。php。我会给你一个标准的家。下面是php文件,您可以对其进行剖析,看看我是如何使用它的。
<?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(); ?>
为此,您必须在wordpress中创建一个称为页面模板的PHP文件。页面模板始终以以下内容开头:
<?php
/*
Template Name:Your Home Page Template Name
*/
?>
之后,您必须在页面模板中编写所有其他代码。创建页面模板文件后,创建一个页面并在此框中打开“页面属性”框一个下拉列表,其中包含名称模板并选择页面模板,然后发布页面。
要创建静态首页,请阅读以下内容
http://codesmeister.blogspot.in/2011/10/how-to-make-homepage-front-page-in.html