嗨,我有一个自定义帖子类型为"研究员"的网站,其中有一个名为"主管"的分类法,其中有名为"职务"的元字段。我想知道如何在前端显示这个"职务"。
元字段(来自functions.php)的代码如下:
//编辑主管分类
// Add term page
function jobtitle_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[jobtitle]"><?php _e( 'Job Title', 'jobtitle' ); ?></label>
<input type="text" name="term_meta[jobtitle]" id="term_meta[jobtitle]" value="">
<p class="description"><?php _e( 'Enter a value for this field','jobtitle' ); ?></p>
</div>
<?php
}
add_action( 'supervisor_add_form_fields', 'jobtitle_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function jobtitle_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[jobtitle]"><?php _e( 'Job Title', 'jobtitle' ); ?></label></th>
<td>
<input type="text" name="term_meta[jobtitle]" id="term_meta[jobtitle]" value="<?php echo esc_attr( $term_meta['jobtitle'] ) ? esc_attr( $term_meta['jobtitle'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter a value for this field','jobtitle' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'supervisor_edit_form_fields', 'jobtitle_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_supervisor', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_supervisor', 'save_taxonomy_custom_meta', 10, 2 );
//结束编辑主管分类
显示后端的图像--http://d.pr/i/TS13
您将需要:
-
研究人员的帖子ID在
single-researcher.php
页面上(如果您的帖子类型为researcher
),您应该能够使用$post->ID来获取研究人员的帖子ID -
分类法的名称这可以通过阅读您之前发布的屏幕截图的URL来获得。应该说:
...edit-tags.php?taxonomy=supervisors&...
。我假设该分类法命名为supervisors
如果你在single-researcher.php
页面上,这可能会起作用。。。注意:$supervisor_tags
是该研究人员的标签数组。每个研究人员可能有0个、1个或更多的主管,所以你可能想循环浏览他们:
global $post;
$supervisor_tags = get_the_terms($post->ID, 'supervisors');
echo "<h2>Supervisors:</h2>";
foreach ($supervisor_tags as $tag){
$term_meta = get_option('taxonomy_' . $tag->term_id);
echo "<p>Name: " . $tag->name . " - Job Title: " . $term_meta['jobtitle'] . "</p>";
}
如果遇到问题,可以echo "<pre>"; print_r($variable); echo "</pre>";
查看$variable
阵列的外观。