0

i'm helping my collegue with his current page and I've stucked with an error:

Undefined variable: alignment

It reffers to the following piece of code:

if( ! function_exists( 'avada_page_title_bar' ) ) {
    /**
     * Render the HTML markup of the page title bar
     * @param  string $title                Main title; page/post title or custom title set by user
     * @param  string $subtitle             Subtitle as custom user setting
     * @param  string $secondary_content    HTML markup of the secondary content; breadcrumbs or search field
     *
     * @return void                         Content is directly echoed
     **/
    function avada_page_title_bar( $title, $subtitle, $secondary_content ) {
        global $smof_data;
        $post_id = get_queried_object_id();

        // Check for the secondary content
        $content_type = 'none';
        if ( strpos( $secondary_content, 'searchform' ) !== FALSE ) {
            $content_type = 'search';
        } elseif ( $secondary_content != '' ) {
            $content_type = 'breadcrumbs';
        }

        // Check the position of page title
        if ( metadata_exists( 'post', $post_id, 'pyre_page_title_text_alignment' ) && 
             get_post_meta( get_queried_object_id(), 'pyre_page_title_text_alignment', TRUE ) != 'default' 
        ) {
            $alignment = get_post_meta( $post_id, 'pyre_page_title_text_alignment', TRUE );
        } elseif ( $smof_data['page_title_alignment'] ) {
            $alignment = $smof_data['page_title_alignment'];
        }

        // Render the page title bar
        echo sprintf( '<div class="fusion-page-title-bar fusion-page-title-bar-%s fusion-page-title-bar-%s">', $content_type, $alignment );
            echo '<div class="fusion-page-title-row">';
                echo '<div class="fusion-page-title-wrapper">';
                    echo '<div class="fusion-page-title-captions">';
                        if( $title ) {
                            // Add entry-title for rich snippets
                            $entry_title_class = '';
                            if ( ! $smof_data['disable_date_rich_snippet_pages'] ) { 
                                $entry_title_class = ' class="entry-title"';
                            }                   
                            echo sprintf( '<h1%s>%s</h1>', $entry_title_class, $title );

                            if ( $subtitle ) {
                                echo sprintf( '<h3>%s</h3>', $subtitle );
                            }

                            if ( get_field( "data_i_miejsce_koncertu" ) && is_single() ) {
                                echo ( '<h3>' . get_field( "data_i_miejsce_koncertu" ) . '</h3>' );
                            }
                        }

                        // Render secondary content on center layout
                        if ( $alignment == 'center') {
                            if ( fusion_get_option( 'page_title_bar_bs', 'page_title_breadcrumbs_search_bar', $post_id ) != 'none' ) {
                                echo '<div class="fusion-page-title-secondary">';
                                    echo $secondary_content;
                                echo '</div>';
                            }
                        }

                    echo '</div>';

                    // Render secondary content on left/right layout
                    if ( $alignment != 'center' ) {
                        if ( fusion_get_option( 'page_title_bar_bs', 'page_title_breadcrumbs_search_bar', $post_id ) != 'none' ) {
                            echo '<div class="fusion-page-title-secondary">';
                                echo $secondary_content;
                            echo '</div>';
                        }
                    }

                echo '</div>';
            echo '</div>';
        echo '</div>';
    }
}

Error is linked with following lines:

echo sprintf( '<div class="fusion-page-title-bar fusion-page-title-bar-%s fusion-page-title-bar-%s">', $content_type, $alignment );

AND

if ( $alignment == 'center') {

AND

            if ( $alignment != 'center' ) {

Why is that? $alignment is defined at the beginning..

thanks!

0

The variable $alignment is only being set conditionally by the following lines:

if ( metadata_exists( 'post', $post_id, 'pyre_page_title_text_alignment' ) && 
             get_post_meta( get_queried_object_id(), 'pyre_page_title_text_alignment', TRUE ) != 'default' 
) {
    $alignment = get_post_meta( $post_id, 'pyre_page_title_text_alignment', TRUE );
} elseif ( $smof_data['page_title_alignment'] ) {
    $alignment = $smof_data['page_title_alignment'];
}

If neither the if() nor the elseif() conditions are met, the variable is never set. Probably there is an initialisation with a default value missing:

$alignment = 'center';

This should be placed above the aforementioned lies of code.

  • hm.. I've checked the core file (full functions.php of the template) and there was no global variable set ;/ and with that it works. When I've copied this specific function to my template - not. – Paweł Skaba Jan 30 '17 at 15:01
  • 1
    Maybe it is defined elsewhere, not in functions.php. Via global $alignment; the defnition could also be buried in some function or even inside a class method. – JHoffmann Jan 30 '17 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.