Hướng dẫn thêm các liên kết sau mỗi bài viết
Mở file functions.php trong child theme của bạn (nếu chưa có child theme, bạn nên tạo child theme để tránh mất thay đổi khi update theme). Thêm đoạn code sau:
/*thêm link sau bài viết*/
// Đăng ký metabox
function custom_links_metabox() {
add_meta_box(
'custom_links_metabox',
'Custom Links',
'render_custom_links_metabox',
['post', 'page'], // Áp dụng cho bài viết và trang
'normal',
'default'
);
}
add_action('add_meta_boxes', 'custom_links_metabox');
// Hiển thị metabox
function render_custom_links_metabox($post) {
// Lấy giá trị meta đã lưu
$custom_links = get_post_meta($post->ID, '_custom_links', true);
// Nếu không có dữ liệu, sử dụng dữ liệu mẫu mặc định
$custom_links = is_array($custom_links) ? $custom_links : [
['description' => 'Zalo', 'url' => 'https://zalo.me/0902299161'],
['description' => 'Facebook', 'url' => 'https://facebook.com/webdy.vn'],
['description' => 'YouTube', 'url' => 'https://youtube.com'],
['description' => 'Instagram', 'url' => 'https://instagram.com'],
['description' => 'Twitter', 'url' => 'https://twitter.com']
];
// Nonce để bảo mật
wp_nonce_field('save_custom_links', 'custom_links_nonce');
// Hiển thị các trường nhập liệu
echo '<div id="custom-links-container">';
foreach ($custom_links as $index => $link) {
echo '<div class="custom-link-item" style="margin-bottom: 10px;">';
echo '<input type="text" name="custom_links[' . $index . '][description]" value="' . esc_attr($link['description']) . '" placeholder="Description" style="width: 45%; margin-right: 5%;">';
echo '<input type="url" name="custom_links[' . $index . '][url]" value="' . esc_url($link['url']) . '" placeholder="URL" style="width: 45%;">';
echo '<button type="button" class="remove-link button" style="margin-left: 10px;">Remove</button>';
echo '</div>';
}
echo '</div>';
echo '<button type="button" id="add-link" class="button">Add Link</button>';
// Thêm script JS để xử lý thêm và xóa link
?>
<script>
document.addEventListener('DOMContentLoaded', function () {
const container = document.getElementById('custom-links-container');
const addButton = document.getElementById('add-link');
addButton.addEventListener('click', function () {
const index = container.children.length;
const newField = `
<div class="custom-link-item" style="margin-bottom: 10px;">
<input type="text" name="custom_links[${index}][description]" placeholder="Description" style="width: 45%; margin-right: 5%;">
<input type="url" name="custom_links[${index}][url]" placeholder="URL" style="width: 45%;">
<button type="button" class="remove-link button" style="margin-left: 10px;">Remove</button>
</div>`;
container.insertAdjacentHTML('beforeend', newField);
});
container.addEventListener('click', function (e) {
if (e.target.classList.contains('remove-link')) {
e.target.parentElement.remove();
}
});
});
</script>
<?php
}
// Lưu dữ liệu meta
function save_custom_links($post_id) {
// Kiểm tra bảo mật
if (!isset($_POST['custom_links_nonce']) || !wp_verify_nonce($_POST['custom_links_nonce'], 'save_custom_links')) {
return;
}
// Kiểm tra quyền chỉnh sửa
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Lưu dữ liệu
if (isset($_POST['custom_links']) && is_array($_POST['custom_links'])) {
$cleaned_links = array_map(function ($link) {
return [
'description' => sanitize_text_field($link['description']),
'url' => esc_url_raw($link['url']),
];
}, $_POST['custom_links']);
update_post_meta($post_id, '_custom_links', $cleaned_links);
} else {
delete_post_meta($post_id, '_custom_links');
}
}
add_action('save_post', 'save_custom_links');
// Hiển thị Custom Links dưới bài viết hoặc trang
function display_custom_links_on_frontend($content) {
if (is_single() || is_page()) {
global $post;
$custom_links = get_post_meta($post->ID, '_custom_links', true);
if (!empty($custom_links) && is_array($custom_links)) {
$output = '<div class="custom-links" style="margin-top: 20px; padding: 10px; border-top: 1px solid #ddd;">';
$output .= '<h4> <span class="gradient">Quý khách cần tư vấn thiết kế web giá rẻ, vui lòng nhấn nút bên dưới:</span></h4>';
$output .= '<div class="link-buttons" style="display: flex; flex-wrap: wrap; gap: 10px;">';
foreach ($custom_links as $link) {
$description = esc_html($link['description']);
$url = esc_url($link['url']);
if (!empty($description) && !empty($url)) {
$output .= '<a href="' . $url . '" target="_blank" rel="noopener noreferrer" class="custom-link-button">';
$output .= $description;
$output .= '</a>';
}
}
$output .= '</div>';
$output .= '</div>';
$content .= $output;
}
}
return $content;
}
add_filter('the_content', 'display_custom_links_on_frontend');
/////-----------
Chào ! Bạn thấy nội dung này thế nào?
Xem thêm: