درود
برای ایجاد متاباکس و تمرین بر روی یک پست از قطعه زیر در مسیر function.php استفاده کردم. قطعه کد شامل:
// Add the metabox to the post editor screen
function add_video_metabox() {
add_meta_box(
'video_metabox',
'Video Metabox',
'render_video_metabox',
'post',
'normal',
'high'
);
}
add_action('add_meta_boxes', 'add_video_metabox');
// Render the content of the metabox
function render_video_metabox($post) {
// Retrieve the current value of the video URL
$video_url = get_post_meta($post->ID, '_video_url', true);
// Output the HTML for the metabox
?>
<label for="video_url">Video URL:</label>
<input type="text" id="video_url" name="video_url" value="<?php echo esc_attr($video_url); ?>" style="width: 100%;" />
<?php
}
// Save the metabox data
function save_video_metabox($post_id) {
// Check if nonce is set
if (!isset($_POST['video_metabox_nonce'])) {
return $post_id;
}
// Verify that the nonce is valid
if (!wp_verify_nonce($_POST['video_metabox_nonce'], 'save_video_metabox')) {
return $post_id;
}
// Check if this is an autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// Check the user's permissions
if ('post' === $_POST['post_type'] && !current_user_can('edit_post', $post_id)) {
return $post_id;
}
// Update the meta field in the database
if (isset($_POST['video_url'])) {
update_post_meta($post_id, '_video_url', sanitize_text_field($_POST['video_url']));
}
}
add_action('save_post', 'save_video_metabox');
فیلد متاباکس با موفقیت ساخته و عملیات را انجام میدهد. برای تست لینک یوتیوب را وارد کردم. اما ویدئو نمایش داده نمیشود.
در ادامه و با جستجو بنظر میرسید باید این قطعه را به فایل single.php اضافه کنم:
// Inside the appropriate template file (e.g., single.php or content.php)
// Start of the loop
while (have_posts()) : the_post();
// ... your existing code for post content ...
// Get the video URL for the current post
$video_url = get_post_meta(get_the_ID(), '_video_url', true);
// Check if a video URL is available
if (!empty($video_url)) {
// Output the video embed code (adjust the embed code based on your needs)
echo '<div class="video-container">';
echo wp_oembed_get($video_url);
echo '</div>';
}
// ... your existing code for post content ...
endwhile;
// End of the loop
متاسفانه همچنان با وضعیت عدم نمایش روبرو هستم.
آیا امکان دارد بفرمایید کجای مسیر را به اشتباه رفتهام؟