Php 静态首页上按id钩住页面
我想在Wordpress CMS上建立一个一页的网站。我正在寻找一种方法,通过id将我的页面挂在静态frontpage上,因为我有几种页面类型 我试过:Php 静态首页上按id钩住页面,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我想在Wordpress CMS上建立一个一页的网站。我正在寻找一种方法,通过id将我的页面挂在静态frontpage上,因为我有几种页面类型 我试过: $id=6; $post = get_post($id); $content = apply_filters('the_content', $post->post_content); echo $content; 但这只返回内容,而不返回包含HTML的内容。关于如何做到这一点,有什么建议吗?或者用其他方法在Wordpress上
$id=6;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
但这只返回内容,而不返回包含HTML的内容。关于如何做到这一点,有什么建议吗?或者用其他方法在Wordpress上建立一个一页的网站
提前感谢要正确输出HTML,请尝试使用PHP的HTML实体编码/解码相关函数将实体转换回其适用的字符(例如,
)
WordPress有一个名为wpautop
的内部函数,该函数将双线提要(\n
)转换为段落拆分wp_texturize
(或类似名称)将某些字符转换为适当的变体,例如引号和类似的字符
试试这个:
$content = apply_filters( 'the_content', wpautop( wp_texturize( html_entity_decode( $post -> post_content ) ) ) );
// Note: not properly tested, but should probably output properly "HTMLized" contents.
如果您想输出帖子内容中的HTML标记以外的内容(并使用wp admin中的帖子编辑器插入),则必须使用主题模板
编辑:
要将整个模板插入主页以创建单页网站,您需要将每个“页面”创建为模板文件。假设您有front page.php
,content about.php
,content portfolio.php
等等。此外,您很可能有header.php
和footer.php
在简化形式中,将插入到front-end.php
的每个内容模板可以如下所示:
<?php
/**
* content-about.php
*/
// Get the wanted content as WP Post.
$page_obj = get_page_by_title( 'About' );
$page_content = wpautop( $page_obj -> post_content );
?>
<article id="about"> <!-- Begin a new section for the one-page template. -->
<h2><?php echo $page_obj -> post_title; ?></h2>
<?php echo $page_content; ?>
</article>
现在,在WordPress和PHP解析front page.PHP
模板后,结果输出可能如下所示(取决于您在每个包含的模板中插入的内容):
...
关于
你好!这是content-about.php中的一些不错的内容
文件夹
-
...
令人惊叹的!
...
现在,您已经有了包含内容的独立模板,它们被嵌入到front page.php
中,以创建单个页面主模板。如果你愿意的话,你可以使用CSS和JS让它变得华丽
注意:您还可以使用PHP自己的include
或require
函数将子模板插入到首页。PHP
您还可以用来构造自定义查询。您可以使用Post和Page参数调用需要显示在首页上的页面
在循环中,您将能够直接使用模板标记,如内容
。您还可以使用is\u page()
或is\u page\u template()
下面是一个来自编解码器的示例。修改以满足您的需要
$the_query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_content();
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
您是否可以粘贴一些示例内容,您的代码与
echo$内容相呼应代码>?使用主题模板是什么意思?我想这就是我所需要的,因为我开发了所有的模板,但我需要一种方法将它们全部放在一个页面中。你的意思是你想将WPsingle.php
模板“嵌入”到静态主页中吗?不,我正在制作一个单页wordpress网站。对于每个部分,我有一个不同的模板,因为它们有不同的布局。例如,我有一个homepage.php、一个textpage.php和一个portfolio.php。我想将创建的页面包含在FrontPage.php中,以便它们显示为一个单页网站。因此本质上,您只需使用简单的phpinclude
s在FrontPage.php
中插入所需的模板。WordPress有一个名为get\u template\u part
的功能,这可能很有用。我编辑了我的答案,以展示如何将各种其他模板包括到一个中央主页模板中。
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<!-- header.php visible contents here -->
<!-- content-about.php: -->
<article id="about">
<h2>About</h2>
<p>Well hello there! This is some <em>nice</em> content from content-about.php.</p>
</article>
<!-- content-portfolio.php: -->
<article id="portfolio">
<h2>Portfolio</h2>
<ul>
<li>
...
</li>
</ul>
</article>
<!-- awesometemplate.php: -->
<article id="awesome">
<h2>Awesome!</h2>
<table>
...
</table>
</article>
<!-- footer.php visible contents here -->
</body>
</html>
$the_query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_content();
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();