Aleš Sýkora / May 29, 2024 / 4 comments
Display Oxygen Reusable part or template with PHP code
min read / Custom Code, Oxygen Builder, Plugins, WordPress / Share on: Twitter, LinkedIn, Facebook
Post summary: 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. UPDATE…
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.
UPDATE 29.5.2024 – since Oxygen 4.8.3, the ct_builder_json needs to be prefixed with _ or it doesn’t work anymore.
_ct_builder_json
Updated code:
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>
Fuel my passion for writing with a beer🍺
Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻
Kaspars Folkmanis
Hi! How’s it possible to pass an additional parameter?
For example I want to render an Oxygen template ID=100 for the post ID=2.
So Oxygen template is for posts. And so then this would display that template with ID=100 with the content of the post of ID=2.
How would I be able to achieve this?
Thanks!
Aleš Sýkora
Hello Kaspars, that’s not possible. Try contact Oxygen support.
Sergio
Hi Aleš, in my case doesn’t work.
I open a thread here: https://github.com/soflyy/oxygen-community/discussions/242
Aleš Sýkora
Have you tried to add the code without if have_posts? Just calling the reusable part?