Site logo

Archived topic

meta tags using advanced custom fields

5 replies · Started by Vinai on May 10, 2017

Viewing posts 1–6 of 6

I want to use my own custom fields (created using Advanced Custom Fields) to appear in the Page Meta Title and Description, for all pages.

Is there a simple way to get this done? I am using the Premium edition of GeneratePress.

Thanks,
Vin

I have tried the code below in the functions.php of the blank generatepress child theme. But the custom meta description field value did not appear in the source code.

/*Display custom meta description or the post excerpt */
function add_custom_meta_des(){

#Homepage Meta Description
if( is_home() || is_front_page() ){
	$meta_des = "Enter your homepage meta description here"; #Edit here
	echo '<meta name="description" content="' . $meta_des . '" />';
}

#Single Page Meta Description
if( is_single() ){
	$des = get_post_meta( get_the_id(), 'description', true);
	if( ! empty( $des )  ){
		$meta_des = esc_html($des);
		echo '<meta name="description" content="' . $meta_des . '" />';
	}
}}
add_action( 'wp_head', 'add_custom_meta_des', 4 );

Something like that should work. Can you link me to your site?

Thanks Tom. I have emailed you the login details of the site, and the page in question. Kindly look into it. If I should post the login details here, let me know, and I'll do so.

Thanks,
Vin

Hi,

I have now managed to get the post title and meta generated using the code below, in the child theme's functions.php file.

$des = get_post_meta(get_the_ID(), 'meta_title', true);
if( ! empty( $des ) ){
$meta_des = esc_html($des);
echo '<title>' . $meta_des . '</title>' ."\r\n";
}

However, on viewing the source code, the wp_head() function generates the page title using the Site Title and Site Tagline automatically, and so now I am seeing 2 titles in the source code head of the page.

How do I suppress the standard default site title and tagline from displaying on the site. I have already checked the "Hide Site Title" & Tagline in the "Site Identity" of the theme.

Thanks,

Is there any reason you're wanting to go this route instead of using a plugin like Yoast SEO?

WordPress itself adds the title tag (https://codex.wordpress.org/Title_Tag), so you would need to filter it with PHP if you don't want to use a plugin.

This archived topic is closed to new replies.