自定义Wordpress分类标准(演员),在主页上显示今天的生日和查询

时间:2018-03-29 02:26:08

标签: php wordpress custom-taxonomy taxonomy-terms

我有一个电影网站,电影作为帖子,演员作为自定义分类。对于我的自定义分类,我在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 );

我可以在actor的分类页面上显示这个字段,并在分类模板中使用以下代码:

<?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,但由于解决方案无效,所以没有回答。

1 个答案:

答案 0 :(得分: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']); 
    }
}

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