0

我有一个电影网站,电影作为帖子,演员作为自定义分类。对于我的自定义分类,我在我的 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,但没​​有回答,因为解决方案没有奏效。

4

1 回答 1

0

实际上,Sally J,我想通了,感谢https://www.wphub.com/blog/posts/adding-metadata-taxonomy-terms/哎呀!错误的链接!请参阅下面的补充)对于 functions.php 部分并用这个更新主页:

<ul>
     <?php 
     $todaymd = date('m-d');
     $args = array(
         'taxonomy' => 'actor',
         'orderby' => 'name',
         'order' => 'ASC',
     );
     $terms = get_terms( $args );
     foreach( $terms as $term ) { 
         $starbday = get_term_meta($term->term_id, 'starbday', true);
         if((substr($starbday,5,5)) == $todaymd) { ?>
               <li><?php echo $term->name; ?></li>
         <?php } else { }} ?>
</ul>

它现在确实有效,但它似乎不是最有效的方式(因为我拉所有演员然后过滤它们,而不是在获取它们时过滤),但它有效。

谢谢您的帮助!

更新由于我在上面输入了错误的链接,这就是我的functions.php现在的内容:

// Add term page for Actor, Director and Book Author Taxonomies
add_action('actor_add_form_fields', 'crq_metabox_add', 10, 1); 
add_action('actor_edit_form_fields', 'crq_metabox_edit', 10, 1);

function crq_metabox_add($tag) {
?>
    <div class="form-field">            
        <label for="crq_starbday"><?php _e( 'Birthdate', 'responsivo' ); ?></label>         
        <input type="text" name="crq_starbday" id="crq_starbday" value="">      
        <p class="description"><?php _e( 'Enter Birthdate (in <em>yyyy-mm-dd</em> format)','responsivo' ); ?></p>   
    </div>
<?php }

// Edit term page for Actor, Director and Book Author Taxonomies

function crq_metabox_edit($tag) {
?>

<tr class="form-field">
    <th scope="row" valign="top">
        <label for="crq_starbday"><?php _e( 'Birthdate', 'responsivo' ); ?></label>
    </th>
    <td>
        <input type="text" name="crq_starbday" id="crq_starbday" value="<?php echo get_term_meta($tag->term_id, 'crq_starbday', true); ?>" />
        <p class="description"><?php _e( 'Enter Birthdate (in <em>yyyy-mm-dd</em> format)','responsivo' ); ?></p>       
    </td>   
</tr>
<?php 
}

// Save extra fields for Actor, Director and Book Author Taxonomies

add_action( 'created_actor', 'save_taxonomy_metadata', 10, 1 );  
add_action( 'edited_actor', 'save_taxonomy_metadata', 10, 1 );

function save_taxonomy_metadata($term_id)
{
    if (isset($_POST['crq_starbday'])) {
        update_term_meta( $term_id, 'crq_starbday', $_POST['crq_starbday']); 
    }
}

...它使用 new(er) update_term_meta()。我尝试meta_query()在主页上使用,但它没有显示任何结果(可能是因为$todaymdstarbday是如此不同(starbday是 yyyy-mm-dd,而$todaymd只是 mm-dd)。而且该'_____'.$todaymd值也不起作用。

于 2018-03-30T02:02:13.443 回答