[Resolved] Necessary changes for custom post type (archive and single)

Home Forums Support [Resolved] Necessary changes for custom post type (archive and single)

Home Forums Support Necessary changes for custom post type (archive and single)

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #254093
    David

    Hello, I’m using GeneratePress for our chess club’s website, and we have one custom post type (called ‘results’, for showing the results from chess tournaments). I was wondering what changes I would need to make in order to do two things (I have created generatepress-child for doing this);

    1) Change the default archive page so that more post results are shown per archive page. At the moment each tournament page/results entry looks very big on the archive page. I know that I can copy the archive.php to archive-results.php and override it, but I’m not sure what to actually change in that file.

    2) Create a custom results page for particular posts (i.e., single-results.php). I have the PHP/HTML code that prints out the relevant parts of the custom post type, but I’m not sure how to set up the single-results.php file to make it work.

    My PHP/HTML skills are quite limited, but given enough examples I can usually sort things out. I had these two things working on a previous template, but I can’t seem to work it out for GeneratePress.

    Thanks!

    #254138
    Tom
    Lead Developer
    Lead Developer

    Hi David,

    1. Typically this is set in “Settings > Reading”, however I’m assuming you you need a different value set just for this post type?

    2. I think if you need a different template for a specific (single) post, you would need to use page/post templates: https://developer.wordpress.org/themes/template-files-section/page-templates/#creating-page-templates-for-specific-post-types

    Let me know if you need more info πŸ™‚

    #254257
    David

    I’m not sure I explained that very well πŸ™‚ The view of the archive page looks like this, and I was wondering if I could change the visual output of the post listing (and also change the “Archives” heading)? I thought I had to do that in a file called archive-results.php (specific to the custom post type “results”).

    The second part is related, but in this case I can’t find where to change the content in single.php (which I would copy to single-results.php). I’ll post the normal GeneratePress single.php code here;

    <?php
    /**
     * The Template for displaying all single posts.
     *
     * @package GeneratePress
     */
     
    // No direct access, please
    if ( ! defined( 'ABSPATH' ) ) exit;
    
    get_header(); ?>
    
    	<div id="primary" <?php generate_content_class();?>>
    		<main id="main" <?php generate_main_class(); ?>>
    		<?php do_action('generate_before_main_content'); ?>
    		<?php while ( have_posts() ) : the_post(); ?>
    
    			<?php get_template_part( 'content', 'single' ); ?>
    
    			<?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; ?>
    
    		<?php endwhile; // end of the loop. ?>
    		<?php do_action('generate_after_main_content'); ?>
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php 
    do_action('generate_sidebars');
    get_footer();

    Where in that code would I make changes to alter the body content of posts? For example, on this page it shows that I added; <h2>Tournament results are currently being fixed; back soon.</h2> to single-results.php, but it doesn’t appear in the post body; instead it’s outside that. So I’m not sure how to edit the template body content of custom “results” pages.

    #254379
    Tom
    Lead Developer
    Lead Developer

    Ah, I understand.

    You’re right, you should be able to just copy archive.php, add it to your child theme and then name it: https://codex.wordpress.org/Post_Type_Templates

    As for the content, look at this line: <?php get_template_part( 'content', 'single' ); ?>

    It’s looking for a file named: content-single.php.

    So you could do: <?php get_template_part( 'content', 'results' ); ?>

    The copy content-single.php, name it content-results.php and make your changes.

    Hopefully this makes more sense πŸ™‚

    #254488
    David

    That’s exactly the part that I couldn’t work out, but it’s sorted now, thank you!

    #254527
    Tom
    Lead Developer
    Lead Developer
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.