Adding and Ordering Sections on the Listings Page

I’m trying to add additional sections to the Listings page. I like the default layout and explored templates, but couldn’t seem to find all the widgets to make the listing page identical to the default with the two sections added under description.

I used the following snippet to add a ‘Meet the Team’ section, but if I add another attribute like ‘Accommodations’ then neither display. They see to conflict with one another. Is there a way to combine the attributes into a single block and insert under description?

add_filter(
    'hivepress/v1/templates/listing_view_page/blocks',
    function( $blocks, $template ) {
        $listing = $template->get_context('listing');
        
        if (!$listing) {
            return $blocks;
        }
        
        // Get the team_detail text entered by the host
        $text = $listing->display_team_detail();
        
        if (!$text) {
            return $blocks;
        }
        
        // Create the header for the team_detail section
        $header = '<h3>Meet the Team</h3>';
        
        // Merge the header and the team_details text
        return hivepress()->helper->merge_trees(
            [ 'blocks' => $blocks ],
            [
                'blocks' => [
                    'page_content' => [
                        'blocks' => [
                            'custom_text_above_description' => [
                                'type' => 'content',
                                // Combine header and text
                                'content' => '<p>' . $header . $text . '</p>',
                                '_order' => 70,
                            ],
                        ],
                    ],
                ],
            ]
        )['blocks'];
    },
    1000,
    2
);

If there isn’t a way to do this is there a way I can replicate the listings page as a template, so I can just modify the blocks under description?

The snippet seems to be ok, please send more details about the issue. Do you mean that if you add a second snippet for another attribute then both are hidden, or when you add the attribute itself?

P.S. I recommend using the hivepress()->template->merge_blocks() function instead of merge_trees, it’ll be deprecated soon. A new one is more efficient and you don’t have to set the blocks array item as a wrapper.

If I add a second snippet for another attribute then both are hidden. I’ve set the order of one attribute to 70, and the other to 71. Description appears to be 69 on the page. I’m using the same code above for ‘accommodations’ attribute with the defined attributes swapped out.

I tried using the merge_blocks() function and it causes the text to completely disappear from the page. Here’s the snippet I wrote for inserting both attributes, but nothing shows on the front-end.

add_filter(
    'hivepress/v1/templates/listing_view_page/blocks',
    function( $blocks, $template ) {
        $listing = $template->get_context('listing');

        if ( !$listing ) {
            return $blocks;
        }

        // Get the values for multiple attributes
        $team_text = $listing->display_team_detail();
        $accommodations_text = $listing->display_accommodations(); 

        // Check if either attribute has a value, return the original blocks if neither do
        if ( !$team_text && !$accommodations_text ) {
            return $blocks;
        }

        $combined_content = '';

        // Add the team detail section if it exists
        if ( $team_text ) {
            $combined_content .= '<h3>Meet the Team</h3><p>' . $team_text . '</p>';
        }

        // Add the accommodations section if it exists
        if ( $accommodations_text ) {
            $combined_content .= '<h3>Accommodations</h3><p>' . $accommodations_text . '</p>';
        }

        // Use merge_blocks to insert the content in the desired position
        return hivepress()->template->merge_blocks(
            $blocks,
            [
                [
                    'name'    => 'custom_team_section',
                    'type'    => 'content',
                    'content' => $combined_content,
                    '_order'  => 70, // Adjust the order as needed
                ],
            ],
            'content'
        );
    },
    1000,
    2
);

Hi there, is there someone who can advise on merging blocks for the listing page? I’ve continued to troubleshoot, but still can’t find a solution.

Please try this one instead:

add_filter(
    'hivepress/v1/templates/listing_view_page/blocks',
    function( $blocks, $template ) {
        $listing = $template->get_context('listing');

        if ( !$listing ) {
            return $blocks;
        }

        // Get the values for multiple attributes
        $team_text = $listing->display_team_detail();
        $accommodations_text = $listing->display_accommodations(); 

        // Check if either attribute has a value, return the original blocks if neither do
        if ( !$team_text && !$accommodations_text ) {
            return $blocks;
        }

        $combined_content = '';

        // Add the team detail section if it exists
        if ( $team_text ) {
            $combined_content .= '<h3>Meet the Team</h3><p>' . $team_text . '</p>';
        }

        // Add the accommodations section if it exists
        if ( $accommodations_text ) {
            $combined_content .= '<h3>Accommodations</h3><p>' . $accommodations_text . '</p>';
        }

        // Use merge_blocks to insert the content in the desired position
        return hivepress()->template->merge_blocks(
            $blocks,
            [
                'page_content' => [
                        'blocks' => [
                            'new_custom_block' => [
                                'name'    => 'custom_team_section',
                                'type'    => 'content',
                                'content' => $combined_content,
                                '_order'  => 70, // Adjust the order as needed
                            ],
                        ],
                ],
            ]
        );
    },
    1000,
    2
);

If it doesn’t work I recommend debugging it with var_dump or error_log to check at which point it fails, and if it reaches the merge_blocks part at all.

That worked! Thank you for helping me troubleshoot.