Alternate ways to display additional fields (from the add-on)

Home Forums Guestbook Add-On Alternate ways to display additional fields (from the add-on)

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1504

    I am looking to display the field ‘year visited’ and ‘cottage’ in the header sentence, the one that says ‘Alex from CITY wrote on August 22, 2021 at 7:17 pm’ I would like instead to change it to ‘Alex from CITY stayed in COTTAGE during YEAR wrote’

    Can this be achieved? If not, what options do I have with regards to display of alternate fields? Displaying them at the top of the entry or at the bottom, or in the metabox is very limiting.

    #1509
    Marcel Pol
    Keymaster

    Hi Aleksandr,
    No problem about asking this. The 3 options for display are the most common ways for displaying. It can be changed and customized, but often that needs custom code for that specific desire/need.

    What I did is disable the date fields and also the City/Origin field in the read-settings.
    Then this next code can go to a custom plugin, or can be added to the functions.php of your theme.
    You might want to check if the slugs of both fields are correct.

    function gwolle_gb_author_name_html_add_meta_from_addon( $html, $entry ) {
    
    	if ( ! function_exists( 'gwolle_gb_addon_get_meta_for_preview' ) ) {
    		return $html;
    	}
    
    	$origin = $entry->get_author_origin();
    	if ( strlen(str_replace(' ', '', $origin)) > 0 ) {
    		$html .= '
    				<span class="gb-author-origin">
    					<span class="gb-author-origin-from-text"> ' . /* translators: city or origin */ esc_html__('from', 'gwolle-gb') . '</span>
    					<span class="gb-author-origin-text"> ' . gwolle_gb_sanitize_output($origin) . '</span>
    				</span>';
    	}
    
    	$cottage = gwolle_gb_addon_get_meta_for_preview( $entry->get_id(), 'cottage' );
    	if ( ! $cottage ) {
    		$cottage = gwolle_gb_addon_get_meta( $entry->get_id(), 'cottage' );
    	}
    	$year = gwolle_gb_addon_get_meta_for_preview( $entry->get_id(), 'year-visited' );
    	if ( ! $year ) {
    		$year = gwolle_gb_addon_get_meta( $entry->get_id(), 'year-visited' );
    	}
    
    	if ( strlen($cottage) > 0 ) {
    		$html .= '<span class="gb-author-cottage-text"> stayed in ' . $cottage . '</span>';
    	}
    	if ( strlen($year) > 0 ) {
    		$html .= '<span class="gb-author-cottage-text"> during ' . $year . ' wrote</span>';
    	}
    
    	var_dump($html);
    
    	return $html;
    
    }
    add_filter( 'gwolle_gb_author_name_html', 'gwolle_gb_author_name_html_add_meta_from_addon', 10, 2 );

    Does it work for you?

    #1517

    Hi Marcel, thanks a lot for your help. The function works well. All I needed to do was disable the city and datetime display before enabling the function, as well as delete the var_dump($html). Thanks again!

    #1518
    Marcel Pol
    Keymaster

    Ouch, yes, I forgot the var_dump( $html ), sorry about that :)

    Good that it works for you.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.