Lấy nội dung các thẻ meta cho head wordpress
Đăng ngày: 04-08-2023 80 lượt xem

1. Title

 function ptl_wp_title( $title, $sep ) {
    global $paged, $page;
    if ( is_feed() )
       return $title;
    $title .= get_bloginfo( ‘name’, ‘display’ );
    $site_description = get_bloginfo( ‘description’, ‘display’ );
    if ( $site_description && ( is_home() || is_front_page() ) )
       $title = “$title $sep $site_description”;
    if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() )
       $title = “$title $sep ” . sprintf( __( ‘Trang %s’, ‘devvn’ ), max( $paged, $page ) );
    return $title;
 }
 add_filter( ‘wp_title’, ‘ptl_wp_title’, 10, 2 );

2. keyword

 function wp_keyword($keyword){
   $posttags = get_the_tags();
           if ($posttags) {
           foreach($posttags as $tag) {
               $keyword.= $tag->name . ‘, ‘;
           }
   }
return $keyword;
}

3. image

 function ptl_wp_image($img_src){
   global $post;
   if(is_single()) {
       if(has_post_thumbnail($post->ID)) {
       $img_src = get_the_post_thumbnail_url(get_the_ID(),’full’);
       }
   }
   if(is_category()){
       $category = get_category( get_query_var( ‘cat’ ) );
       $cat_id = $category->cat_ID;
       // Get the image ID for the category
       $image_id = get_term_meta ( $cat_id, ‘category-image-id’, true );
       // Echo the image
       $img_src =  wp_get_attachment_url( $image_id );
   }
   if(is_page()){
       $img_src =  wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
   }
   if(empty($img_src)){
       $img_src =  get_stylesheet_directory_uri() .”/assets/images/banner.jpg”;
   }
   return $img_src;
}
add_filter(”,’ptl_wp_image’);
// end get image