WordPress自定义分类法(Actors):实现首页显示今日生日演员
Hey there! Since you’ve already set up the "Actors" custom taxonomy for your movie site, let’s walk through how to show actors who have a birthday today on your homepage. We’ll need to add a birthday field to your Actors taxonomy first, then write a custom query to fetch those actors, and finally display them.
Step 1: Add a Birthday Custom Field to the Actors Taxonomy
First, we need a way to store each actor’s birthday. Add this code to your functions.php file (make sure to back up the file first!):
// Add birthday field to Actors taxonomy edit page add_action('edit_term_form_fields', 'add_actor_birthday_field', 10, 2); function add_actor_birthday_field($term, $taxonomy) { // Only target the Actors taxonomy if ($taxonomy !== 'actors') return; $birthday = get_term_meta($term->term_id, 'actor_birthday', true); ?> <tr class="form-field"> <th scope="row" valign="top"> <label for="actor_birthday"><?php _e('Birthday', 'textdomain'); ?></label> </th> <td> <input type="date" name="actor_birthday" id="actor_birthday" value="<?php echo esc_attr($birthday); ?>" class="regular-text"> <p class="description"><?php _e('Enter the actor\'s birthday (YYYY-MM-DD format)', 'textdomain'); ?></p> </td> </tr> <?php } // Save the birthday field when updating an actor term add_action('edited_term', 'save_actor_birthday_field', 10, 3); function save_actor_birthday_field($term_id, $tt_id, $taxonomy) { if ($taxonomy !== 'actors') return; if (isset($_POST['actor_birthday'])) { update_term_meta($term_id, 'actor_birthday', sanitize_text_field($_POST['actor_birthday'])); } else { delete_term_meta($term_id, 'actor_birthday'); } }
Note: Replace 'actors' with your actual Actors taxonomy slug if it’s different.
Step 2: Write a Custom Query to Fetch Today’s Birthday Actors
Next, we’ll create a function to get actors whose birthday matches today’s month and day. Add this to your functions.php too:
function get_today_birthday_actors() { // Get current month and day (format: MM-DD) $today_month_day = date('m-d'); $args = array( 'taxonomy' => 'actors', 'hide_empty' => false, // Include actors even if no posts are assigned 'meta_query' => array( array( 'key' => 'actor_birthday', 'value' => $today_month_day, 'compare' => 'LIKE', 'type' => 'DATE' ) ) ); return get_terms($args); }
This query looks for actor terms where the actor_birthday meta value ends with today’s MM-DD (since we stored birthdays as YYYY-MM-DD).
Step 3: Display the Actors on Your Homepage
Now, open your homepage template file (usually front-page.php or index.php) and add this code where you want the birthday actors to appear:
<?php $birthday_actors = get_today_birthday_actors(); if (!empty($birthday_actors)) { echo '<section class="birthday-actors">'; echo '<h2>🎂 Today\'s Birthday Actors</h2>'; echo '<ul>'; foreach ($birthday_actors as $actor) { $actor_link = get_term_link($actor); $birthday = get_term_meta($actor->term_id, 'actor_birthday', true); // Calculate age (optional) $age = date_diff(date_create($birthday), date_create('today'))->y; echo '<li>'; echo '<a href="' . esc_url($actor_link) . '">' . esc_html($actor->name) . '</a>'; if ($age) { echo ' (' . $age . ' years old)'; } echo '</li>'; } echo '</ul>'; echo '</section>'; } else { echo '<p>No actors have a birthday today.</p>'; } ?>
You can style the .birthday-actors section with CSS to match your site’s design.
Quick Notes
- Make sure all existing actors have their birthdays added via the Actors taxonomy edit page (under Posts > Actors in your WordPress admin).
- If you stored birthdays in a different format (like DD/MM/YYYY), you’ll need to adjust the
meta_queryto match that format’s month/day positioning.
内容的提问来源于stack exchange,提问作者Reid C.

