[wp]metaとogpについての暫定的な設定

ifのオンパレード

<head prefix="og: http://ogp.me/ns#">
<?php /* meta */ ?>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<?php if ( is_single()): ?>
	<?php if ($post->post_excerpt): ?>
	<meta name="description" content="<? echo $post->post_excerpt; ?>" />
	<meta property="og:description" content="<? echo $post->post_excerpt; ?>">
	<?php else :
        $summary = strip_tags($post->post_content);
        $summary = str_replace("\n", "", $summary);
        $summary = mb_substr($summary, 0, 120). "…"; ?>
	<meta name="description" content="<?php echo $summary; ?>" />
	<meta property="og:description" content="<?php echo $summary; ?>">
	<?php endif; ?>
<?php elseif ( is_home() || is_front_page() ): ?>
	<meta name="description" content="<?php bloginfo('description'); ?>" />
	<meta property="og:description" content="<?php bloginfo('description'); ?>">
<?php elseif ( is_category() ): ?>
	<meta name="description" content="<?php echo category_description(); ?>" />
	<meta property="og:description" content="<?php echo category_description(); ?>">
<?php elseif ( is_tag() ): ?>
	<meta name="description" content="<?php echo tag_description(); ?>" />
	<meta property="og:description" content="<?php echo tag_description(); ?>">
<?php else: ?>
	<meta name="description" content="<?php the_excerpt();?>" />
	<meta property="og:description" content="<?php the_excerpt();?>">
<?php endif; ?>
	<meta property="og:title" content="<?php if(is_home()): bloginfo('name'); else: the_title(); endif; ?>">
	<meta property="og:type" content="<?php if(is_home()): ?>website<?php else: ?>article<?php endif; ?>">
	<meta property="og:url" content="<?php echo get_the_permalink(); ?>">
<?php if(has_post_thumbnail()): ?>
	<meta property="og:image" content="<?php the_post_thumbnail_url('full'); ?>">
<?php else: ?>
	<meta property="og:image" content="<?php echo get_template_directory_uri(); ?>/img/ogp.png">
<?php endif; ?>
	<meta property="og:site_name" content="<?php bloginfo( 'name' ); ?>">
	<meta property="og:locale" content="ja_JP">
	<meta name="twitter:card" content="summary_large_image">
<?php /* /meta */ ?>
<?php /* css */ ?>
    <link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/style.css">
<?php if(is_home()|| is_front_page()) : ?><link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/css/index.css"><?php endif; ?>
<?php /* /css */ ?>
<?php /* js */ ?>
    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
	<script src="<?php echo get_template_directory_uri(); ?>/js/scripts.js"></script>
<?php /* /js */ ?>
<?php wp_head(); ?>
</head>

とりあえずこんな感じで。

バグったら

Add Thisとかを入れるとdescriptionにhtmlタグを突っ込まれる事がある。<head>に<body>用のタグを入れると画面上に出力されてよろしくない。

タグを噛ませたら削れるので便利。

<?php echo strip_tags(get_bloginfo('description')); ?>

コメント

タイトルとURLをコピーしました