feat: add Letters to the Editor post type

Introduced a new custom post type "Letters to the Editor" with corresponding archive and single templates. Enhanced post type functionality includes support for featured images, authors, excerpts, and comments. Added pagination and a meta box for article authors to improve user interaction and content management.
This commit is contained in:
Kumi 2024-06-20 20:53:04 +02:00
parent 1d2734bf00
commit 5adf97494d
Signed by: kumi
GPG key ID: ECBCC9082395383F
3 changed files with 131 additions and 0 deletions

44
archive-letter.php Normal file
View file

@ -0,0 +1,44 @@
<?php get_header(); ?>
<!-- Archive Content -->
<section class="container my-5">
<h1 class="display-4 text-center">Letters to the Editor</h1>
<div class="row">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<?php if (has_post_thumbnail()) : ?>
<img src="<?php the_post_thumbnail_url(); ?>" class="card-img-top" alt="<?php the_title(); ?>">
<?php endif; ?>
<div class="card-body">
<h5 class="card-title"><?php the_title(); ?></h5>
<p class="card-text"> <?php
$authors = get_post_meta(get_the_ID(), 'article_authors', true);
if (!empty($authors)) {
echo implode(', ', array_map('esc_html', $authors));
} else {
the_author();
}
?> | <?php the_date(); ?></p>
<p class="card-text"><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>" class="btn btn-outline-primary">Read More</a>
</div>
</div>
</div>
<?php endwhile;
else : ?>
<p><?php _e('Sorry, no letters found.', 'duck-behavior-journal'); ?></p>
<?php endif; ?>
</div>
<div class="row">
<div class="col-md-12">
<?php
echo paginate_links(array(
'total' => $wp_query->max_num_pages,
));
?>
</div>
</div>
</section>
<?php get_footer(); ?>

View file

@ -409,3 +409,63 @@ function redirect_doi_to_article($query)
}
add_action('parse_query', 'redirect_doi_to_article');
function create_letter_post_type() {
$labels = array(
'name' => _x('Letters to the Editor', 'Post type general name', 'duck-behavior-journal'),
'singular_name' => _x('Letter to the Editor', 'Post type singular name', 'duck-behavior-journal'),
'menu_name' => _x('Letters to the Editor', 'Admin Menu text', 'duck-behavior-journal'),
'name_admin_bar' => _x('Letter to the Editor', 'Add New on Toolbar', 'duck-behavior-journal'),
'add_new' => __('Add New', 'duck-behavior-journal'),
'add_new_item' => __('Add New Letter', 'duck-behavior-journal'),
'new_item' => __('New Letter', 'duck-behavior-journal'),
'edit_item' => __('Edit Letter', 'duck-behavior-journal'),
'view_item' => __('View Letter', 'duck-behavior-journal'),
'all_items' => __('All Letters', 'duck-behavior-journal'),
'search_items' => __('Search Letters', 'duck-behavior-journal'),
'parent_item_colon' => __('Parent Letters:', 'duck-behavior-journal'),
'not_found' => __('No letters found.', 'duck-behavior-journal'),
'not_found_in_trash' => __('No letters found in Trash.', 'duck-behavior-journal'),
'featured_image' => _x('Featured Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'duck-behavior-journal'),
'set_featured_image' => _x('Set featured image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'duck-behavior-journal'),
'remove_featured_image' => _x('Remove featured image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'duck-behavior-journal'),
'use_featured_image' => _x('Use as featured image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'duck-behavior-journal'),
'archives' => _x('Letter archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'duck-behavior-journal'),
'insert_into_item' => _x('Insert into letter', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'duck-behavior-journal'),
'uploaded_to_this_item' => _x('Uploaded to this letter', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'duck-behavior-journal'),
'filter_items_list' => _x('Filter letters list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'duck-behavior-journal'),
'items_list_navigation' => _x('Letters list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'duck-behavior-journal'),
'items_list' => _x('Letters list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'duck-behavior-journal'),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'letter'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
);
register_post_type('letter', $args);
}
add_action('init', 'create_letter_post_type');
function add_article_meta_boxes() {
add_meta_box(
'article_authors',
__('Authors', 'duck-behavior-journal'),
'render_article_authors_meta_box',
array('letter'),
'side',
'default'
);
}
add_action('add_meta_boxes', 'add_letter_meta_boxes');

27
single-letter.php Normal file
View file

@ -0,0 +1,27 @@
<?php get_header(); ?>
<!-- Letter Content -->
<section class="container my-5">
<div class="row">
<div class="col-lg-8 mx-auto">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1 class="display-4"><?php the_title(); ?></h1>
<?php
$authors = get_post_meta(get_the_ID(), 'article_authors', true);
if (!empty($authors)) {
echo implode(', ', array_map('esc_html', $authors));
} else {
the_author();
}
?> | <?php the_date(); ?>
<?php if (has_post_thumbnail()) : ?>
<img src="<?php the_post_thumbnail_url(); ?>" class="img-fluid mb-4" alt="<?php the_title(); ?>">
<?php endif; ?>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
</div>
</div>
</section>
<?php get_footer(); ?>