All in One SEO Pack は、投稿記事毎にタイトルや概要(Description)、キーワード(Keywords)を指定できる便利なプラグインです。
記事毎に登録されたDescription やKeywords を、meta タグ以外のOGPなどにも使いたいことがありますので、今回はAll in One SEO Pack のTitle、Description、Keywords の値を取得する方法と、これらの値が空白の時の条件分岐例を紹介いたします。
All in One SEO Pack のDescription やKeywords を取得
All in One SEO Pack の Title、Description、Keywords ですが、「カスタムフィールド」を利用していますので get_post_meta()関数によりそれぞれの値を取得することが可能です。
Title
[php] <?php echo get_post_meta($post->ID, _aioseop_title, true); ?>[/php]
Description
[php] <?php echo get_post_meta($post->ID, _aioseop_description, true); ?>[/php]
Keywords
[php] <?php echo get_post_meta($post->ID, _aioseop_keywords, true); ?>[/php]
All in One SEO Pack のDescription やKeywords の値が空白の場合の条件分岐
カスタムフィールドに「Description」「Keywords」の値が入ってればその値を、入っていなければサイト共通のDescriptionや、あらかじめ指定したキーワードを読み込ませる条件分岐も可能です。[php] <?php
$description = get_post_meta($post->ID, _aioseop_description, true);
$keywords = get_post_meta($post->ID, _aioseop_keywords, true);
?>
<?php
//Description値が空の場合
if($description==”) : ?>
<meta name="description" content="<?php bloginfo(‘description’); ?>">
<?php
//値が入っている場合
else: ?>
<meta name="description" content="
<?php echo get_post_meta($post->ID, _aioseop_description, true); ?>
">
<?php endif; ?>
<?php
//Keywords値が空の場合
if($keywords==”) : ?>
<meta name="keywords" content="キーワード">
<?php
//値が入っている場合
else: ?>
<meta name="keywords" content="
<?php echo get_post_meta($post->ID, _aioseop_keywords, true); ?>
">
<?php endif; ?>
[/php] もう少しコードをすっきりさせたい所ですが、is_category() や is_archive() などの他の条件分岐と合わせて使うなど、色々と応用できそうです。
コメントを残す