iNeedPHPCodingHelp Posted January 24, 2020 Share Posted January 24, 2020 Hi all, I'm new here, I just require a (most likely simple for you lot) little help with editing some PHP code... I am editing a WordPress child themes functions.php file, basically I want to get the logo image alt text to be generated per page by the pages title, I have sucessfully achieved this on some other sites with different themes but am having issues here. This works in some themes: alt="<?php echo the_title(); ?> keyword keyword"/> So I need to include this: alt="<?php echo the_title(); ?> keyword keyword"/> Or this: [show_post_categories show=”title”] Into this, replacing the site_title in the alt= parts: // LOGO CODE FOR SORTING ALT TEXT <!-- Header logo --> <a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?><?php echo get_bloginfo( 'name' ) && get_bloginfo( 'description' ) ? ' - ' : ''; ?><?php bloginfo( 'description' ); ?>" rel="home"> <?php if(flatsome_option('site_logo')){ $logo_height = get_theme_mod('header_height',90); $logo_width = get_theme_mod('logo_width', 200); $site_title = esc_attr( get_bloginfo( 'name', 'display' ) ); if(get_theme_mod('site_logo_sticky')) echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.get_theme_mod('site_logo_sticky').'" class="header-logo-sticky" alt="'.$site_title.'"/>'; echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.flatsome_option('site_logo').'" class="header_logo header-logo" alt="'.$site_title.'"/>'; if(!get_theme_mod('site_logo_dark')) echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.flatsome_option('site_logo').'" class="header-logo-dark" alt="'.$site_title.'"/>'; if(get_theme_mod('site_logo_dark')) echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.get_theme_mod('site_logo_dark').'" class="header-logo-dark" alt="'.$site_title.'"/>'; } else { bloginfo( 'name' ); } ?> </a> <?php if(get_theme_mod('site_logo_slogan')){ echo '<p class="logo-tagline">'.get_bloginfo('description').'</p>'; } ?> THIS IS THE WHOLE FUNCTIONS.PHP FILE IN MY CHILD THEME: <?php // Add custom Theme Functions here /** * Change text strings * * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext */ function custom_related_products_text( $translated_text, $text, $domain ) { switch ( $translated_text ) { case 'Related products' : $translated_text = __( 'More fire & security systems you may like', 'woocommerce' ); break; } return $translated_text; } add_filter( 'gettext', 'custom_related_products_text', 20, 3 ); /* USE SHORTCODES IN WIDGET TITLES AND PAGE TITLES !! */ add_filter('the_title', 'do_shortcode'); add_filter('widget_title', 'do_shortcode'); /* NOT SURE WHY I HAD THIS HERE??... <?php echo do_shortcode("[shortcode]"); ?> */ /* ADD ALT TAG TO GRAVATAR - BLOG-AUTHOR-IMAGE - IMAGES */ function crunchify_gravatar_alt($crunchifyGravatar) { if (have_comments()) { $alt = get_comment_author(); } else { $alt = get_the_author_meta('display_name'); } $crunchifyGravatar = str_replace('alt=\'\'', 'alt=\'Avatar for ' . $alt . '\' title=\'Gravatar for ' . $alt . '\'', $crunchifyGravatar); return $crunchifyGravatar; } add_filter('get_avatar', 'crunchify_gravatar_alt'); // LOGO CODE FOR SORTING ALT TEXT <!-- Header logo --> <a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?><?php echo get_bloginfo( 'name' ) && get_bloginfo( 'description' ) ? ' - ' : ''; ?><?php bloginfo( 'description' ); ?>" rel="home"> <?php if(flatsome_option('site_logo')){ $logo_height = get_theme_mod('header_height',90); $logo_width = get_theme_mod('logo_width', 200); $site_title = esc_attr( get_bloginfo( 'name', 'display' ) ); if(get_theme_mod('site_logo_sticky')) echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.get_theme_mod('site_logo_sticky').'" class="header-logo-sticky" alt="'.$site_title.'"/>'; echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.flatsome_option('site_logo').'" class="header_logo header-logo" alt="'.$site_title.'"/>'; if(!get_theme_mod('site_logo_dark')) echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.flatsome_option('site_logo').'" class="header-logo-dark" alt="'.$site_title.'"/>'; if(get_theme_mod('site_logo_dark')) echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.get_theme_mod('site_logo_dark').'" class="header-logo-dark" alt="'.$site_title.'"/>'; } else { bloginfo( 'name' ); } ?> </a> <?php if(get_theme_mod('site_logo_slogan')){ echo '<p class="logo-tagline">'.get_bloginfo('description').'</p>'; } ?> I just can't get it to work, keeps throwing errors on save :( I know this is just a silly newbies error - any assistance would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
maxxd Posted January 24, 2020 Share Posted January 24, 2020 And that code is exactly why I hated working with WordPress themes. Anyway, replace the 4 instances of $site_title with get_the_title() That should do the trick for you. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 24, 2020 Share Posted January 24, 2020 Alternatively, you could just update the line that defines $site_title. That way you're not calling get_the_title() multiple times. Plus, the value should be escaped for inclusion in an HTML attribute. $site_title = esc_attr( get_the_title() ); As for the following, WordPress' the_title() function echos the value be default. I forget what happens when you try to echo something that has already been echoed in WordPress, but that probably lead to the issues you were having. 1 hour ago, iNeedPHPCodingHelp said: So I need to include this: alt="<?php echo the_title(); ?> keyword keyword"/> 1 Quote Link to comment Share on other sites More sharing options...
iNeedPHPCodingHelp Posted January 24, 2020 Author Share Posted January 24, 2020 32 minutes ago, maxxd said: And that code is exactly why I hated working with WordPress themes. Anyway, replace the 4 instances of $site_title with get_the_title() That should do the trick for you. Many thanks for your reply, I had tried that before with no joy, have just tried again and the same result. The alt text is still displaying the site title, maybe I'm editing the wrong file (I don't think so due to what is in it), or it might be the way I am adding the code? <!-- Header logo --> <a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?><?php echo get_bloginfo( 'name' ) && get_bloginfo( 'description' ) ? ' - ' : ''; ?><?php bloginfo( 'description' ); ?>" rel="home"> <?php if(flatsome_option('site_logo')){ $logo_height = get_theme_mod('header_height',90); $logo_width = get_theme_mod('logo_width', 200); $site_title = esc_attr( get_bloginfo( 'name', 'display' ) ); if(get_theme_mod('site_logo_sticky')) echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.get_theme_mod('site_logo_sticky').'" class="header-logo-sticky" alt="'.$get_the_title().'"/>'; echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.flatsome_option('site_logo').'" class="header_logo header-logo" alt="'.$get_the_title().'"/>'; if(!get_theme_mod('site_logo_dark')) echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.flatsome_option('site_logo').'" class="header-logo-dark" alt="'.$get_the_title().'"/>'; if(get_theme_mod('site_logo_dark')) echo '<img width="'.$logo_width.'" height="'.$logo_height.'" src="'.get_theme_mod('site_logo_dark').'" class="header-logo-dark" alt="'.$get_the_title().'"/>'; } else { bloginfo( 'name' ); } ?> </a> <?php if(get_theme_mod('site_logo_slogan')){ echo '<p class="logo-tagline">'.get_bloginfo('description').'</p>'; } ?> Appreciate the help! Quote Link to comment Share on other sites More sharing options...
maxxd Posted January 24, 2020 Share Posted January 24, 2020 Note that "$get_the_title()" is not the same as "get_the_title()". 1 Quote Link to comment Share on other sites More sharing options...
iNeedPHPCodingHelp Posted January 24, 2020 Author Share Posted January 24, 2020 29 minutes ago, cyberRobot said: Alternatively, you could just update the line that defines $site_title. That way you're not calling get_the_title() multiple times. Plus, the value should be escaped for inclusion in an HTML attribute. $site_title = esc_attr( get_the_title() ); As for the following, WordPress' the_title() function echos the value be default. I forget what happens when you try to echo something that has already been echoed in WordPress, but that probably lead to the issues you were having. Thanks a lot for your help cyberRobot I've got that working perfectly, but only by editing the core WordPress file... I previously tried to add, in various (haven't a clue) ways, into my child theme functions.php file so it overrides the core function but with luck at all, could you possibly give me some guidance on implementing this that way please? My naive attempt was adding this snippet into the child functions file: function site_logo($site_title, $site_logo, $get_the_title) { $site_title = esc_attr( get_the_title() ); } Obviously that doesn't seem to work! 😶 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.