我們知道wordpress有一個the_tags函數可以獲取到文章設置的所有標簽,並按照你想要的形式輸出。在文章頁面輸出標簽有助於內鏈布局,提升SEO效果。在模板中顯示標簽名並鏈接到該標簽中,如果當前頁中無標簽就不顯示,這個函數必須使用在WordPress主循環中。就是能獲取到全局變量post的地方,一般用於文章頁與文章列表頁。
the_tags函數位於wp-includes/category-template.php文件中:
/** * Retrieve the tags for a post. * * @since 2.3.0 * * @param string $before Optional. Before list. * @param string $sep Optional. Separate items using this. * @param string $after Optional. After list. */ function the_tags( $before = null, $sep = ', ', $after = '' ) { if ( null === $before ) $before = __('Tags: '); $the_tags = get_the_tag_list( $before, $sep, $after ); if ( ! is_wp_error( $the_tags ) ) { echo $the_tags; } }
可以看到the_tags函數是通過調用get_the_tag_list取得數據。
函數使用方法
<?php the_tags( $before, $sep, $after ); ?> //$before //在顯示之前輸出的內容,一般是標簽鏈接所處容器HTML標簽。 //$sep //用來分隔的內容,你可以為空,具體效果看下面的圖。 //$after //顯示在標簽之后的內容,一般是標簽鏈接所處容器HTML標簽。
使用示例
默認方法
<?php the_tags(); ?>
等同於:<?php the_tags( 'Tags: ', ', ', '' ); ?>
得到:Tags:XXX, XXXX
再來一個
<?php the_tags( '<ul><li>', '</li><li>', '</li></ul>' ); ?>