Support » Theme: Astra » Custom post template?

  • 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(); ?>
    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(); ?>
    <?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 3 months, 1 week ago by nicoter.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello,

    astra_content_loop() allows you to change markup of a page without overriding the templates from the theme.

    The snippet that you are adding is correct – To improve this you can just add this action to functions.php and not create a separate template for the custom post type.

    Having code added from actions/filters makes it easier for us to maintain backward compatibility. For eg, if we change the content of any template in the future you will not need to manually keep the templates updated every time we make changes.

    Here is what you can add to the functions.php file –

    function add_cpt_content() {
        // bail if this current post type is different.
        if ( 'cpt' !== get_post_type() ) {
        <!-- Your custom HTML markup here -->
        <p>My custom theme HTML</p>	
    add_action( 'astra_entry_content_before', 'add_cpt_content' );

    Hi Nikhil,

    Thanks for the detailed answer and the better solution without custom template.

    How can I change markup of a page without overriding the templates by using astra_content_loop()?


    I have an open topic with the same question. Looks like we were ignored …

    We will be writing a detailed blog post regarding this.

    Until then, I do have an example to share how this can be used –

    For Elementor Pro’s theme builder compatibility, we need to remove theme’s templates and allow elementor to display it’s content in place of Astra’s templates. This is done in the Elementor Pro compatibility class

    For eg – In case of a 404 page, we remove our template by calling remove_action and insert Elementor’s 404 layout, code here.

    Similar goes for archive layout, Theme’s templates can be conditionally disabled by removing it’s actions like this


    @keepact, @nicoter – Can you give your use cases for overriding the templates so that I can include these in a documentation post?

    • This reply was modified 2 months, 3 weeks ago by Nikhil Chavan.

    Thank you so much for returning.

    I imagined cases of removing the <article> tag from pages, putting id in the header for javascript and for blogging to change the order of things.

    For example, on the search page, I had to make Grid combinations and create orders to be able to use existing markup and not create another template. Incorporating this into a simple example in your Doc would be great!

    Hi everyone, I have a similar question. I use custom post template, and I would like to use on my custom “single-myposts”, only Comment section (“Allow Comments []”) from <?php astra_content_loop(); ?> and nothing more, how can i do it?

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