Site logo

Archived topic

Custom post types: feature images on archive page

13 replies · Started by Pedro on January 7, 2015

Viewing posts 1–14 of 14

Hello Tom,

I created a custom post type named 'restaurante'. For some reason, when I access the corresponding archive page (check it here), feature images don't appear.

I think I've customized the theme properly, also enabling "Show" feature images on the Blog add-on.

Any hints? Thanks in advance (and happy 2015)!

Pedro

PS - Also, is there a way to have this custom post archive page as an editable main page for the custom post? E.g. in this case I'd write a some text about restaurants, add some images, etc.

Hi Pedro,

It doesn't look like you're using GeneratePress for this site? The add-ons will only work when using GP.

Let me know :)

Sorry, I momentarily switched to try stuff out... it's back on now!

Ah - I see the issue.

By default, GP only looks for posts in the "post" post type to include the post image.

I think I've made a change that should fix this in the next update - need to do some testing.

Here's what you can do for now..

/**
 * Prints the Post Image to post excerpts
 * Use a custom post type
 */
if ( ! function_exists( 'generate_custom_post_type_post_image' ) ) :
	add_action( 'generate_after_entry_header', 'generate_custom_post_type_post_image' );
	function generate_custom_post_type_post_image()
	{
		
		// If there's no featured image, return
		if ( ! has_post_thumbnail() )
			return;
		
		// If the post type is "restaurante", and we're not on a single template
		if ( 'restaurante' == get_post_type() && !is_single() ) {
		?>
			<div class="post-image">
				<a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a>
			</div>
		<?php
		}
	}
endif;

You can add that snippet of code using a plugin like this: https://wordpress.org/plugins/code-snippets/

Note: If you update to the newest version and you end up with duplicate post images, it means I fixed this issue, and you can remove the above snippet.

Let me know :)

It works like a charm, Tom! Thank you so much.

Glad I could help.

I've fixed this in 1.2.5, so you may experience double featured images when you update.

If that happens, simply remove the snippet I just gave you.

Thanks!

Thanks, I will.

It seems like the same thing is happening to the sidebars, however. Compare: custom post type vs normal post.

I have the theme customized with sidebar-content. Also, the sidebar appearing at the bottom of the custom post isn't the correct one (using inspect element, it's identified as a "right sidebar"). I suppose it's the same issue?

Are you using a custom page template for this post type?

The HTML structure is missing a major component which holds the content:

<div id="primary"...

You can see it (inspect element) on the normal post, but it's not there for the post type.

Sorry, I can't code at all, just go by intuition (but with enough precaution...).

I think I've got it working! Basically copied your single.php to my custom post's php, only replacing what seems to be the content part of the code.

Note: I'm using the WP Editor to do this and it seems to be identifying an error at:

</main><!-- #main -->
</div><!-- #primary -->

That is, </main> and </div> are colored red.

That usually means the opening to those tags have been stripped.

This is what your file should look like:

<?php
/**
 * Your custom page template
 *
 * @package Generate
 */

get_header(); ?>

	<div id="primary" <?php generate_content_class();?>>
		<main id="main" <?php generate_main_class(); ?> itemtype="http://schema.org/Blog" itemscope="itemscope" itemprop="mainContentOfPage" role="main">
		<?php do_action('generate_before_main_content'); ?>
		<?php while ( have_posts() ) : the_post(); ?>

			YOUR CUSTOM STUFF IN HERE

		<?php endwhile; // end of the loop. ?>
		<?php do_action('generate_after_main_content'); ?>
		</main><!-- #main -->
	</div><!-- #primary -->

<?php 
do_action('generate_sidebars');
get_footer();

Yeah, that's what I have except this last part before the end of the loop:

<?php
				// If comments are open or we have at least one comment, load up the comment template
				if ( comments_open() || '0' != get_comments_number() ) : ?>
					<div class="comments-area">
						<?php comments_template(); ?>
					</div>
			<?php endif; ?>

But yeah, that's pretty much what I did :) thanks again, Tom!

And it's still not working?

In that case, you have some broken HTML that you're adding into the middle section.

Make sure you close every <div> you open.

No no, everything is working :) thank you

Awesome! You're welcome :)

This archived topic is closed to new replies.