[Support request] meta tags using advanced custom fields

Home Forums Support [Support request] meta tags using advanced custom fields

Home Forums Support meta tags using advanced custom fields

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #316322
    Vinai

    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

    #316323
    Vinai

    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 );
    #316362
    Tom
    Lead Developer
    Lead Developer

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

    #316390
    Vinai

    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

    #316660
    Vinai

    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,

    #316777
    Tom
    Lead Developer
    Lead Developer

    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.

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