• Resolved ingridskard

    (@ingridskard)


    Thank you for this plugin, it is exactly what we needed for our project. Unfortunately we’re facing an issue with data context and/or custom templating, and surprisingly I’ve not been able to find others reporting the same on google. Which makes me think/hope we’re just missing something big. Any and all help is greatly appreciated.

    Our setup:
    https://tmp2.shinysticker.io/bla-loger/

    • Tabs each contain an IP block referencing the contents of a specific sub-page
    • Each sub-page contains an IP block referencing a CPT “lodge”, which is just a set of ACFs.
    • This is where we want to use a custom template part. Preferably a template part, as the sub-page will have other content as well.

    Following this tutorial: https://medium.com/@wesmodes/using-wordpress-insert-pages-plugin-with-your-custom-post-types-and-custom-templates-535c141f9635

    We find:

    • The tutorial assumes templates are php files, but they are htmls in our theme “Twenty Twenty-Three”. Do we need to change theme in order for the plugin to find our custom template, or is there some workaround?
    • I created a custom template “single-lodge.html” in the theme’s “parts” directory and added the tutorial’s sample code to it (tweaked* to display a single ACF from the lodge in context). It showed up under Appearance -> Editor -> Template parts, but the editor just seems to interpret the code as a “Custom HTML” block. The HTML content has been wrapped in a <p> tag, which I can remove, but it then fails on save. Tried converting the block to a “Code” block, but the block fails with an error message and saving fails.

    Please let me know if there is any way we can make “Insert pages” work with custom content templates on full-site editor themes.

    Alternatively we could do away with the CPT, move the ACFs to the sub-pages and view them in the sub-page using ACF Views, but in that case the “content” setting will need to pull ACF data from sub-page context (currently it pulls from page context as per your response here, https://wordpress.org/support/topic/acf-fields-do-reference-the-source-page/)

    TYSM for your time!

    <?php
    /**
    * Template Name: Lodge Template
    * Template Post Type: lodge
    *
    * Template for displaying ACF for a lodge.
    *
    * @package understrap
    */
    // Exit if accessed directly.
    defined( 'ABSPATH' ) || exit;
    ?>
    <article <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <div class="entry-content lodge-content">
    <div class="image">
    <?php $emblem = get_field('emblem'); ?>
    <?php if ( ! empty ( $emblem ) ): ?>
    <img
    src="<?php echo $emblem['url']; ?>"
    alt="<?php echo $emblem['alt']; ?>" />
    <?php endif; ?>
    </div> <!-- .image -->
    </div><!-- .entry-content.lodge-content -->
    </article><!-- #post-## -->

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Paul Ryan

    (@figureone)

    Thanks so much for the detailed notes. You’re correct, we haven’t yet enabled support for the new block themes that have html templates instead of php templates.

    https://developer.wordpress.org/themes/block-themes/

    We’ll start work on that! In the meantime, you should be able to put a php template in your block theme (following the instructions from the medium article). It won’t show up in the Theme Editor, but you can reference it in the Insert Pages block.

    For example, if you save your “Lodge Template” above as wp-content/themes/twentytwentythree/templates/lodge-template.php, then in the Insert Pages block under Settings you can specify Display = Use a custom template and Custom Template Filename = templates/lodge-template.php

    Let us know if that ends up working. Thanks!

    Thread Starter ingridskard

    (@ingridskard)

    Hi, thanks for getting back to me so quick and for your suggestion! It works – the plugin finds the template now, and the template can fetch all kinds of fields from ACF using get_field, exactly as needed.

    However as it turns out, get_field only works if I send in post id, which completely defeats the purpose of templating… And it is not needed in the tutorial example code.

    Code below is our currently working template. Ie. if I remove the id argument (1025) it outputs “Emblem is empty!”

    Can you see any obvious cause for this behavior? The current context is we’re now templating a page-inserted CPT into a sub-page which is page-inserted into those tabs.

    Can we pull id from context somehow, or is there some implicit context that is confused by our setup?

    Thanks for taking a look!

    <?php
    /**
     * Template Name: Lodge Template
     * Template Post Type: lodge
     *
     * Template for displaying ACF for a lodge.
     *
     * @package understrap
     */
    // Exit if accessed directly.
    defined( 'ABSPATH' ) || exit;
    ?>
    <article <?php post_class(); ?> id="post-<?php the_ID(); ?>">
        <div class="entry-content lodge-content">
            <div>
                <?php $emblem = get_field('emblem', 1025); ?>
                <?php if ( empty ( $emblem ) ): ?>
                    Emblem is empty!
                <?php else: ?>
                    Emblem: <img 
                    src="<?php echo $emblem['url']; ?>" 
                    alt="<?php echo $emblem['alt']; ?>" />
                <?php endif; ?>
            </div> <!-- .image -->
        </div><!-- .entry-content.lodge-content -->
    </article><!-- #post-## -->
    Thread Starter ingridskard

    (@ingridskard)

    Tried echoing $post->ID and it outputs the ID of sub-page, not the CPT, which explains why it doesn’t target anything.

    How can I make the template detect the CPT so I can send in the correct ID?

    Thread Starter ingridskard

    (@ingridskard)

    Tested some more and it turns out the template outputs page-ID on tabbed page, sub-page ID on sub-page and CPT ID on the CPT page.

    Plugin Author Paul Ryan

    (@figureone)

    Ah I think I understand, in your inserted CPT, you are trying to fetch an ACF value from the parent page that inserted it, not from the CPT page itself.

    If that’s the case you can access Insert Page’s “stack” of inserted page IDs (we use this internally to catch any possible loops of inserted pages, such as A inserts B inserts C inserts A, which would lead to an infinite loop of inserts). The last item in the stack is the page currently being inserted, and should match the ID returned by get_the_ID() in your template. The second-to-last item in the stack should be the ID of the parent page that inserted the current page. So something like this should work:

    $stack = InsertPagesPlugin::get_instance()->inserted_page_ids;
    $current_page_id = end( $stack );
    $parent_page_id  = prev( $stack );
    $emblem          = get_field( 'emblem', $parent_page_id );

    You should probably check whether $parent_page_id is empty before referencing it, since this code in your template might get called in a context where there is no parent page.

    Thread Starter ingridskard

    (@ingridskard)

    Fantastic, thank you so much. That did the trick. Turns out my CPT is always at the end of the stack, which lets the template access and output it correctly in any context 🥳

    • This reply was modified 1 year, 4 months ago by ingridskard.
    Plugin Author Paul Ryan

    (@figureone)

    Great! Best of luck with the rest of the site

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom templating with full-site editor themes?’ is closed to new replies.