将内容附加到Wordpress中的所有帖子。附加内容未显示在存档中

时间:2018-07-03 18:48:42

标签: php wordpress

我正在尝试在每个Wordpress帖子的末尾添加一个带有过滤器的div。该div将包含每个帖子的简短描述,该描述仅在博客存档中可见。

通过functions.php中的以下内容,我能够将div完美地添加到帖子本身中,但是它不会显示在博客存档中。我应该使用另一个钩子在两个地方都显示(甚至只是显示在档案中而不是单个帖子中)吗?

add_filter ('the_content', 'insertPostContent');

function insertPostContent($content) {
       if(is_single()) {
          $content.= '<div id="archive_description" class="wpb_column grve-column grve-bookmark grve-column-1">';
          $content.= '<p style="margin:0;"><strong>Full Description:</strong>Lorem Ipsum</p>';
          $content.= '<a href="#">Read More</a>';
          $content.= '</div>';
       }
       return $content;
}

1 个答案:

答案 0 :(得分:1)

您的条件仅是将模板文件single.phpis_single()定位,您需要添加is_archive()来定位archive.php模板。修改您的功能。

function insertPostContent($content) {
       if( is_single() || is_archive ) {
          $content.= '<div id="archive_description" class="wpb_column grve-column grve-bookmark grve-column-1">';
          $content.= '<p style="margin:0;"><strong>Full Description:</strong>Lorem Ipsum</p>';
          $content.= '<a href="#">Read More</a>';
          $content.= '</div>';
       }
       return $content;
}