• Resolved valeriaglkvch

    (@valeriaglkvch)


    Hi
    I have some trouble with generating AMP content using custom folder for AMP templates.

    So,
    I have a folder “amp” and this code
    add_theme_support( ‘amp’, array(
    ‘template_dir’ => ‘amp’,
    ));

    I don`t understand how I should code the template (for example single.php) in order to get amp compatible post content.

    in the header.php in folder amp I added

    <?php global $post;
    $amp_post = new AMP_Post_Template($post);
    ?>

    single.php looks like below:

    <?php include ‘header.php’; ?>

    <div class=”amp-container”>
    <main class=”main-amp-content”>
    <article class=”amp-wp-article”>
    <header class=”amp-wp-article-header”>
    <h1 class=”amp-wp-title”><?php echo esc_html( $amp_post->get( ‘post_title’ ) ); ?></h1>

    </header>
    <?php
    $featured_image = $amp_post->get( ‘featured_image’ );

    if ( empty( $featured_image ) ) {
    return;
    }

    $amp_html = $featured_image[‘amp_html’];
    $caption = $featured_image[‘caption’];
    ?>
    <figure class=”amp-wp-article-featured-image wp-caption”>
    <?php echo $amp_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
    <?php if ( $caption ) : ?>
    <p class=”wp-caption-text”>
    <?php echo wp_kses_data( $caption ); ?>
    </p>
    <?php endif; ?>
    </figure>

    <div class=”amp-wp-article-content”>
    <?php echo $amp_post->get( ‘post_amp_content’ );?>
    </div>

    <footer class=”amp-wp-article-footer”>
    <?php $amp_post->load_parts( apply_filters( ‘amp_post_article_footer_meta’, array( ‘meta-taxonomy’, ‘meta-comments-link’ ) ) ); ?>
    </footer>
    </article>
    </main>

    </div>

    <?php include ‘footer.php’; ?>

    But images, iframes of my content are rendered incorrectly, they are just lost instead of being as amp-img and amp-iframe.

    Can you please desribe me the way I should code the templates in my custon folder for AMP?

    Thanks in advance!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    I believe the problem is that you are adding theme support but you intend to use Reader mode. Theme support is only relevant for Native/Transitional modes. So you can remove the add_theme_support() call, and the default directory for templates is amp/, so you don’t need to define it. As for the template itself, you should copy the template you want to modify from the plugin’s templates directory, and put the modified version in your theme.

    Thread Starter valeriaglkvch

    (@valeriaglkvch)

    I use Transitional mode.

    I need to do the whole site as AMP (normal site + AMP) that`s why I create folder for AMP templates.

    If I remove the function add_theme_support() The plugin uses my normal single.php file.

    “As for the template itself, you should copy the template you want to modify from the plugin’s templates directory, and put the modified version in your theme.”

    – In plugin folder I have templates only for single post and page but I need custom amp templates for homepage, category page, so all pages – the whole site should have AMP version.

    Thread Starter valeriaglkvch

    (@valeriaglkvch)

    Hi, what about my issue?

    Plugin Author Weston Ruter

    (@westonruter)

    If using Transitional mode, then you should not be using AMP_Post_Template. You should instead just write templates like you do normally in WordPress (e.g. using template tags).

    So, for example, you should be able to copy your theme’s normal single.php into amp/single.php and re-add this code:

    add_theme_support( 'amp', array(
        'template_dir' => 'amp',
    ));

    And then the changes you make to amp/single.php should only be served in the AMP version.

    You can read more about this under “Paired Mode” in https://amp-wp.org/documentation/how-the-plugin-works/amp-plugin-serving-strategies/

    @valeriaglkvch Did you manage to get your custom AMP templates working? As Weston mentioned you can copy your files the amp/ directory and add the theme support code before editing.

    Thread Starter valeriaglkvch

    (@valeriaglkvch)

    Yes, I did. It is not actual any more. Thanks.

    Hi all,

    I have a similar issue…
    I’ve tried to pull amp templates from another directory in another plugin I’ve just created, giving a full path to it, but it didn’t work…
    I’ve also setted paired flag in config page (and in the code too…) as you can see in the following code, wrapped in my custom plugin function:

    public static function register_template_dir() {
            // set custom template's dir
            add_theme_support( 'amp', array(
                'paired' => true,
                'template_dir' => self::$plugin_fs_dir. 'amp-templates',
               ),
            ) );
            
        }
    

    This function is loaded when “after_setup_theme” hook is fired by calling

    add_action( 'after_setup_theme', array( 'myplugin', 'register_template_dir'), 6 );
    

    Is it possible to give a full path or custom templates have to be only putted in AMP plugin relative dir?
    My current version of AMP plugin is 1.2.2 and I found amp original templates are in
    \wp-content\plugins\amp\templates

    So, my next question, in case is not possible to give a path outside AMP plugin, is where exactly I have to put my custom templates?

    Thamks in advance for help.
    Nick

    Plugin Author Weston Ruter

    (@westonruter)

    @walkstudio Please open a new support topic with your question. Thanks.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Custom folder for AMP templates’ is closed to new replies.