我有一个电影网站,电影作为帖子,演员作为自定义分类。对于我的自定义分类,我在我的 functions.php 文件中添加了这个:
$labels = array(
'name' => _x( 'Actors', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Actor', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Actors', 'textdomain' ),
'popular_items' => __( 'Popular Actors', 'textdomain' ),
'all_items' => __( 'All Actors', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Actor', 'textdomain' ),
'update_item' => __( 'Update Actor', 'textdomain' ),
'add_new_item' => __( 'Add New Actor', 'textdomain' ),
'new_item_name' => __( 'New Actor Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate actors with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove actors', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used actors', 'textdomain' ),
'not_found' => __( 'No actors found.', 'textdomain' ),
'menu_name' => __( 'Actors', 'textdomain' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'actor' ),
);
register_taxonomy( 'actor', 'post', $args );
我在我的 functions.php 文件中添加了一个字段,其中包含他们的出生日期(格式为 yyyy-mm-dd):
function crq_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[starbday]"><?php _e( 'Birthdate', 'responsivo' ); ?></label>
<input type="text" name="term_meta[starbday]" id="term_meta[starbday]" value="">
<p class="description"><?php _e( 'Enter Birthdate (in <em>yyyy-mm-dd</em> format)','responsivo' ); ?></p>
</div>
<?php }
add_action( 'actor_add_form_fields', 'crq_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function crq_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[starbday]"><?php _e( 'Birthdate', 'responsivo' );
?>
</label>
</th>
<td>
<input type="text" name="term_meta[starbday]" id="term_meta[starbday]" value="<?php echo esc_attr( $term_meta['starbday'] ) ? esc_attr( $term_meta['starbday'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter Birthdate (in <em>yyyy-mm-dd</em> format)','responsivo' ); ?></p>
</td> </tr>
add_action( 'actor_edit_form_fields', 'crq_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_actor', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_actor', 'save_taxonomy_custom_meta', 10, 2 );
我可以在分类模板中使用以下代码在参与者的分类页面上显示此字段:
<?php
$birthd = $term_meta['starbday'];
echo date('F j, Y', strtotime($birthd));
?>
现在,我正在尝试运行查询以在主页上显示今天的生日。到目前为止,这是我想出的。
<ul>
<?php
$todaymd = date('m-d');
$args = array(
'taxonomy' => 'actor',
'orderby' => 'name'
'order' => 'ASC',
'hide_empty' => false,
'meta_query' => array(
array(
'key' => 'starbday',
'value' => '_____'.$todaymd,
'compare' => 'LIKE'
)
)
);
$terms = get_terms( 'actor', $args );
foreach( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
这根本不起作用,并且没有显示任何内容。如果今天的日期没有生日就会出现这种情况,即使我更改$todaymd
为'03-15'
(我知道这是几位演员的出生月份和日期),它仍然没有显示任何内容。有什么建议么?
我在这里看到了类似的问题... https://wordpress.stackexchange.com/questions/82323/how-can-i-get-the-actor-birthday-by-date,但没有回答,因为解决方案没有奏效。