要輸出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標記其他(和使用可溼性粉劑管理員帖子編輯器插入),你必須要繼續與主題模板。
編輯:
要插入整個模板到首頁,創建一個單頁的網站,你需要創建的每個「頁」爲模板文件。假設您有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>
其他模板(content-portfolio.php
,content-contact.php
等)應遵循類似的結構。
然後在front-page.php
中,您需要包含您的header.php
和footer.php
,正如您通常那樣。然後,插圖中使用get_template_part
調用來獲取和內front-page.php
顯示content-abc.php
模板:
<?php
/**
* front-page.php
*/
get_header(); ?>
<!-- Add some markup if you want to. -->
<?php get_template_part('content', 'about'); // 'content-about.php' ?>
<?php get_template_part('content', 'portfolio'); // 'content-portfolio.php' ?>
<?php get_template_part('awesometemplate'); // 'awesometemplate.php' ?>
<!-- Add some markup if you want to. -->
<?php get_footer();
現在,經過WordPress和PHP解析front-page.php
模板,生成的輸出可能是這樣(取決於你插入到每個東西包括模板) :
<!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>
持有的內容和現在你已經走散了模板嵌入front-page.php
創建一個單頁的主模板。如果你願意的話,你可以使用CSS和JS來使它變得華麗。
注意:您也可以使用PHP自己的include
或require
函數將子模板插入front-page.php
。
您能粘貼一些您的代碼與'echo $ content;'回顯的示例內容嗎? – ojrask 2014-08-27 13:05:31