目前我所知道的最好的处理方法是Posts 2 Posts plugin:
这里有一个示例,展示了如何设置自定义帖子类型(如果您已经有了这些类型,那么阅读本文的其他人会受益匪浅)以及
p2p_register_connection_type()
插件需要设置帖子关系。这可以放在你的主题中
functions.php
文件或中的
.PHP
您可能正在编写的插件的文件:
add_action(\'init\',\'event_performer_init\');
function event_performer_init() {
register_post_type(\'event\',
array(
\'label\' => \'Events\',
\'public\' => true,
\'show_ui\' => true,
\'query_var\' => \'event\',
\'rewrite\' => array(\'slug\' => \'events\'),
\'hierarchical\' => true,
//\'supports\' => array(\'title\',\'editor\',\'custom-fields\'),
)
);
register_post_type(\'performer\',
array(
\'label\' => \'Performers\',
\'public\' => true,
\'show_ui\' => true,
\'query_var\' => \'performer\',
\'rewrite\' => array(\'slug\' => \'performers\'),
\'hierarchical\' => true,
//\'supports\' => array(\'title\',\'editor\',\'custom-fields\'),
)
);
if ( function_exists(\'p2p_register_connection_type\') )
p2p_register_connection_type( \'event\', \'performer\' );
global $wp_rewrite;
$wp_rewrite->flush_rules(false); // This only needs be done first time
}
然后在主题的模板文件中
single-event.php
您可以添加如下代码来显示每个乐队的信息(我在这里展示了基本知识;我将留给您填写所有细节和/或在WordPress答案网站上询问其他更具体的问题,例如您是否需要知道如何获取特色图像等)
<?php
if (count($performers = p2p_get_connected($post->ID))) {
foreach($performers as $performer_id) {
$performer = get_post($performer_id);
echo \'The Band: \' . apply_filters(\'the_title\',$performer->post_title);
echo \'Facebook Link: \' . get_post_meta($post->ID,\'facebook_link\',true);
}
}
?>