2010-11-03 84 views
22

如何在一個頁面中包含一個或多個頁面的頁面內容?WordPresspress:在另一個頁面中包含一頁的內容

ex。我有pageA的頁面BpageC,我想包括在pageX屬性

這些網頁的內容是有加載的職位WordPress的功能指定的頁面/文章?
like show_post(「pageA」)??

+0

最簡單的方法可能是[這個插件(http://wordpress.org/plugins/insert-pages/) – Chris 2014-08-20 16:36:03

回答

42

@dany

沒有一個show_post()功能本身在WordPress的核心,但它是非常容易寫:

function show_post($path) { 
    $post = get_page_by_path($path); 
    $content = apply_filters('the_content', $post->post_content); 
    echo $content; 
} 

請注意,這將與頁面的路徑來調用,即:

<?php show_post('about'); // Shows the content of the "About" page. ?> 
<?php show_post('products/widget1'); // Shows content of the "Products > Widget" page. ?> 

當然,我可能不會像一般性地命名爲show_post()的情況下WordPress的核心在未來添加一個相同名稱的功能。你的選擇。

而且,也沒有輕微意味着@kevtrout因爲我知道他是非常好的,可以考慮在計算器上的姊妹網站WordPress Answers在未來發布你的WordPress的問題。 WordPress愛好者在那裏回答問題的比例要高得多。

希望這會有所幫助。

-Mike

+0

哇,正是我想要的。非常感謝Mike!並感謝您的鏈接。 – Zebra 2010-11-04 17:25:01

+1

@dany - 不客氣。下一次來到WordPress的答案,你會看到更多像這樣...... :) – MikeSchinkel 2010-11-04 20:19:47

+0

有沒有辦法做到這一點與個別項目? – sat 2011-05-13 13:50:17

1
<?php query_posts('p=43'); 

global $more; 
//set $more to 0 in order to only get the first part of the post 
$more = 0; 

    // the Loop 
    while (have_posts()) : the_post(); 

    // the content of the post ?> 
    the_title(); 
    the_content(); 

    endwhile; ?> 

這顯然是帖子的一部分,我從wordpress codex中得到了詳細信息。

+0

你得到排序? – Stuart 2010-11-04 20:19:51

0

有趣......我環顧四周,瞭解如何嵌入WordPress的內容別處(如,在其他網站上),並發現了一些東西......

  • WWW。 shooflydesign.org/buzz/past/embedding_wordpress。 html
    • 顯示如何使用php腳本在另一個站點中嵌入Wordpress內容;也許只是使用相同的東西來嵌入Wordpress到自己?
  • www。 corvidworks。 com/articles/wordpress-content-on-other-pages
    • 類似的概念;在另一頁上嵌入Wordpress;只是在嘗試一種新的WP後
  • 使用該工具自己進
    • 我的搜索拉昇一些建議,可以只使用一個進到自己的博客嵌入到一個職位。沒有什麼可以阻止這一點。您可能希望它自動執行,因此重新構建Feed看起來可能會有問題,但根據您想要執行的操作,值得一試。

希望那些是有幫助的。雖然他們都是解決方案讓您的WP內容在某些地方其他比WP ...他們可能會爲您的問題工作,並允許您顯示A,B和C在X.

+0

謝謝,我試着你的建議 – Zebra 2010-11-03 19:30:53

2

頁面只是發佈,在數據庫中使用post_type爲'page'。您可以在您的pageX模板中寫入一個帖子查詢來顯示其他頁面上的多個頁面的內容,以獲取您指定的帖子並將它們輸出到一個循環中。

有三種方法可以從數據庫中獲取帖子內容:

  1. get_posts
  2. query_posts
  3. WP_Query

這些鏈接都指向WordPress的法典。 Get_posts和query_posts有一個可用參數'page_id',您可以在其中指定要檢索和顯示的頁面的ID。

+0

@ ketrout:謝謝你的鏈接 – Zebra 2010-11-04 17:25:57

0

有顯示特定頁面的另一個使用query_posts裏面的內容(WordPress的功能),它是:

<?php query_posts("posts_per_page=1&post_type=page&post_id=134"); the_post(); ?> 

您設定的頁數顯示給1 ,帖子類型是頁面而不是帖子,頁面ID爲

+0

query_posts不允許調用'post_id',而是必須使用'p'。現在的代碼會工作,但它會返回最近創建的頁面。下面的固定代碼將返回指定的'post_id'。 '<?php query_posts(「posts_per_page = 1&post_type = page&p = 134」); the_post(); ?>' – nuclearsugar 2014-05-05 21:03:00

2

您可以安裝插件"Improved Include Page"。一旦安裝,您創建頁X,然後輸入:

[include-page id="123"] 
[include-page id="124"] 
[include-page id="125"] 

其中這些頁面A,B和C分別

1

我發現this answer張貼在WordPress的論壇ID的。你添加一些代碼給functions.php,然後只要你喜歡就使用簡碼。

function get_post_page_content($atts) { 
     extract(shortcode_atts(array(
      'id' => null, 
      'title' => false, 
     ), $atts)); 

     $the_query = new WP_Query('page_id='.$id); 
     while ($the_query->have_posts()) { 
      $the_query->the_post(); 
       if($title == true){ 
       the_title(); 
       } 
       the_content(); 
     } 
     wp_reset_postdata(); 

    } 
    add_shortcode('my_content', 'get_post_page_content'); 

對於短碼,

[my_content id="Enter your page id number" title=Set this to true if you want to show title /] 
相關問題