• Hi,

    I created a custom post template for a custom post type (CPT). I named it after my CPT e.g. single-cpt.php and copied the the content of single.php. It works so far.

    Since Astra is doing the whole layout of the post in astra_content_loop() – which is defined in other files – I’m adding my custom HTML via the astra_entry_content_before hook, see:

    get_header(); ?>
    
    <?php
    add_action( 'astra_entry_content_before', 'add_cpt_content' );
    function add_cpt_content() { ?>
        <p>My custom theme HTML</p>	
    <?php } ?>
    
    <?php if ( astra_page_layout() == 'left-sidebar' ) : ?>
    	<?php get_sidebar(); ?>
    <?php endif ?>
    
    	<div id="primary" <?php astra_primary_class(); ?>>
    		<?php astra_primary_content_top(); ?>
    		<?php astra_content_loop(); ?>
    		<?php astra_primary_content_bottom(); ?>
    	</div>
    
    <?php if ( astra_page_layout() == 'right-sidebar' ) : ?>
    	<?php get_sidebar(); ?>
    <?php endif ?>
    
    <?php get_footer(); ?>

    Is there a better way to create custom post templates where I can edit what happens in astra_content_loop()?

    The workaround with the hook is somewhat an ugly hack.

    • This topic was modified 5 years, 3 months ago by nicoter.
Viewing 2 replies - 31 through 32 (of 32 total)
  • Hey @caspervoogt , thanks for your feedback. I use my approach continuously in several projects and just double checked it using WP 5.9.3 and Astra pro 3.6.5 and 3.6.7 and it still works fine.
    Using a template single-xxx.php in child theme folder is not an Astra feature but WordPress standard behaviour which seems not to be overridden by Astra. So I assume it does not depend on the specific Astra version or pro vs. free.

    But I generally use Gutenberg and no page builder like Elementor, so I can not tell if there might be challenges with the page builders.

    In the past sometimes I struggled with naming the template correctly. In my example my custom post type is called “mycpt”, defined i.e. in a plugin with
    register_post_type( ‘mycpt’, $args );
    For this the template has to be named single-mycpt.php in the child theme folder. For a custom post type “my_cpt” the template is single-my_cpt.php and so on.

    As the single-xxx.php is easy to create, customizing the loop needs more effort, using remove_action and add_action. The solution proposed by @vyshnav4u has a approach not so far from my snippets but is differently implemented.

    Apart from this I am sorry that my proposal could not help you.

    Hi!

    While we are waiting for official documentation, may I have a little help?
    I built a custom post type, using CPT UI and ACF.
    I copied the single.php file and renamed it single-mycpt.php, added the code provided by Tanitat, and uploaded in my child theme. it worked so far.
    Now I wonder how I should do to display my custom fields?
    I guess I have to use the ACF function the_field(). Should I create another file for this?
    Thanks for your precious help, I didn’t use CPT UI for a while and not with Astra theme.

    • This reply was modified 2 years, 5 months ago by diallyprod.
Viewing 2 replies - 31 through 32 (of 32 total)
  • The topic ‘Custom post template?’ is closed to new replies.