2014-08-27 65 views
0

我想在Wordpress CMS上構建一個頁面網站。我正在尋找一種方法,通過id將我的頁面掛在靜態首頁上,因爲我有幾種頁面類型。通過靜態主頁上的id鉤住頁面

我已經試過:

$id=6; 
$post = get_post($id); 
$content = apply_filters('the_content', $post->post_content); 
echo $content; 

但這僅返回內容,而不是與HTML內容。有關如何實現此目的的任何提示?或者其他方式在Wordpress上構建一個單頁網站?

在此先感謝

+0

您能粘貼一些您的代碼與'echo $ content;'回顯的示例內容嗎? – ojrask 2014-08-27 13:05:31

回答

0

要輸出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.phpcontent-about.phpcontent-portfolio.php等等。另外你最有可能有header.phpfooter.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.phpcontent-contact.php等)應遵循類似的結構。

然後在front-page.php中,您需要包含您的header.phpfooter.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自己的includerequire函數將子模板插入front-page.php

+0

與主題模板一起工作意味着什麼?我認爲這是我所需要的,因爲我開發了所有的模板,但我需要一種方法將它們放在一個頁面中。 – luukgruijs 2014-08-27 13:12:52

+0

您是否想要將WP'single.php'模板「嵌入」到靜態主頁中? – ojrask 2014-08-27 13:19:57

+0

不,我正在製作一個單頁的WordPress網站。對於每個部分我有不同的模板,因爲他們有不同的佈局。例如,我有一個homepage.php,一個textpage.php和一個portfolio.php。我想在front-page.php中包含創建的頁面,以便它們顯示爲單頁網站 – luukgruijs 2014-08-27 13:23:46

0

您還可以使用WP_Query來構建自定義查詢。您可以使用「發佈」和「頁面」參數調用您需要在首頁上顯示的頁面。

在循環內部,您可以直接使用模板標籤,如the_content。您還可以使用is_page()is_page_template()is_page_template()

以編解碼器爲例,在每頁基礎上有條件地在循環內加載模板標記和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();