所以,我想尝试做一些有点复杂的事情,当谈到WordPress时,它有点高于我目前的技术水平!
我有一个名为" 产品"的自定义帖子类型。我希望能够完成每个"帖子"从产品发布类型到网站上每个其他帖子类型的选择元框,标准和自定义类似。
然后,我希望能够使用选择将信息提取到页面上,以便将其显示给用户。
我不知道我是如何开始这样做的。我可以制作元框和自定义帖子类型,我只是不知道如何将它们粘合在一起!
答案 0 :(得分:1)
我创建了相同的代码,这里是从post-type
中提取帖子的代码,并将其显示在帖子类型的select标记中。
您必须创建一个元框,然后您需要从另一个帖子类型中提取帖子,以便它将在如下创建的元框的选择标记中填充帖子名称。
我为destination_category
创建了它。您可以更改post_type
并按顺序显示地点,以使您的代码成功。
<?php
//Creation of Meta Box for post type "destination_category" (Start)
add_action( 'admin_init', 'my_destination_category' );
//destination_sub_category_admin - is the required HTML id attribute
//Select Destination Sub Category - is the text visible in the heading of the meta box section
//display_destination_subcategory_meta_box - is the callback which renders the contents of the meta box
//destination_category - is the name of the custom post type where the meta box will be displayed
// normal - defines the part of the page where the edit screen section should be shown
// high - defines the priority within the context where the boxes should show
function my_destination_category() {
add_meta_box( 'destination_sub_category_admin','Select Destination Sub Category','display_destination_subcategory_meta_box', 'destination_category', 'normal', 'high');
function display_destination_subcategory_meta_box( $select_category ) {
// Retrieve Current Selected Category ID based on the Category Created
global $wpdb;
$selectcat="SELECT * FROM ".$wpdb->prefix."posts WHERE `post_type`='destination_category' AND `post_status`='publish' ORDER BY `ID` DESC";
$resultant = $wpdb->get_results($selectcat);
$rescount=count($resultant);
$category_selected_id = intval( get_post_meta( $select_category->ID, 'destination_category_id', true ) );
?>
<link rel="stylesheet" type="text/css" href="<?php echo plugins_url('css/metabox.css',__FILE__ ) ?>" />
<table>
<tr>
<td style="width: 150px">Select Category</td>
<td>
<select style="width: 100px" name="category_selection" id="meta_box_category" style="float:left; width:50%; !important">
<?php
if($rescount==0)
{?>
<option value="null">No Posts have been created</option>
<?php
}
else
{
// Generate all items of drop-down list
foreach($resultant as $singleresultant)
{
?>
<option value="<?php echo $singleresultant->ID; ?>" <?php echo selected( $singleresultant->ID, $category_selected_id ); ?>>
<?php echo $singleresultant->post_title; ?>
</option>
<?php
}
}
?>
</select>
</td>
</tr>
</table>
<?php
}
// Registering a Save Post Function
add_action( 'save_post', 'destination_admin_sub_category', 10, 2 );
function destination_admin_sub_category( $select_category_id, $select_category ) {
// Check post type for movie reviews
if ( $select_category->post_type == 'destination_category' ) {
// Store data in post meta table if present in post data
if ( isset( $_POST['category_selection'] ) && $_POST['category_selection'] != '' ) {
echo update_post_meta( $select_category_id, 'destination_category_id', $_POST['category_selection'] );
}
}
}
}
?>
我在这里提供了详细的代码说明,以便您可以非常轻松地开发代码。
跳,这样可以帮助你解决问题。