Tối ưu hóa Bài Viết WordPress để Chèn Nội Dung Một Lần sau Khi Xuất Bản

Bạn có thể tận dụng các hàm hook của WordPress để tự động chèn nội dung quảng bá sau khi bài viết được xuất bản. Dưới đây là một đoạn mã PHP có thể giúp bạn thực hiện điều này.

Mở file functions.php của theme hoặc sử dụng một plugin quản lý code để thêm đoạn mã sau:

[php]function insert_content_once_after_publish($post_id, $post, $update) {
if ($post->post_type == ‘post’ && $post->post_status == ‘publish’ && !$update) {
$additional_content = ‘Nội dung bạn muốn thêm vào đây’;

$content = $post->post_content;

$new_content = $content . $additional_content;

wp_update_post(array(‘ID’ => $post_id, ‘post_content’ => $new_content));

remove_action(‘save_post’, ‘insert_content_once_after_publish’);
}
}

add_action(‘save_post’, ‘insert_content_once_after_publish’, 10, 3);
[/php]

Với đoạn mã trên, bạn có thể tự động chèn nội dung sau khi bài viết được xuất bản, giúp tối ưu hóa nội dung và cải thiện SEO cho trang web WordPress của bạn.

Leave a Reply

Your email address will not be published.Required fields are marked *