feat: introduce custom post type for articles
Added a custom post type 'article' including templates for index, archive, and single views. Enhanced article metadata handling with a custom meta box for authors. Updated homepage to feature articles instead of generic posts. This provides a more specialized structure and better organization for journal content.
This commit is contained in:
parent
c61ba4a695
commit
f6e4824fe5
4 changed files with 186 additions and 2 deletions
39
archive-article.php
Normal file
39
archive-article.php
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
<?php get_header(); ?>
|
||||||
|
|
||||||
|
<!-- Archive Content -->
|
||||||
|
<section class="container my-5">
|
||||||
|
<h1 class="display-4 text-center">Articles</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 esc_html($authors[0]);
|
||||||
|
if (count($authors) > 1) {
|
||||||
|
echo ' et al.';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
the_author();
|
||||||
|
}
|
||||||
|
?> | <?php the_date(); ?>
|
||||||
|
</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 articles found.', 'duck-behavior-journal'); ?></p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<?php get_footer(); ?>
|
104
functions.php
104
functions.php
|
@ -166,3 +166,107 @@ function duck_behavior_journal_customizer($wp_customize)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
add_action('customize_register', 'duck_behavior_journal_customizer');
|
add_action('customize_register', 'duck_behavior_journal_customizer');
|
||||||
|
|
||||||
|
function create_article_post_type()
|
||||||
|
{
|
||||||
|
$labels = array(
|
||||||
|
'name' => _x('Articles', 'Post type general name', 'duck-behavior-journal'),
|
||||||
|
'singular_name' => _x('Article', 'Post type singular name', 'duck-behavior-journal'),
|
||||||
|
'menu_name' => _x('Articles', 'Admin Menu text', 'duck-behavior-journal'),
|
||||||
|
'name_admin_bar' => _x('Article', 'Add New on Toolbar', 'duck-behavior-journal'),
|
||||||
|
'add_new' => __('Add New', 'duck-behavior-journal'),
|
||||||
|
'add_new_item' => __('Add New Article', 'duck-behavior-journal'),
|
||||||
|
'new_item' => __('New Article', 'duck-behavior-journal'),
|
||||||
|
'edit_item' => __('Edit Article', 'duck-behavior-journal'),
|
||||||
|
'view_item' => __('View Article', 'duck-behavior-journal'),
|
||||||
|
'all_items' => __('All Articles', 'duck-behavior-journal'),
|
||||||
|
'search_items' => __('Search Articles', 'duck-behavior-journal'),
|
||||||
|
'parent_item_colon' => __('Parent Articles:', 'duck-behavior-journal'),
|
||||||
|
'not_found' => __('No articles found.', 'duck-behavior-journal'),
|
||||||
|
'not_found_in_trash' => __('No articles 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('Article 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 article', '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 article', '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 articles 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('Articles 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('Articles 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' => 'article'),
|
||||||
|
'capability_type' => 'post',
|
||||||
|
'has_archive' => true,
|
||||||
|
'hierarchical' => false,
|
||||||
|
'menu_position' => null,
|
||||||
|
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
|
||||||
|
);
|
||||||
|
|
||||||
|
register_post_type('article', $args);
|
||||||
|
}
|
||||||
|
|
||||||
|
add_action('init', 'create_article_post_type');
|
||||||
|
|
||||||
|
function add_article_meta_boxes()
|
||||||
|
{
|
||||||
|
add_meta_box(
|
||||||
|
'article_authors',
|
||||||
|
__('Article Authors', 'duck-behavior-journal'),
|
||||||
|
'render_article_authors_meta_box',
|
||||||
|
'article',
|
||||||
|
'side',
|
||||||
|
'default'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
add_action('add_meta_boxes', 'add_article_meta_boxes');
|
||||||
|
|
||||||
|
function render_article_authors_meta_box($post)
|
||||||
|
{
|
||||||
|
$authors = get_post_meta($post->ID, 'article_authors', true);
|
||||||
|
?>
|
||||||
|
<div id="article-authors">
|
||||||
|
<div id="article-authors-list">
|
||||||
|
<?php
|
||||||
|
if (!empty($authors)) {
|
||||||
|
foreach ($authors as $author) {
|
||||||
|
?>
|
||||||
|
<p><input type="text" name="article_authors[]" value="<?php echo esc_attr($author); ?>" /></p>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
?>
|
||||||
|
<p><input type="text" name="article_authors[]" value="" /></p>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</div>
|
||||||
|
<button type="button" id="add-author-button" class="button"><?php _e('Add Author', 'duck-behavior-journal'); ?></button>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
jQuery(document).ready(function($) {
|
||||||
|
$('#add-author-button').on('click', function() {
|
||||||
|
$('#article-authors-list').append('<p><input type="text" name="article_authors[]" value="" /></p>');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
|
function save_article_authors_meta_box($post_id)
|
||||||
|
{
|
||||||
|
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
|
||||||
|
if (!isset($_POST['article_authors'])) return;
|
||||||
|
|
||||||
|
$authors = array_map('sanitize_text_field', $_POST['article_authors']);
|
||||||
|
update_post_meta($post_id, 'article_authors', $authors);
|
||||||
|
}
|
||||||
|
add_action('save_post', 'save_article_authors_meta_box');
|
||||||
|
|
16
index.php
16
index.php
|
@ -15,7 +15,7 @@
|
||||||
<h2 class="text-center mb-4">Featured Articles</h2>
|
<h2 class="text-center mb-4">Featured Articles</h2>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<?php
|
<?php
|
||||||
$args = array('post_type' => 'post', 'posts_per_page' => 3);
|
$args = array('post_type' => 'article', 'posts_per_page' => 3);
|
||||||
$loop = new WP_Query($args);
|
$loop = new WP_Query($args);
|
||||||
while ($loop->have_posts()) : $loop->the_post();
|
while ($loop->have_posts()) : $loop->the_post();
|
||||||
?>
|
?>
|
||||||
|
@ -26,7 +26,19 @@
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h5 class="card-title"><?php the_title(); ?></h5>
|
<h5 class="card-title"><?php the_title(); ?></h5>
|
||||||
<p class="card-text"><?php the_author(); ?> | <?php the_date(); ?></p>
|
<p class="card-text">
|
||||||
|
<?php
|
||||||
|
$authors = get_post_meta(get_the_ID(), 'article_authors', true);
|
||||||
|
if (!empty($authors)) {
|
||||||
|
echo esc_html($authors[0]);
|
||||||
|
if (count($authors) > 1) {
|
||||||
|
echo ' et al.';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
the_author();
|
||||||
|
}
|
||||||
|
?> | <?php the_date(); ?>
|
||||||
|
</p>
|
||||||
<a href="<?php the_permalink(); ?>" class="btn btn-outline-primary">Read More</a>
|
<a href="<?php the_permalink(); ?>" class="btn btn-outline-primary">Read More</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
29
single-article.php
Normal file
29
single-article.php
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<?php get_header(); ?>
|
||||||
|
|
||||||
|
<!-- Article 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>
|
||||||
|
<p class="text-muted">
|
||||||
|
<?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>
|
||||||
|
<?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(); ?>
|
Loading…
Reference in a new issue