• Resolved stuartcr

    (@stuartcr)


    Hi all!!

    i’m quite new to creating my own functions….

    I’m struggling to understand why my function “serviced_office_title” does not display within the <h1 class=”trail-title”></h1>, the function works but is above the H1 tag.

    my assumption was if i replaced “$flash_header_title” with my function
    i.e $flash_header_title = serviced_office_title();

    then whatever “serviced_office_title()” creates would replace “$flash_header_title”

    echo ‘<h1 class=”trail-title”>’;
    echo $flash_header_title; <<< my title here???
    echo ‘</h1>’;

    this is what happens – not inside the H1 tag

    <div class="tg-container">
    Bishops Home Builders - 2A
    <h1 class="trail-title"></h1>
    <ul class="trail-items"><li class="trail-item trail-begin"><a class="trail-home" href="" title="Home"><span>Home</span></a></li><li class="trail-item"><a class="item-custom-post-type" href="" title="Serviced Offices"><span>Serviced Offices</span></a></li><li class="trail-item"><span>Bishops Home Builders</span></li></ul></div>

    where am i going wrong? have i made the wrong assumption?? can anyone help?

    if ( ! function_exists( 'flash_page_title' ) ) :
    	/**
    	 * Title for page header
    	 *
    	 * @since Flash 1.0
    	 */
    	function flash_page_title() {
    		if ( is_archive() ) {
    			$flash_header_title = get_the_archive_title();
    		} elseif ( is_404() ) {
    			$flash_header_title = esc_html__( 'Page NOT Found', 'flash' );
    		} elseif ( is_search() ) {
    			$flash_header_title = sprintf( esc_html__( 'Search Results for: %s', 'flash' ), esc_html( get_search_query() ) );
    		}elseif ( is_singular('serviced-office')) {
            $flash_header_title = serviced_office_title();
            } elseif ( is_singular() ) {
    			$flash_header_title = get_the_title();
    		} elseif ( is_home() ) {
    			$queried_id         = get_option( 'page_for_posts' );
    			$flash_header_title = get_the_title( $queried_id );
    		} 
            else {
    			$flash_header_title = '';
    		}
    
    		if ( flash_is_woocommerce_page() ) {
    			$flash_header_title = woocommerce_page_title( $echo = false );
    		}
    
    		echo '<h1 class="trail-title">';
    		echo $flash_header_title;
    		echo '</h1>';
    	}
    endif;
    
    if ( ! function_exists( 'serviced_office_title' ) ) :
    	/**
    	 * Title for page header
    	 *
    	 * @since Flash 1.0
    	 */
    	function serviced_office_title() {
            if( get_field('is_this_office_available')=='yes' ){ 
            echo the_field('office_number');
    		}
            elseif( get_field('is_this_office_available')=='no' ){
    		echo get_the_title();
    		echo ' - ';
            echo the_field('office_number');
    		}   
    	}
    endif;
Viewing 5 replies - 1 through 5 (of 5 total)
  • Dion

    (@diondesigns)

    Your custom function must return the data, not echo it. For example, return the_field('office_number'); instead of echo the_field('office_number');.

    Thread Starter stuartcr

    (@stuartcr)

    sorry to sound stupid. but how do i do that?

    or are you actually explain it to me “return” instead of “echo”.

    again this is very new to me.

    thank you for you help

    Thread Starter stuartcr

    (@stuartcr)

    Hi there,

    i changed my code to this:

    if ( ! function_exists( 'serviced_office_title' ) ) :
    
    	function serviced_office_title() {
            if( get_field('is_this_office_available')=='yes' ){ 
            return get_field('office_number');
    		}
            elseif( get_field('is_this_office_available')=='no' ){
    		return get_the_title();
    		return get_field('office_number');
    		}   
    	}
    endif;

    BUT:

    if “no”, does appear in the the H1 BUT not with the office number, just the page title.

    return get_field(‘office_number’); <<< this isn’t working as expected, can i not have 2 “returns”.

    is there a way to combine them?

    where am i going wrong?? i’m getting there.

    Moderator bcworkz

    (@bcworkz)

    You can use the concatenation operator . (a dot) to combine multiple strings into one.
    return get_the_title() . ' ' . get_field('office_number');

    Thread Starter stuartcr

    (@stuartcr)

    uh-mazing!! that worked!! thank you for your help!!

    • This reply was modified 2 years, 7 months ago by stuartcr.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘php function not work as expected’ is closed to new replies.