• 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 15 replies - 1 through 15 (of 32 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() ) {
            return;
        }
    
        ?>
        <!-- Your custom HTML markup here -->
        <p>My custom theme HTML</p>	
    
        <?php
    }
    
    add_action( 'astra_entry_content_before', 'add_cpt_content' );
    
    Thread Starter nicoter

    (@nicoter)

    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()?

    Thanks

    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 5 years, 3 months 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?

    Hello @sloba88,

    You can override the template from the wp-content\themes\astra\template-parts\blog\blog-layout.php file into the Child Theme and can make customizations as per your requirement.

    If you are wondering how to make a child theme, then please refer to this article – https://wpastra.com/docs/install-astra-child-theme/

    Regards,
    Sweta

    hi,

    did you ever write this blog post? I’m am having trouble creating custom post templates and would like to read what you have documented.

    thanks

    Hello @bluemuse

    Not yet, we haven’t yet worked on this requirement.

    We are caught up with some other urgent tasks, if possible we shall work on the same soon.

    Regards,
    Suman

    Astra is such an awesome theme, but the support/documentation for CPTs is surprisingly thin.

    My use case: what is the correct way to create a custom loop for the single page of a custom post type?

    I can of course duplicate single-layout.php from template-parts/single, but that overrides all singles. I want to create a custom single-layout.php for just a specific custom post type. I cannot figure out a non-hacky way of doing it.

    Suggestions?

    Hello @watermelonkid

    Thank you for the feedback!

    I have reached out to the concerned team so that we can add the necessary documents on this topic as well.

    While for the custom requirements. The WordPress Template Hierarchy will help you out.

    Do refer to this screenshot, on which template page needs to be created. And for more information here you can refer to this WP article.

    I hope that helps.

    Regards,
    Suman

    I am facing the same problem. Customizing the template for CPTs is not a very easy task on the topic. Searching on google I got here thinking there was a solution.
    @watermelonkid Astra is such an awesome theme, but the support/documentation for CPTs is surprisingly thin.

    My use case: what is the correct way to create a custom loop for the single page of a custom post type?

    I think the same thing about Astra.

    And there’s a detail I have a premium theme. I opened a ticket but I still haven’t had help with this issue.

    Hello @vetorbox

    Sorry for the experience.

    I can see my colleague has responded to your query, and have also forwarded the documentation request to the concerned team.

    We shall try and share the same soon by end of this month.

    Do let us know if there’s anything else we can help you with.

    Have a nice day and week ahead. Stay safe! ??

    Hello @about2press

    Sorry, for the delay as we have some major updates coming up very soon. Which caused the delay with some documentations.

    I will curate a quick list with the above articles in them and it can be found in the Knowledgebase very soon by this week.

    Thanks for sharing the same! ??

    Regards,
    Suman

Viewing 15 replies - 1 through 15 (of 32 total)
  • The topic ‘Custom post template?’ is closed to new replies.