0

我想在 WordPress 中的每个帖子之后插入一些代码...我知道您可以在此之后执行此操作,例如,在 single.php 中

<?php the_content(); ?>

但是,如果我这样做,它会把它放在错误的地方.. 一个示例帖子在这里:http ://www.hardwareblog.com/348/computer-hardware/top-10-gadget-gift-ideas-to-avoid- this-christmas/ -- 如果我把它放在上面的代码示例之后,它将被放在社交和 facebook 链接之后.....我想把它放在那些之前,所以它就在帖子之后。

我做了一些检查和测试..这里的代码来自 post-template.php

function the_content($more_link_text = null, $stripteaser = 0) {
    $content = get_the_content($more_link_text, $stripteaser);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    echo $content;
}

似乎 facebook 和社交代码被插入到 apply_filters() 函数的输出中......虽然我不知道在哪里。

对我正在尝试做的事情有任何帮助吗?

4

1 回答 1

1

以下是内容和功能过滤器的示例:

    function the_content_replacer($content) 
    {
//global $post, $posts;
       //$content = str_replace(']]>', ']]&gt;', $content);

 $content .= "\n<div style=\"display:none;\">text here</div>"; 

//$content = preg_replace('/="http:\/\/cnn/i', 
// '="http://example.com?http://cnn', $content, -1); 
       return $content;
    }
    add_filter('the_content', 'the_content_replacer', 1);
  1. http://wordpress.stackexchange.com上有关此过滤器的更多示例............
  2. 您只需复制并粘贴主题中文件“functions.php”中的内容,它就会起作用。
  3. 如果您运行多站点,您也可以将它放在目录 wp-content/mu-plugins 中,这样它就可以在您的多站点环境中的所有博客上运行。
  4. 第三个参数决定了应用过滤器时的重要性,见:https ://wordpress.stackexchange.com/questions/2126/at-what-priority-does-add-filter-overwrite-core-functions

--> 最好在http://wordpress.stackexchange.com中发布所有 WordPress 问题!!!

--> 如果你使用例如

$content .= "\n<div style=\"display:none;\">text here</div>";

它不会删除结束段落标记(注意字符串开头的换行符)

于 2010-11-26T20:48:03.853 回答