Code tự cập nhật tháng hiện tại lên meta title bài viết

Code tự cập nhật tháng hiện tại lên meta title bài viết, sang tháng mới tự đổi luôn, rất tiện cho các bạn viết bài cập nhật theo tháng như mã giảm giá, giá vé, tổng hợp giá sản phẩm… Có 2 code dành cho các bạn dùng Rankmath và Yoastseo, bạn nào ko dùng gì thì cài 1 trong 2 thằng kia rồi dùng nhé 😀

Code dành cho Rankmath

Mặc định rankmath có hỗ trợ cái này rồi, chỉ cần thêm %currentmonth% vào là được, thế nhưng nó lại hiện là tháng tám, nhìn cứ tù tù

Để đổi lại thành sạng số nhìn cho nó chuyên nghiệp thì chèn code sau vào function nhé!

add_action( 'rank_math/vars/register_extra_replacements', 'register_custom_rep' );
function register_custom_rep() {
RankMath\Helper::register_var_replacement(
'currentmonth_number','get_currentmonth_number',
array(
'name' => esc_html__( 'Current Month', 'rank-math' ),
'desc' => esc_html__( 'Current server month', 'rank-math' ),
'example' => current_time( 'F' ),
)
);}
function get_currentmonth_number() {
if ( ! isset( $replacement ) ) {
$replacement = date_i18n( 'm' );
}
return $replacement;
}

Sau đó thay vì dùng %currentmonth% ở title, ta sẽ dùng %currentmonth_number% . Chèn xong có thể trong backend nó ko đổi, bạn cứ cập nhật bài viết bình thường, ngoài frontend nó sẽ đổi 😀

Ah muốn thêm ngày thì dùng %currentday%, thêm năm thì dùng %currentyear% nhé, thêm ngày/tháng/năm luôn thì %currentdate%

Code dành cho Yoastseo

Với Yoast, bạn chèn code sau vào function

add_filter( 'the_title', 'do_shortcode' );
add_filter( 'single_post_title', 'do_shortcode' );
add_filter( 'wpseo_title', 'do_shortcode' );
add_filter( 'wpseo_metadesc', 'do_shortcode' );
add_shortcode( 'year' , 'get_year' );
function get_year() {
$year = date("Y");
return "$year";
}
add_shortcode( 'month' , 'get_month' );
function get_month() {
$month = date("m");
return "$month";
}
add_shortcode( 'date', 'lh_footer_date' );
function lh_footer_date() {
ob_start();
echo date('d');
return ob_get_clean();
}

Sau đó dùng shortcode [month] hoặc [year], [date] là đc

Bài viết cùng chủ đề:
guest
0 Bình luận
Inline Feedbacks
View all comments