2010-08-15 113 views
1

这是我第一次玩过滤器,所以原谅我,如果这似乎基本。我做了一些安静的搜索,并找不到与我的问题有关的任何事情,从我所能找到的一切应该没有问题。我的理解是,手动添加的过滤器可以与任何其他过滤器同时运行,wordpress会自动运行,即:创建我自己的过滤器将与wordpress的默认wpautop和wptexturize过滤器一起运行。WordPress的:自定义the_content过滤器是删除其他过滤器,即:wpautop

但由于某种原因,我添加的过滤器运行良好,除了现在wpautop和wptexturize无法在the_content上运行。从功能中删除我的自定义过滤器后,默认过滤器再次触发。没有安装修改输出的其他插件。如果您发现我的陈述出现问题,任何帮助都将受到超级赞赏,以及任何一般性建议,以便更好地实现此目的。

function tumblr_chat_post($content) { 
global $post; 
$content = $post->post_content; 
if ($post->post_type == 'post') { 
    $postcats = wp_get_object_terms($post->ID, 'category'); 
    foreach ($postcats as $mycat) { 
     if ($mycat->name == "chats") { 
      $chatoutput = "<dl class=\"transcript\">\n"; 
      $split = preg_split("/(\r?\n)+|(<br\s*\/?>\s*)+/", $content); 
       foreach($split as $haystack) { 
        if (strpos($haystack, ":")) { 
         $string = explode(":", trim($haystack), 2); 
         //list($who, $what) = explode(":", $haystack, 2); 
         $who = trim($string[0]); 
         $what = trim($string[1]); 
         $row_class = empty($row_class)? " class=\"alt\"" : ""; 
         $chatoutput = $chatoutput . "<dt$row_class><b>$who:</b></dt> <dd><i>$what</i></dd>\n"; 
        } 
        else { 
        // the string didn't contain a needle so we will keep it and output it later 
         $no_chatoutput = $no_chatoutput . $haystack . "\n"; 
        } 
       } 
       // print our new formated chat post and push unformatted content to the end of the post. 
       $content = $chatoutput . "</dl>\n" . $no_chatoutput; 
       return $content; 
     } 
     else { 
     // I don't exist in the defined category, so no processing is needed 
     return $content; 
     } 
    } 
} 
else { 
    // I'm not a regular post, so no processing is needed. 
    return $content; 
} 
} 
add_filter("the_content", "tumblr_chat_post", 20); 

回答

1

你没有通过已经过筛选,$content到您的过滤功能(因此你失去任何过滤做你达到你的函数之前)。它应该像这样定义:

function tumblr_chat_post($content) { 
    // rest of your logic m'ere 
} 
+0

我明白你的意思了,但我想我仍然缺少一些东西,因为向函数args中添加$ content是没有丝毫区别的。是因为我在函数内从$ post-> post_content提取$内容吗? – Jervis 2010-08-15 19:39:16

+0

是的,是的。手动拉动post_content也会跳过过滤器修改它的任何操作。很高兴知道并感谢您对最初问题的快速回复! – Jervis 2010-08-15 19:59:57