一个简单的WordPress页面代码示例

admin123 10月前 413


当提到编写WordPress页面代码时,有许多不同的元素和功能可以包含在页面中。以下是一个简单的WordPress页面代码示例,包含一个标题、段落和图像:


<?php
/**
 * Template Name: Custom Page Template
 */
get_header(); ?>
<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php while (have_posts()) : the_post(); ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <header class="entry-header">
                    <h1 class="entry-title"><?php the_title(); ?></h1>
                </header>
                <div class="entry-content">
                    <?php the_content(); ?>
                </div>
                
                <div class="featured-image">
                    <?php if (has_post_thumbnail()) {
                        the_post_thumbnail();
                    } ?>
                </div>
            </article>
        <?php endwhile; ?>
    </main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>


在这个示例中,我们创建了一个自定义页面模板,以便在WordPress中使用。代码开始于`get_header()`,用于引入网站的头部文件。然后,在`<div id="primary" class="content-area">`中定义主要内容区域。在`<main id="main" class="site-main" role="main">`中,我们开始循环输出页面内容。

在循环中,我们使用了`the_title()`函数来输出页面的标题,使用`the_content()`函数输出页面的内容。接下来,我们添加了一个可选的特色图片部分,使用`has_post_thumbnail()`函数来检查是否有设置特色图片,并使用`the_post_thumbnail()`函数来显示特色图片。

最后,在代码的结尾,我们使用`get_footer()`引入网站的页脚文件。

请注意,此只是一个基本示例,您可以根据需要进行自定义和扩展。


最新回复 (0)
全部楼主
返回