2

这可能是一个奇怪的问题。当我添加 Facebook Like Button 和 Gigpress 等插件时,它们提供了在每篇单页博客文章之前或之后插入内容的选项。例如,我将 Gigpress 和 FB Like 按钮都设置为在我的帖子中的文本下方添加内容,尽管它并不完美,但这是有效的。喜欢按钮显示在帖子文本下方。

那么这在后端是如何完成的呢?看起来模板或其他 php 文件没有被插件更改,但似乎也没有任何明显的 php 代码可以提取数据。这种类型的功能是否以某种方式内置于“框架”中?

我问的原因是出于格式原因......两个插件添加的内容冲突并且看起来很糟糕。我试图弄清楚如何修改css。

谢谢

4

1 回答 1

7

他们通过过滤器动作和挂钩来实现它。

在您的情况下-使用the_content过滤器..

示例(来自法典):

add_filter( 'the_content', 'my_the_content_filter', 20 );
/**
 * Add a icon to the beginning of every post page.
 *
 * @uses is_single()
 */
function my_the_content_filter( $content ) {

    if ( is_single() )
        // Add image to the beginning of each page
        $content = sprintf(
            '<img class="post-icon" src="%s/images/post_icon.png" alt="Post icon" title=""/>%s',
            get_bloginfo( 'stylesheet_directory' ),
            $content
        );

    // Returns the content.
    return $content;
}

一个更容易理解的例子:

 add_filter( 'the_content', 'add_something_to_content_filter', 20 );


 function add_something_to_content_filter( $content ) {

            $original_content = $content ; // preserve the original ...
            $add_before_content =  ' This will be added before the content.. ' ;
            $add_after_content =  ' This will be added after the content.. ' ;
            $content = $add_before_content . $original_content  . $add_after_content ;

        // Returns the content.
        return $content;
    }

要查看此示例的实际效果,请将其放入您的 functions.php 中

这实际上是理解 wordpress 和开始编写插件的最重要的一步。如果您真的有兴趣,请阅读上面的链接。

另外,打开您刚才提到的插件文件并查找 过滤器操作...

于 2013-10-05T18:59:51.890 回答