作为一名程序员,我会开始编写自己的代码来连接我的帖子类型。这不是一种快速的方式,也不容易,但很有趣。
The post types
我们首先创建两种简单的帖子类型,
Author
和
Books
:
add_action(\'init\', \'p2p2_register_author\');
add_action(\'init\', \'p2p2_register_book\');
function p2p2_register_author(){
$labels = array(
\'name\' => \'Author\',
\'singular_name\' => \'Author\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New Author\',
\'edit_item\' => \'Edit Author\',
\'new_item\' => \'New Author\',
\'all_items\' => \'All Authors\',
\'view_item\' => \'View Authors\',
\'search_items\' => \'Search Authors\',
\'not_found\' => \'No authors found\',
\'not_found_in_trash\' => \'No authors found in Trash\',
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Authors\'
);
register_post_type(
\'Author\',
array (
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'author\' ),
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
)
);
}
function p2p2_register_book(){
$labels = array(
\'name\' => \'Books\',
\'singular_name\' => \'Book\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New Book\',
\'edit_item\' => \'Edit Book\',
\'new_item\' => \'New Book\',
\'all_items\' => \'All Books\',
\'view_item\' => \'View Book\',
\'search_items\' => \'Search Books\',
\'not_found\' => \'No books found\',
\'not_found_in_trash\' => \'No books found in Trash\',
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Books\'
);
register_post_type(
\'Book\',
array (
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'book\' ),
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
)
);
}
没什么特别的。事实上,它来自
Codex!
The metabox
让我们继续向book post类型中添加作者的metabox:
add_action(\'admin_init\', \'p2p2_add_author_metabox\');
function p2p2_add_author_metabox(){
add_meta_box(
\'book_author\',
__(\'Book Author\', \'bandpress\'),
\'p2p2_book_author_metabox\',
\'book\',
\'side\',
\'default\',
array( \'id\' => \'p2p2_author\')
);
}
在这里你可以看到一个回调函数
p2p2_book_author_metabox
这将是我们的代谢箱内的内容。
The content of the metabox
让我们创建函数:
function p2p2_book_author_metabox($post, $args){
wp_nonce_field( plugin_basename( __FILE__ ), \'p2p2_book_author_nonce\' );
$author_id = get_post_meta($post->ID, \'p2p2_book_author\', true);
echo "<p>Select the author of the book</p>";
echo "<select id=\'p2p2_book_author\' name=\'p2p2_book_author\'>";
// Query the authors here
$query = new WP_Query( \'post_type=author\' );
while ( $query->have_posts() ) {
$query->the_post();
$id = get_the_ID();
$selected = "";
if($id == $author_id){
$selected = \' selected="selected"\';
}
echo \'<option\' . $selected . \' value=\' . $id . \'>\' . get_the_title() . \'</option>\';
}
echo "</select>";
}
这就是魔法发生的地方。首先,我们要在数据库中查询作者,然后填写
<select>
使用我们的查询结果。检查
Codex 有关详细信息
WP_Query
. 现在,您可以转到您的book post类型并查看下拉列表:
Saving our content
当然,我们希望保存所选内容,因此我们添加了另一个函数来为我们保存metabox:
add_action(\'save_post\', \'p2p2_save_author_metabox\', 1, 2);
function p2p2_save_author_metabox($post_id, $post){
// Don\'t wanna save this now, right?
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
if ( !isset( $_POST[\'p2p2_book_author_nonce\'] ) )
return;
if ( !wp_verify_nonce( $_POST[\'p2p2_book_author_nonce\'], plugin_basename( __FILE__ ) ) )
return;
// We do want to save? Ok!
$key = \'p2p2_book_author\';
$value = $_POST["p2p2_book_author"];
if ( get_post_meta( $post->ID, $key, FALSE ) ) { // If the custom field already has a value
update_post_meta( $post->ID, $key, $value );
} else { // If the custom field doesn\'t have a value
add_post_meta( $post->ID, $key, $value );
}
if ( !$value ) delete_post_meta( $post->ID, $key ); // Delete if blank
}
现在去和作者一起保存一本书吧!这本书的作者将被保存在
wp_postmeta
数据库表和下拉列表的选定值将是元中的值。
An author column for book
让我们为我们的书扩展管理区域。我们将从更改列开始:
add_filter(\'manage_edit-book_columns\', \'p2p2_add_book_columns\');
function p2p2_add_book_columns($columns){
$new_columns[\'cb\'] = \'<input type="checkbox" />\';
$new_columns[\'title\'] = _x(\'Title\', \'column name\', \'bandpress\');
$new_columns[\'p2p2_author\'] = __(\'Author\', \'bandpress\');
return $new_columns;
}
此函数确保我们只看到列标题和p2p2\\U作者。WordPress的批量编辑功能需要cb复选框列。现在我们需要在我们的专栏中添加一些信息。我们添加了此功能:
add_action(\'manage_book_posts_custom_column\', \'p2p2_fill_book_columns\', 10, 2);
function p2p2_fill_book_columns($column_name, $id) {
global $wpdb;
switch ($column_name) {
case \'p2p2_author\':
$author_id = get_post_meta($id, \'p2p2_book_author\', true);
$author = get_post($author_id);
$permalink = get_permalink($author_id);
echo "<a href=\'" . $permalink . "\'>" . $author->post_title . "</a>";
break;
default:
break;
} // end switch
}
该开关适用于您刚才在上一个函数中添加的每一列。你通过回应你想展示的内容来填满它。我们得到了这本书的作者的帖子,并为他/她的“个人资料页”创建了一个很好的永久链接。它看起来是这样的:
To be continued
我们在WordPress站点的后端连接了两种帖子类型,但在前端我们看不到任何东西。要做到这一点,还需要做很多工作,但可能性是无限的。我们可以:
按作者对书籍进行排序在作者页上显示书籍列表在书籍页上显示作者的其他书籍列表创建一个包含作者图片的奇特元数据库在作者管理页中为他/她所写的书籍创建一列,以及更多我将继续研究这个答案,因为我自己需要这个解决方案。然而,我现在将停止在中的工作。明天我将开始更新此答案。