插件建议在这里被认为是离题的。一种解决方案是在帖子中添加一个元框,如下所示:
Add to Functions.php
/** Add Post Metabox **/
function add_custom_meta_box() {
add_meta_box(
\'pick_post\', // $id
\'Pick Post\', // $title
\'show_custom_meta_box_pick_post\', // $callback
\'agenda\', // $page
\'side\', // $context
\'high\'); // $priority
}
add_action(\'add_meta_boxes\', \'add_custom_meta_box\');
/** Post Metabox Callback (show some stuff in box) **/
function show_custom_meta_box_pick_post() {
$doelArr = get_posts( array( \'post_type\' => \'doel\', \'orderby\' => \'title\', \'order\' => \'ASC\', \'posts_per_page\' => -1 ) );
$meta_doel = get_post_meta($post->ID, \'_doellist\', true);
?>
<input type="hidden" name="info_meta_box_nonce" value="<?php echo wp_create_nonce(basename(__FILE__)); ?>" />
<strong>Associated Doel</strong>
<br />
<select id="selectdoel" name="_doellist">
<option value="">Select Doel</option>
<?php foreach($doelArr as $doel) : setup_postdata( $doel ) ?>
<?php if($doel->ID == $meta_doel) : ?>
<option value="<?php echo $doel->ID; ?>" selected="selected"><?php echo $doel->post_title; ?></option>
<?php else : ?>
<option value="<?php echo $doel->ID; ?>"><?php echo $doel->post_title; ?></option>
<?php endif; ?>
<?php endforeach; ?>
</select>
}
/** Save Our Meta-data **/
function save_custom_meta($post_id) {
global $post;
// check autosave
if ((defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) || (!current_user_can(\'edit_post\', $post_id)))
return $post_id;
if($post->post_type == \'agenda\')
{
// Doel Selection List Check
if(isset($_POST[\'_doellist\']) && $_POST[\'_doellist\'] != \'\')
update_post_meta($post_id, \'_doellist\', strip_tags($_POST[\'_doellist\']));
else
delete_post_meta($post_id, \'_doellist\');
}
}
这样做的目的是创建一个包含Doel帖子下拉列表的小型元框,然后将Doel帖子ID保存为元值。您可以在任何时候通过将议程ID传递给
get_post_meta()
作用
$meta_doel = get_post_meta($post->ID, \'_doellist\', true);
然后你可以像这样得到整个Doel帖子:
$doel_post = get_post($meta_doel);