Thursday, August 18, 2022
The Great Tit
Updated: 18. 8. 2022, Added: 18. 8. 2022

Display Oxygen Reusable part or template with PHP code

If you want to have your website semantic and use lot of custom code, sometimes you need to add Oxygen part into your custom PHP (for example in code block).

How to display Oxygen Parts with PHP?

Just use this small echo code and change the 478 to your template or reusable part ID.

echo do_oxygen_elements( json_decode( get_post_meta( 478, 'ct_builder_json', true ), true ) );

Example usage

Here is the example of tabs, which shows content of other pages via AJAX. There is a custom sidebar, which is changing by the content in the tab. It's jus example without JavaScript. But you can see, how I use the reusable parts in PHP conditionals.

<a id="popis"></a>
<?php
global $post;

$idstranek = array(
    'post_parent' => get_the_ID(),
);
$stranka = get_children($idstranek);
if (!empty($stranka)) {
    $idotce = get_the_ID();
    $slug_otec = $post->post_name;
    $title_otec = get_the_title();
} else {
    $idotce = wp_get_post_parent_id($post_ID);
    $slug_otec = get_post_field('post_name', $post->post_parent);
    $title_otec = get_the_title($post->post_parent);
}

?>
<div id="ajax-tab-container" class='tab-container obsah'>
    <ul id="child-menu" class='tabs c-child-menu child_menu c-margin-bottom-l'>
        <li class='tab page_item noscroll'><a href="#<?php echo $slug_otec; ?>"><?php echo $title_otec; ?></a></li>
        <?php

        $args = array(
            'post_type'      => 'angebot',
            'posts_per_page' => -1,
            'post_parent'    => $post->ID,
            'no_found_rows'  => 'true',
            'post_status' => 'publish',
            'orderby' => 'date',
            'order' => 'ASC'
        );
        $galerie = "fotogalerie";
        $kalkulator = "preis-und-mengenberechnung";
        $childs = new WP_Query($args);
        if ($childs->have_posts()) : ?>
            <?php while ($childs->have_posts()) : $childs->the_post();
                $slug = $post->post_name;
            ?>
                <?php if ($slug == $kalkulator) : ?>
                    <li class='tab page_item vypocet noscroll'><a href="#formular"><?php the_title(); ?></a></li>
                <?php elseif ($slug == $galerie) : ?>
                    <li class='tab page_item noscroll'><a href="#galerie"><?php the_title(); ?></a></li>
                <?php else : ?>
                    <li class='tab page_item'><a href="<?php the_permalink(); ?> .obsah-wrapper" data-target="#<?php echo $slug; ?>"><?php the_title(); ?></a></li>
                <?php endif; ?>
            <?php endwhile; ?>
        <?php endif;
        wp_reset_postdata(); ?>
    </ul>
    <div class="popis">
        <div class='panel-container'>
            <?php $childs = new WP_Query($args);
            if ($childs->have_posts()) : ?>
                <?php while ($childs->have_posts()) : $childs->the_post();
                    $slug = $post->post_name;
                ?>
                    <?php if ($slug == $kalkulator) : ?>
                        <div id="formular"><?php echo do_shortcode('[oxygen-template id="164"]'); ?></div>
                    <?php elseif ($slug == $galerie) : ?>
                        <div id="galerie"><?php echo do_shortcode('[oxygen-template id="167"]'); ?></div>
                    <?php else : ?>
                        <div id="<?php echo $slug; ?>"></div>
                    <?php endif; ?>
                <?php endwhile; ?>

            <?php endif;
            wp_reset_postdata(); ?>
            <div id="<?php echo $slug_otec; ?>"><?php echo do_shortcode('[oxygen-template id="165"]'); ?></div>
        </div>
    </div>
    <div class="sidebar">
    <?php echo do_shortcode('[oxygen-template id="171"]'); ?>
    </div>

</div>
Did I help you? Support me on Patreon!
Beep this article to your friends!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 comments on “Display Oxygen Reusable part or template with PHP code”

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram