Last active
January 20, 2018 14:40
-
-
Save mankutila/f2645cb6f8b6d6f1c6a6fb11a32964cb to your computer and use it in GitHub Desktop.
Show meta information in WP using ACF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<title><?php showMeta('title');?></title> | |
<meta name="description" content="<?php showMeta('description');?>"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/* 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