Skip to content

Instantly share code, notes, and snippets.

@mankutila
Last active January 20, 2018 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mankutila/f2645cb6f8b6d6f1c6a6fb11a32964cb to your computer and use it in GitHub Desktop.
Save mankutila/f2645cb6f8b6d6f1c6a6fb11a32964cb to your computer and use it in GitHub Desktop.
Show meta information in WP using ACF
<title><?php showMeta('title');?></title>
<meta name="description" content="<?php showMeta('description');?>">
<?
/* First, create ACF fields with slugs 'meta_title', 'meta_description' for posts, pages and categories */
function showMeta($meta) {
$stringAfter = ' | Центр эстетической медицины ШайнЭст';
$titleForFrontPage = 'Центр эстетической медицины ШайнЭст';
if ( is_tax() ) {
$term_slug = get_query_var( 'term' );
$taxonomy = get_query_var( 'taxonomy' );
$term = get_term_by( 'slug', $term_slug, $taxonomy);
$term_id_str = $taxonomy . '_' . $term->term_id;
$meta_title = get_field('meta_title', $term_id_str);
$meta_description = get_field('meta_description', $term_id_str);
} else {
$meta_title = get_field('meta_title');
$meta_description = get_field('meta_description');
}
$defaultTitle = wp_title('', false);
if ($meta == "title") {
if ( $meta_title ) {
$resultTitle = $meta_title . $stringAfter;
} else if ( is_front_page() ) {
$resultTitle = $titleForFrontPage;
} else {
$substr = substr($defaultTitle, 2);
$resultTitle = $substr . $stringAfter;
}
echo $resultTitle;
}
if ($meta == "description") {
if ( $meta_description ) {
$resultDesc = $meta_description;
} else {
$substr = substr($defaultTitle, 2);
$resultDesc = $substr;
}
echo $resultDesc;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment