Display Date & Author Info on Custom Post Type Archive Page
-
22-04-2021 - |
Frage
I have created a custom post type called 'articles' with all the same capabilities as the general post type & I am using the same template to display both archives.
The blog page that uses the built in posts shows posted on and by,
The custom post type page omits this info
I am lost at this point and don't know what I need to do that's extra, since its using the same code to display both pages.
This is post type function:
function post_type_articles() {
$labels = array(
'name' => _x( 'Articles', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Article', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Articles', 'text_domain' ),
'name_admin_bar' => __( 'Article', 'text_domain' ),
'archives' => __( 'Article Archives', 'text_domain' ),
'attributes' => __( 'Article Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Article', 'text_domain' ),
'all_items' => __( 'All Articles', 'text_domain' ),
'add_new_item' => __( 'Add New Article', 'text_domain' ),
'add_new' => __( 'Add New Article', 'text_domain' ),
'new_item' => __( 'New Article', 'text_domain' ),
'edit_item' => __( 'Edit Article', 'text_domain' ),
'update_item' => __( 'Update Article', 'text_domain' ),
'view_item' => __( 'View Article', 'text_domain' ),
'view_items' => __( 'View Article', 'text_domain' ),
'search_items' => __( 'Search Articles', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into article', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this article', 'text_domain' ),
'items_list' => __( 'Articles list', 'text_domain' ),
'items_list_navigation' => __( 'Articles list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter articles list', 'text_domain' ),
);
$args = array(
'label' => __( 'Article', 'text_domain' ),
'description' => __( 'Article Description', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'rewrite' => array( 'slug' => 'articles' ),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-aside',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => 'articles',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type( 'articles', $args );
} add_action( 'init', 'post_type_articles', 0 );
and here is the loop
<header class="entry-header">
<?php
if ( is_singular() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( '<h2 class="entry-title archive-header"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
endif;
if ( 'post' === get_post_type() ) : ?>
<div class="entry-meta">
<?php factorypress_posted_on(); ?>
</div><!-- .entry-meta -->
<?php
endif; ?>
</header><!-- .entry-header -->
<div class="entry-content">
<div class="row">
<div class="col-sm-3">
<?php the_post_thumbnail('large', array('class' => 'img-responsive')); ?>
</div><!-- /.col-sm-2 -->
<div class="col-sm-9">
<?php
the_content( 'more', sprintf(
wp_kses(
/* translators: %s: Name of current post. Only visible to screen readers */
__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'factorypress' ),
array(
'span' => array(
'class' => array(),
),
)
),
get_the_title()
) );
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'factorypress' ),
'after' => '</div>',
) );
?>
</div><!-- /.col-sm-10 -->
</div><!-- /.row -->
</div><!-- .entry-content -->
<footer class="entry-footer">
<?php factorypress_entry_footer(); ?>
</footer><!-- .entry-footer -->
Thanks in advance if you can answer this for me!
Lösung
If you're using a custom archive template I think you should add the conditional is_post_type_archive()
if ( 'post' === get_post_type() || is_post_type_archive( array( 'articles' ) ) );
Andere Tipps
In Twenty Twenty One Theme we can find in inc/template-tags.php:
// Early exit if not a post.
if ( 'post' !== get_post_type() ) {
return;
}
We can override the file to change that code block to:
// Early exit if not a post or article.
if ( 'post' !== get_post_type() && 'articles' !== get_post_type() ) {
return;
}
This specific line in your code prevents the execution of the factorypress_posted_on
method that displays the post metadata.
if ( 'post' === get_post_type() ) : ?>
That only validates if the post type is post not your custom post type articles. I think you should replace that particular line of code with this one:
if ( 'post' === get_post_type() || 'articles' === get_post_type()) : ?>