2010-08-30 52 views
4

我已經嘗試了這麼多的PHP組合,以獲得wordpress輸出$ post-> post_content作爲格式化文本(而不是echo $post->post_content給我的原始格式。這種組合似乎是最有前途的,但它不是?輸出任何東西任何想法爲什麼不是apply_filter('the_content')輸出任何內容?

(這是這一行:<?php $content = apply_filters('the_content', $s->post_content); ?>

<?php query_posts('orderby=menu_order&order=asc&posts_per_page=-1&post_type=page&post_parent='.$post->ID); if(have_posts()) { while(have_posts()) { the_post(); ?> 
    <div class="page"> 
     <?php 
      global $wpdb; 
      $subs = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent='$post->ID' AND post_type='page' AND post_status='publish'"); 
      if($subs) { 
     ?> 
     <div class="navi"></div> 
     <a class="naviNext"><img src="<?php bloginfo('template_url'); ?>/images/navi-next.png" alt="" /></a> 
     <div class="scrollable"> 
      <div class="items"> 
       <?php foreach($subs as $s) { ?> 
       <div class="item"> 
        <h2><?php echo $s->post_title; ?></h2> 
        <?php $content = apply_filters('the_content', $s->post_content); echo $content; ?> 
       </div> 
       <?php } ?> 
      </div> 
     </div> 
     <?php } else { the_content(); } ?> 
    </div> 
    <?php } } wp_reset_query(); ?> 
+0

不知道爲什麼你沒有得到任何輸出,EXACT相同的代碼在我的WP安裝上工作正常。 – shamittomar 2010-09-10 12:50:22

回答

0

您需要echoapply_filters調用的結果:

<?php echo apply_filters('the_content', $s->post_content); ?> 

或者,當你知道了編碼:

<?php 
    $content = apply_filters('the_content', $s->post_content); 
    echo $content; 
?> 
+0

是的...我早些時候嘗試過 - 仍然不輸出任何東西! – 2010-08-30 18:32:32

+0

廢話!沒有注意到你在原來的例子中已經這麼做了。如果你只是'echo $ s-> post_content',你會得到原始格式的帖子嗎? – Pat 2010-08-30 19:32:52

+0

yes- echo $ s-> post_content給了我原始的格式,但回顯過濾器沒有。 – 2010-08-30 19:43:20

0

很抱歉,如果這是太基本的,但如果你呼應內容可能會有所幫助:

<?php 
$content = apply_filters('the_content', $s->post_content); 
echo $content; 
?> 
0

你是如何加入過濾器?你可以使用add_filter來指定一個可以接收$ content的函數。您可以通過此功能進行任何您需要的過濾。

http://codex.wordpress.org/Plugin_API#Create_a_Filter_Function

+0

這看起來像是朝着正確方向邁出的一步......我不是在任何地方添加過濾器,只是應用它。我將如何添加它在這種情況下?謝謝! – 2010-09-03 16:07:02

+0

你應該可以通過你的主題的functions.php文件來添加它 – 2010-09-04 22:32:20

+0

我只是回頭看你原來的帖子,你也可能在你內部使用setup_postdata($ s)作循環並使用the_content()。 你可以看到這裏使用 - http://codex.wordpress.org/Template_Tags/get_posts#Posts_list_with_offset – 2010-09-04 22:59:30

0

嗯..出於某種原因,我可以當我刪除的頂線和底線獲取內容顯示。

也許問題是query_posts調用..而不是apply_filters()調用。

我可以根據是否使用apply_filters()來切換顯示模式。我相信你就是這樣。

+0

只是做<?php echo $ s-> post_content; ?>給了我無格式的內容(p標籤和所有格式都被剝離了),所以我嘗試使用「the_content_ filter來獲取富文本。 – 2010-09-03 21:12:23

+0

對不起,我不清楚,我刪除了對query_posts()的調用。這就解決了我的問題,之後我就可以使用the_content_filter和期望的結果,這就是爲什麼我想看看是否可以通過修改或刪除對query_posts()的調用來獲得相同的結果 – kenzaraque 2010-09-03 21:23:04

+0

我是最初的認爲query_posts可能是問題,如果刪除它確實解決了問題,那麼我認爲最可能的解釋是$ post全局已經填充了「其他」數據,其中一個過濾器是從全局讀取內容變量。WordPress似乎做得很好,所以它可能是一個寫得不好的插件/ functions.php文件的錯誤。從數據庫讀取總是會導致問題。使用Wordpress的API - 它更容易,它會確保必要的全局變量被填充和必要的過濾器/操作被觸發。 – Brendon 2010-09-05 16:32:22

9

據我所知,將主'格式'應用於內容主體的功能是wpautop()。該功能應該通過wordpress掛鉤到'the_content'中。這個函數確實做了令人討厭的事情(比如弄髒嵌入代碼),並且有很多插件會從過濾器堆棧中解開它。嘗試更換您的線路:

<?php $content = apply_filters('the_content', $s->post_content); echo $content; ?> 

<?php $content = wpautop($s->post_content); echo $content; ?> 

是否有幫助,那麼你可能有wpautop的問題取得了一些脫鉤。

1

man86,

我看你是通過$ wpdb-> get_results獲得這個職位數據()。關於它的事情是,數據返回原始的,所以你需要「做準備」,你可以用普通崗位的功能,如the_content()(將返回已格式化的內容,就像你喜歡它之前)。

如何嘗試這個(見代碼註釋):

<?php query_posts('orderby=menu_order&order=asc&posts_per_page=-1&post_type=page&post_parent='.$post->ID); 

if(have_posts()) { while(have_posts()) { the_post(); ?> 
<div class="page"> 
    <?php 
     global $wpdb; 
     $subs = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent='$post->ID' AND post_type='page' AND post_status='publish'"); 
     if($subs) { ?> 
    <div class="navi"></div> 
    <a class="naviNext"><img src="<?php bloginfo('template_url'); ?>/images/navi-next.png" alt="" /></a> 
    <div class="scrollable"> 
     <div class="items"> 
      <?php foreach($subs as $post) { // <-- changed $s to $post 
      setup_postdata($post) // <--use setup_postdata to prepare post 
      ?> 
      <div class="item"> 
       <h2><?php the_title(); // <-- use "the_title() now that the data has been prepared ?></h2> 
       <?php the_content(); // <-- use "the_content() now that the data has been prepared ?> 
      </div> 
      <?php } ?> 
     </div> 
    </div> 
    <?php } else { the_content(); } ?> 
</div> 
<?php } } wp_reset_query(); ?> 

參考:http://codex.wordpress.org/Class_Reference/wpdb#Examples_5( 「由用戶5坐上草稿的所有信息」)

謝謝,我希望幫助!

Vq。

2

我有同樣的問題。原來在我的主題中有一個函數,它也過濾了the content,但是有一個bug,導致過濾器返回一個空字符串。

因此,檢查您的主題和插件功能,篩選the_content。在崇高的文本2例如,你可以做一個快速的「查找文件」與⌘/CTRL + + ˚F找到可能的罪犯。

相關問題