[Resolved] Custom post types: feature images on archive page

Home Forums Support [Resolved] Custom post types: feature images on archive page

Home Forums Support Custom post types: feature images on archive page

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #62575
    Pedro

    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.

    #62630
    Tom
    Lead Developer
    Lead Developer

    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 πŸ™‚

    #62632
    Pedro

    Sorry, I momentarily switched to try stuff out… it’s back on now!

    #62636
    Tom
    Lead Developer
    Lead Developer

    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 πŸ™‚

    #62639
    Pedro

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

    #62641
    Tom
    Lead Developer
    Lead Developer

    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!

    #62648
    Pedro

    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?

    #62684
    Tom
    Lead Developer
    Lead Developer

    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.

    #62697
    Pedro

    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.

    #62740
    Tom
    Lead Developer
    Lead Developer

    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();
    #62836
    Pedro

    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!

    #62844
    Tom
    Lead Developer
    Lead Developer

    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.

    #62846
    Pedro

    No no, everything is working πŸ™‚ thank you

    #62847
    Tom
    Lead Developer
    Lead Developer

    Awesome! You’re welcome πŸ™‚

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.