refactor: extract publication status formatting

Moved code for fetching and displaying publication status terms into a reusable function `display_publication_status_terms`. This simplifies template files by reducing redundancy and enhances maintainability. The new function also supports displaying terms with associated colors.
This commit is contained in:
Kumi 2024-08-03 17:12:13 +02:00
parent 594a694178
commit c738ed6f89
Signed by: kumi
GPG key ID: ECBCC9082395383F
4 changed files with 15 additions and 30 deletions

View file

@ -63,16 +63,7 @@
</p>
<p class="card-text"><?php the_excerpt(); ?></p>
<p class="card-text">
<?php
$terms = get_the_terms(get_the_ID(), 'publication_status');
if (!empty($terms) && !is_wp_error($terms)) {
$term_list = array();
foreach ($terms as $term) {
$term_list[] = $term->name;
}
echo implode(', ', $term_list);
}
?>
<?php display_publication_status_terms(get_the_ID()); ?>
</p>
<a href="<?php the_permalink(); ?>" class="btn btn-outline-primary">Read More</a>
</div>

View file

@ -326,6 +326,17 @@ function enqueue_color_picker($hook_suffix)
add_action('admin_enqueue_scripts', 'enqueue_color_picker');
function display_publication_status_terms($post_id) {
$terms = get_the_terms($post_id, 'publication_status');
if (!empty($terms) && !is_wp_error($terms)) {
foreach ($terms as $term) {
$color = get_term_meta($term->term_id, 'publication_status_color', true);
$color_style = $color ? ' style="color:' . esc_attr($color) . ';"' : '';
echo '<span' . $color_style . '>' . esc_html($term->name) . '</span> ';
}
}
}
function add_article_meta_boxes()
{
add_meta_box(

View file

@ -45,16 +45,7 @@
</p>
<p class="card-text"><?php the_excerpt(); ?></p>
<p class="card-text">
<?php
$terms = get_the_terms(get_the_ID(), 'publication_status');
if (!empty($terms) && !is_wp_error($terms)) {
$term_list = array();
foreach ($terms as $term) {
$term_list[] = $term->name;
}
echo implode(', ', $term_list);
}
?>
<?php display_publication_status_terms(get_the_ID()); ?>
</p>
<a href="<?php the_permalink(); ?>" class="btn btn-outline-primary">Read More</a>
</div>

View file

@ -23,16 +23,8 @@
<span id="doi-copy-message" style="display: none; color: green; margin-left: 10px;"><?php _e('Copied!', 'duck-behavior-journal'); ?></span>
</p>
<p class="text-muted">
<strong>Status:</strong> <?php
$terms = get_the_terms(get_the_ID(), 'publication_status');
if (!empty($terms) && !is_wp_error($terms)) {
$term_list = array();
foreach ($terms as $term) {
$term_list[] = $term->name;
}
echo implode(', ', $term_list);
}
?>
<strong>Status:</strong>
<?php display_publication_status_terms(get_the_ID()); ?>
</p>
<?php if (has_excerpt()) : ?>
<p class="text-muted"><strong>Abstract:</strong> <?php echo get_the_excerpt(); ?></p>