我经常使用tinymce“链接到现有内容”按钮来链接到帖子,但想知道是否也可以在列表中显示类别?
我们需要链接到相当多的类别,手动操作有点痛苦。
有谁知道这是否可行?
谢谢,
詹姆斯
我认为此功能没有内置行为。它必须是自定义编码。
这就是我用的:
function custom_wp_link_query_args( $query ) {
// Only modify the query if the current user can edit posts
if (!current_user_can('edit_posts')) {
return $query;
}
// Ensure that we are including your custom post type
$query['post_type'] = array_merge((array) $query['post_type'], array('your-cpt'));
return $query;
}
add_filter('wp_link_query_args', 'custom_wp_link_query_args');
function custom_wp_link_query( $results, $query ) {
// Initialize an array for new results including your custom taxonomy terms
$new_results = array();
foreach ($results as $result) {
$new_results[] = $result;
}
// Get terms from your custom taxonomy and add them to results
$terms = get_terms(array(
'taxonomy' => 'your-cpt-taxonomy',
'hide_empty' => false,
));
foreach ($terms as $term) {
$new_results[] = array(
'ID' => $term->term_id,
'title' => $term->name,
'permalink' => get_term_link($term),
'info' => 'Your CPT taxonomy title',
);
}
return $new_results;
}
add_filter('wp_link_query', 'custom_wp_link_query', 10, 2);
确保将 your-cpt、your-cpt-taxonomy 和 Your CPT 分类标题更改为您的场景。