【发布时间】:2020-05-20 09:40:40
【问题描述】:
我一直在使用以下代码将标签和类别用作我的 wordpress 帖子的 META 关键字。
function wcs_add_meta_keywords() {
global $post;
if ( is_single() ) {
$cats = get_the_category( $post->ID );
$tags = get_the_tags( $post->ID );
$keywords = '';
foreach ( $cats as $cat ) {
$keywords .= $cat->cat_name . ", ";
}
foreach ( $tags as $tag ) {
$keywords .= $tag->name . ", ";
}
echo '<meta name="keywords" content="' . $keywords . '" />' . "\n";
}}add_action( 'wp_head', 'wcs_add_meta_keywords' , 2 );
以下代码用于将产品描述用作 META 描述。
function wcs_add_meta_description_tag() {
global $post;
if ( is_single() ) {
$meta = strip_tags( $post->post_content );
$meta = strip_shortcodes( $post->post_content );
$meta = str_replace( array("\n", "\r", "\t"), ' ', $meta );
$meta = mb_substr( $meta, 0, 125, 'utf8' );
echo '<meta name="description" content="' . $meta . '" />' . "\n";
}}add_action( 'wp_head', 'wcs_add_meta_description_tag' , 2 );
但现在我想在 woocommerce 中为我的产品实现相同的目标。我已经了解并知道 woocommerce 使用分类法,所以我尝试使用 get_terms() 和 product_tag,product_cat 代替 get_the_category 和 get_the_tag。但它不起作用。
谁能帮助正确使用这两个代码的变量。
提前致谢
【问题讨论】:
-
请注意,StackOverFlow 中不允许同时提出多个问题,因此您必须删除 META 描述部分并提出一个新问题它。
-
@LoicTheAztec 所以你赢得了答案,甚至是问题。谢谢:-)
标签: php wordpress woocommerce meta taxonomy-terms