Showing Sections

Cuisine Sections doesn’t do stuff automatically when it comes to content. That’s because it was written for developers and we know you guys and gals want to be able to put sections wherever you wish.

You can make it as easy and as hard as you want.


Calling the_sections()

In line with WordPress’ default template tags we’ve added a few of our own. Although it pains us to do so and not work completely OOP, we understand that this fits in a bit more with WordPress’ way of working.

To display all sections in the right order you can just call the_sections();

This echoes all output. You can also fetch the html, for late use with: get_sections();

Checking to see wether or not a page or post even has sections you can use: has_sections();

If you have access to a Section object, you can also show it’s columns with template tags: get_columns( $section );

The $section variable in this example needs to be a valid instance of a ChefSections\SectionTypes-class. You can check this by seeinf if it implements a ChefSections\Contracts\Section interface.


Showing a single section

You can also load any section you want from anywhere in your theme with get_section(). The function takes the following parameters:

  • $postId -> the post id where this section is set
  • $sectionId -> the id of the section you’re loading
  • $path -> path to a custom template (optional)

Here’s an example:

echo get_section( 2, 1 ); //load the first section from the second post, 

echo get_section( 2, 1, '/sections/custom.php' ); //the same as above, but use the custom.php file in my theme as it's template.

Extending the Walker

All of the template-tag examples above use the CuisineSections\Front\Walker class to display sections, columns and section-templates.

This walker can, obviously be completely extended to suit your own needs. The Walker class relies heavily on an extended Cuisine Collection called ChefSections\Collections\SectionCollection, which you are also free to extend.

Although the walker class is free to be extended, some methods need to be still callable:

walk();

The walk() method loops through all found sections and displays them, triggering the Sections and Columns beforeTemplate() and afterTemplate() in the process. These methods add flexibility and provide you with the options to, for example, register custom scripts or add extra html.

Here’s the default walk() method, as it appears in the Walker class:

public function walk(){

    ob_start();

    foreach( $this->collection->getNonContainered() as $section ){

        $section->beforeTemplate();

            Template::section( $section )->display();

        $section->afterTemplate();
    }

    //reset post-data, to be sure:
    wp_reset_postdata();
    wp_reset_query();


    return apply_filters( 'cuisine_sections_output', ob_get_clean(), $this );
}

As you can see the complete output is eventually filterable through cuisine_sections_output.


setCollection();

The default setCollection method populates a protected $collection variable in the Walker class. You can change the outcome of this function (which -by default- uses a post-id to get all the sections associated with a post), as long as the output is a version of the SectionCollection class.