Skip Navigation

[Resolved] Please make Layouts integration's code-change more clear

This thread is resolved. Here is a description of the problem and solution.

Problem: How to integrate layouts in my theme?

Solution: Following this DOC: https://toolset.com/documentation/user-guides/layouts-theme-integration/

There is a also a more advanced possibility which is explained here: https://toolset.com/forums/topic/please-make-layouts-integrations-code-change-more-clear/#post-329505

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 3 replies, has 2 voices.

Last updated by gaborG 6 years, 3 months ago.

Assigned support staff: Beda.

Author
Posts
#329443

I just noticed for about the 8th time trying to integrate Layouts that I was changing the wrong section of the code.
Those who are more prepared in PHP likely never would have made my mistake, but here's the thing...
My code looks like this:

<?php while ( have_posts() ) : the_post(); ?>

				<?php

					/*
					 * Include the Post-Format-specific template for the content.
					 * If you want to override this in a child theme, then include a file
					 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
					 */
					get_template_part( 'template-parts/content', get_post_format() );
				?>

			<?php endwhile; ?>

Now, as someone who can read perfectly but isn't that much at home in coding, this meant that I will remove the entire section starting with the <?. And apparently that was a mistake. I had to remove everything starting from the second line, and change the first line between the PHP tags only.

I've suffered with this s@^t for the past few days. Please help newcommers do this easily by refreshing your code examples the way they are presented in current WP versions, and clearly indicating which part to change.

Thank you.

#329505

Thank you for contacting us here in the Support Forum

I apologize this confusion.

The WordPress standard for a Loop is:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>//start
<?php endwhile; else : ?>
	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>//end

https://codex.wordpress.org/The_Loop

Now, everything inside the Loop like calls to certain template parts, which is a widely used approach but not a default / mandatory part, is related to Themes.

Themes can add templates (in your case, it's using template-parts/content) to display the Loop's content in different manners

Those parts load a template part (other than header, sidebar, footer) into a template. It makes it easy for a theme to reuse sections of code and an easy way for child themes to replace sections of their parent theme.
https://codex.wordpress.org/Function_Reference/get_template_part

Now, as explained here:
https://toolset.com/documentation/user-guides/layouts-theme-integration/
You need to replace everything that is representing the Loop:
This example from a existing Theme index.php:

<?php
/**
* The main template file
*
*/
get_header(); ?>
  
<div id="primary" class="content-area">
  <div id="content" class="site-content" role="main">
    <?php
      if ( have_posts() ) :
      // Start the Loop.
        while ( have_posts() ) : the_post();
           get_template_part( 'content', get_post_format() );
        endwhile;
        else:
           get_template_part ('content', 'none' );
        endif;
     ?>
  </div><!-- #content -->
</div><!-- #primary -->
  
<?php get_footer(); >

Would, after integration, look like this:

<?php
/**
* The main template file
*
*/
get_header(); ?>
  
<div id="primary" class="content-area">
  <div id="content" class="site-content" role="main">
    <?php
      the_ddlayout( 'default-layout' );
    ?>
  </div><!-- #content -->
</div><!-- #primary -->
  
<?php get_footer();

A much nicer approach, unfortunately not documented but really nice if you WANT TO KEEP YOUR THEME's CODE, but also use Layouts at the same time, is this:

Code after integration:

<?php
/**
* The main template file
*
*/
if ( defined( 'WPDDL_VERSION' ) && is_ddlayout_assigned() ) : // if Layouts Plugin is active and a layout is assigned
        get_header( 'layouts' );
        the_ddlayout( 'page-default' ); // Loads 'page-default' layout by default
        get_footer( 'layouts' );
    else: // if not use the normal WP loop
        get_header();?>

<div id="primary" class="content-area">
  <div id="content" class="site-content" role="main">
    <?php
      if ( have_posts() ) :
      // Start the Loop.
        while ( have_posts() ) : the_post();
           get_template_part( 'content', get_post_format() );
        endwhile;
        else:
           get_template_part ('content', 'none' );
        endif;
     ?>
  </div><!-- #content -->
</div><!-- #primary -->

<?php get_footer();
endif; // IF Layouts are enabled

This allows you to either use Layouts OR your Themes default Code.
Means, if your page that is edited in this template is NOT using any Layouts, it will just fallback to the current Theme's code.

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#329506

So you say if I place the entire code in that IF, it works even better. I might do that actually.
Thanks for the info.

#329507

In fact it looks like my previous integration wasn't good either, but now with your IF version I can suddenly see my contents 😀 Thank you again.