Published a method to display custom posts (including featured images) in the monthly/daily archives of WordPress

update Last updated: October 19, 2024 at 1:49 PM

From the WordPress plugin "Archives Calendar Widget", we will publish a method to display monthly/daily archives including custom posts.

WordPress standard specifications do not support monthly/daily archive display of custom posts, but this method allows you to display archives including custom posts.

I will explain later, but I will also explain how to modify the program to enable the display of featured images in themes (such as hpb) that do not support featured display on the archive page.

The other day, in the following announcement, by customizing the plugin "WordPress Popular Posts", a menu of 200 popular articles was newly established, and a post calendar including custom posts was implemented using the WordPress plugin "Archives Calendar Widget". reported that

However, due to the specifications of WordPress, I was of the opinion that it was not possible to display the daily archive, but as a result of trying again, I was able to display the daily archive!

The method is shown below for your reference.

Add code to functions.php

In normal WordPress specifications, monthly and daily archives of custom posts cannot be displayed, but by writing the following code in functions.php of the theme, monthly/daily archives of custom posts can be displayed. Become. * In the example below, the 'news' and 'gallery' taxonomies are custom posts.

/* 月別・日別アーカイブにカスタム投稿を含めて表示する
---------------------------------------------------------------- */
function my_pre_get_posts( $query ) {
    if ( ($query->is_month() || $query->is_day()) && $query->is_main_query() ) {
        $query->set( 'post_type', array('post','news', 'gallery') );
    }
}
add_action( 'pre_get_posts', 'my_pre_get_posts' );

function my_getarchives_where( $where ){
    $where = "WHERE";
    $where .= " (post_type = 'post' OR post_type = 'news' OR post_type = 'gallery')";
    $where .= " AND post_status = 'publish'";
    return $where;
}
add_filter( 'getarchives_where', 'my_getarchives_where' );

View custom post archives in the Archives Calendar Widget

Custom post archive display is a WordPress plugin Archives Calendar Widget You can easily do this by installing .
First, add "Archives Calendar Widget" to WordPress plugin, select "Appearance/Widget" from WordPress setting screen, and select custom post (other than post) from "Post type" tab on the above screen. Check.
You will now be able to view your post archives, including custom posts, from the Post Calendar (Archive).

Please note that this plugin currently does not support the WordPress widget block editor implemented in WordPress 5.8, so it must be operated with the plugin "Classic Widgets".

2024.10.19 Added
Due to security issues, this plugin is currently no longer available for download from wordpress.org. Downloads can only be done from github.com below.

View custom post daily archives

A custom post's daily archive can be viewed by clicking on the post's date from the Archives Calendar Widget, as shown below. In addition, the monthly archive will be displayed when you select the year and month title of the posting calendar.

Show featured on archive page (if theme is hpb)

If the feature is not displayed on the archive page, it can be displayed by modifying the theme program.
In this example, it is an example of modifying the program of the homepage builder (hpb) theme program provided by JustSystems.

The display of the archive page is executed in 'content.php' which is called as a template part by the 'get_template_part()' function from archive.php. Therefore, modify this program as follows.

Modifying content.php

Open content.php from the editor, add the marked points (6 lines) as below, update the file, and the featured image will be displayed on the archive page.
In the code below, if you want to display the featured image on the search screen, please delete the description of '!is_search()'.

<?php
/**
 * @package _hpb
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<header class="entry-header">
		<h1 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
	</header><!-- .entry-header -->
	<?php
		// アイキャッチを追加 by Senri on February 19, 2023
		if ( !is_search() && has_post_thumbnail() ) {
			the_post_thumbnail('thumbnail');
		}
	?>
	<?php if ( is_search() ) : // Only display Excerpts for Search ?>
	<div class="entry-summary">
		<?php the_excerpt(); ?>
	</div><!-- .entry-summary -->
	<?php else : ?>
	<div <?php if ( get_post_type() == 'page' ) : ?>id="page-content"<?php endif; ?> class="entry-content">
		<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', '_hpb' ) ); ?>
		<?php
			wp_link_pages( array(
				'before' => '<div class="page-links">' . __( 'Pages:', '_hpb' ),
				'after'  => '</div>',
			) );
		?>
	</div><!-- .entry-content -->
	<?php endif; ?>

	<footer class="entry-meta">
		<?php if ( get_post_type() != 'page' ) : ?>
		<?php hpb_entry_meta( get_post_type() ); ?>
		<?php endif; ?>

		<?php if ( ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) : ?>
		<span class="sep"> | </span>
			<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', '_hpb' ), __( '1 Comment', '_hpb' ), __( '% Comments', '_hpb' ) ); ?></span>
		<?php endif; ?>

		<?php edit_post_link( __( 'Edit', '_hpb' ), '<span class="sep"> | </span><span class="edit-link">', '</span>' ); ?>
	</footer><!-- .entry-meta -->
</article><!-- #post-## -->

Archive page after program modification

After modifying the program of the theme, the archive page will be displayed as follows.

2024.01.17 Added
The following article describes how to display eye-catching images around the article list.

Reference article

Below are links to articles related to custom posts.

Add this entry to the hasebookmark
X (post)

Leave a Reply