如何在wordpress中使用插件过滤主题中加载的帖子?

我想制作插件来编辑帖子,并在他们实时获得时向他们的标题添加一些东西。喜欢主页上的帖子。我只想对帖子属性进行一些更改。我已经找到了一个关于这个问题的解决方案,但我不知道如何让它工作。链接到解决方案。谢谢


偶然的你
浏览 91回答 1
1回答

牧羊人nacy

我认为你可以使用WordPress钩子,这就是我的做法。在添加以下代码中:functions.php&nbsp;<?php&nbsp;&nbsp; &nbsp; &nbsp; function populate_posts_data( $posts, $query ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;global $wpdb;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( !count( $posts ) )&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $posts;&nbsp; // posts array is empty send it back with thanks.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while ( $posts as $post ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// do whatever you want for each post, alter its data, etc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$post->title = $post->title . ' Edited');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $posts;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;add_filter( 'the_posts', 'populate_posts_data' );&nbsp;?>谢谢,希望它能帮助你。
打开App,查看更多内容
随时随地看视频慕课网APP