代码之家  ›  专栏  ›  技术社区  ›  Brett

在WordPress的每篇文章后面插入代码

  •  0
  • Brett  · 技术社区  · 14 年前

    我想在WordPress的每一篇文章后插入一些代码。。。我知道你可以在这之后做,例如,在single.php中

    <?php the_content(); ?>
    

    不过,如果我那样做,那就放错地方了。。下面是一个例子: http://www.hardwareblog.com/348/computer-hardware/top-10-gadget-gift-ideas-to-avoid-this-christmas/ --如果我把它放在上面的代码示例之后,它将放在sociable&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&sociable代码似乎被插入到apply_filters()函数的输出中。。。。虽然我不知道在哪里。

    有什么帮助吗?

    1 回复  |  直到 12 年前
        1
  •  1
  •   Community CDub    8 年前

    以下是内容和函数的筛选器示例:

        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. 如果运行multisite,也可以将它放到wp content/mu plugins目录中,这样它就可以在多站点环境中的所有博客上工作。
    4. 第三个参数确定应用筛选器时的重要性,请参见: https://wordpress.stackexchange.com/questions/2126/at-what-priority-does-add-filter-overwrite-core-functions

    -->最好将所有WordPress问题发布到 http://wordpress.stackexchange.com网站 !!!

    -->如果您使用例如。

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

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