我想知道这样做是否可行。我有Slider 自定义帖子类型和Services 自定义帖子类型。
对于滑块帖子,我已经为URL值创建了一个元框,以显示在所有滑块帖子旁边。我现在想做的是从Services custom post type, 并将它们显示在这个滑块post metabox中作为选择输入,这样我就可以选择链接应该指向哪个帖子。
提前感谢:)
以下是当前代码:
function rm_display_slider_metabox($post) {
wp_nonce_field(basename(__FILE__), \'slider_nonce\');
$slide_url_value = get_post_meta($post->ID, \'_slide_url\', true);
$slide_url = isset($slide_url_value) ? esc_attr($slide_url_value) : \'\';
$services_loop = new WP_Query(
array(
\'post_type\' => \'services\',
\'posts_per_page\' => -1,
\'post_status\' => \'publish\'
));
?>
<div class="slider meta">
<p>
<label for="slide-url"><?php _e(\'Select URL from the dropdown below for this slide\', \'rmtheme\' ); ?></label>
<br />
<select name="_slide_url">
<?php while ($services_loop->have_posts()) : $services_loop->the_post(); ?>
<option value="<?php the_permalink(); ?>"><?php the_title(); ?></option>
<?php endwhile; ?>
</select>
</p>
</div>
<?php
}
function rm_save_slider_meta($post) {
global $post;
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
return $post_id;
if (!isset($_POST[\'slider_nonce\']) || !wp_verify_nonce($_POST[\'slider_nonce\'], basename(__FILE__)))
return $post_id;
if (!current_user_can(\'edit_post\'))
return $post_id;
$allowed = array(
\'a\' => array(
\'href\' => array()
)
);
if (isset($_POST[\'_slide_url\']))
update_post_meta($post->ID, \'_slide_url\', wp_kses($_POST[\'_slide_url\'], $allowed));
}
add_action(\'save_post\', \'rm_save_slider_meta\');
add_action(\'add_meta_boxes\', \'rm_slider_metabox\');