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(); ?>
    
    <?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 2 years, 11 months ago by nicoter.
Viewing 15 replies - 16 through 30 (of 32 total)
  • So this is how I got along while migrating a wordpress site with several custom post types to Astra w. Gutenberg.
    Finally using hooks is the way I had to go, but I did not want to mess up my functions.php with tons of HTML-markup and a bunch of logic my cpts deal with. So I put the hooks and logic into template files.

    I created 3 files for a cpt named “mycpt” in my child theme’s folder – you might not neeed all 3 of them:

    • single-mycpt.php as a copy of single.php
    • template-parts/content-mycpt.php as a copy of template-parts/content.php
    • template-parts/single/single-mycpt-layout.php as a copy of template-parts/single/single-layout.php

    single-mycpt.php will be used automatically for mycpt, as has been confirmed by other developers above.

    Inside single-mycpt.php I add the following code to invoke the template parts (i.e. next line after get_header(); ):

    /* define template for single custom post type mycpt */
    remove_action( 'astra_template_parts_content', array( Astra_Loop::get_instance(), 'template_parts_post' ) );
    add_action( 'astra_template_parts_content', 'content_mycpt_single_template' );
    
    function content_mycpt_single_template() {
        get_template_part( 'template-parts/content', 'mycpt' );
    }
    
    /* define template for content loop custom post type mycpt */
    remove_action( 'astra_entry_content_single', 'astra_entry_content_single_template' );
    add_action( 'astra_entry_content_single', 'astra_entry_content_single_mycpt_template' );
    
    function astra_entry_content_single_mycpt_template() {
        get_template_part( 'template-parts/single/single-mycpt-layout' );
    }

    Now I can customize the templates, add some logic, styling, deal with different post formats etc.
    If possible I stick to using hooks i.e.
    <?php add_action( 'astra_entry_content_after', 'mycpt_content_bottom' ); ?>
    and place this code into one of my templates. So all of my customization for a cpt can be found in the 3 template files which seems to be a tidy solution to me.

    This is for single view only, don’t know yet how to manage the archives.

    Any update on creating custom post templates using Astra without using Elementor? : )

    Hi @blitzmr2,

    Our content team is preparing and lining this up. Stay tuned.

    Kind regards,
    Herman πŸ™‚

    @brainstormteam @bsfherman Any updates? Thanks!

    Hi @lbell,

    We’re sorry, our documentation team is quite busy with some other priority documentations. They already have this on their to-do list. Trust me! οΏ½?

    Kind regards,
    Herman πŸ™‚

    That would interest me too.
    Toolset is out of the question for me.

    Are there any updates yet?
    :>

    Hi @twistedde,

    This is still in the line of our documentation to-do list. You can always head over to our Knowledge Base and/or our Blog anytime to check whether we have published it or not.

    Kind regards,
    Herman πŸ™‚

    theguitarlesson

    (@theguitarlesson)

    Hi,
    This thread was opened almost 2 years ago.

    Being able to use our own templates for Custom Post Types is important!

    Please give us detailed info on this ASAP!

    By this, I mean exact code snippets that we can use to filter astra_content_loop() or whatever functions output post content.

    Thanks
    Tom

    pikamander2

    (@pikamander2)

    @brainstormteam – Any updates on this?

    We’re sorry for the delay.

    Our documentation team should have this ready within the next few weeks.

    Kind regards,
    Herman πŸ™‚

    Hello,

    Any update on this? I’m in the process of removing Elementor from my site – I need to create a custom post template with Astra.

    Regards,

    Tango.

    @tangolima I have done something like this in functions.php to customize cpt single post page

    add_action( ‘astra_content_loop’, ‘custom_post_type_mycpt’ );
    function custom_post_type_mycpt(){
    if( “mycpt” == get_post_type() ) {
    remove_action( ‘astra_template_parts_content’, array( Astra_Loop::get_instance(), ‘template_parts_default’ ) );
    remove_action( ‘astra_template_parts_content’, array( Astra_Loop::get_instance(), ‘template_parts_post’ ) );

    //Your custom loop for cpt
    }
    }

    • This reply was modified 6 months, 1 week ago by vyshnav4u.

    The last snippet from @vyshnav4u worked for me!

    caspervoogt

    (@caspervoogt)

    @tanitat “single-mycpt.php will be used automatically for mycpt, as has been confirmed by other developers above.” I tried your approach inside my child theme and it had no effect; it looked like a fine approach. I also tried @vyshnav4u’s approach and that also didn’t work.

    What we really need here is official documentation from @brainstormteam. Custom post types are not a small detail. When can we expect documentation, please?

    I think @brainstormteam isn’t too concerned about the customer. Perhaps we should look for another topic that pays more attention to its customers. Customizing the template is the basics, this should be made clear in any documentation

Viewing 15 replies - 16 through 30 (of 32 total)
  • You must be logged in to reply to this topic.