teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

解決済コードの追記

2017/05/06 09:11

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -71,4 +71,66 @@
71
71
 
72
72
  また、固定ページの中に設定項目を全部入れています。
73
73
 
74
- よろしくお願いします。
74
+ よろしくお願いします。
75
+
76
+ ###解決済コード
77
+ ```ここに言語を入力
78
+ <meta property="og:type" content="blog">
79
+ <!-- OGP -->
80
+ <?php if (is_single() || is_page()) { ?>
81
+ <?php while (have_posts()) : the_post(); ?>
82
+ <meta property="og:title" content="<?php the_title(); ?>" />
83
+ <meta property="og:type" content="article" />
84
+ <?php if (has_post_thumbnail()): ?>
85
+ <?php
86
+ $thumbnail_id = get_post_thumbnail_id($post->ID);
87
+ $src_info = wp_get_attachment_image_src($thumbnail_id, 'large');
88
+ $src = $src_info[0];
89
+ ?>
90
+ <meta property="og:image" content="<?php echo $src; ?>" />
91
+ <?php elseif(get_field( 'background', 345 )): ?>
92
+ <?php
93
+ $ogp_img = get_field('background',345);
94
+ //「background」はカスタムフィールドのフィールド名、「345」は設定用に作成した固定ページのID
95
+ $size = "full";
96
+ if( !empty($ogp_img) ): ?>
97
+ <meta property="og:image" content="<?php echo $ogp_img; ?>" />
98
+ <?php endif; ?>
99
+ <?php else: ?>
100
+ <meta property="og:image" content="<?php bloginfo('template_url'); ?>/images/img_ogp.png" />
101
+ <?php endif; ?>
102
+ <meta property="og:url" content="<?php the_permalink(); ?>" />
103
+ <meta property="og:description" content="<?php bloginfo('description'); ?>" />
104
+ <?php endwhile; ?>
105
+
106
+ <?php } elseif (is_home()) { ?>
107
+ <meta property="og:title" content="<?php bloginfo('name'); ?>" />
108
+ <meta property="og:type" content="blog" />
109
+ <?php if(get_field( 'background', 345 )): ?>
110
+ <?php
111
+ $ogp_img = get_field('background',345);
112
+ //「background」はカスタムフィールドのフィールド名、「345」は設定用に作成した固定ページのID
113
+ $size = "full";
114
+ if( !empty($ogp_img) ): ?>
115
+ <meta property="og:image" content="<?php echo $ogp_img; ?>" />
116
+ <?php endif; ?>
117
+ <?php else: ?>
118
+ <meta property="og:image" content="<?php bloginfo('template_url'); ?>/images/img_ogp.png" />
119
+ <?php endif; ?>
120
+ <meta property="og:url" content="<?php bloginfo('url'); ?>" />
121
+ <meta property="og:description" content="<?php bloginfo('description'); ?>" />
122
+ <?php } else { ?>
123
+ <meta property="og:title" content="<?php wp_title('|', true, 'right'); ?><?php bloginfo('name'); ?>" />
124
+ <meta property="og:type" content="article" />
125
+ <meta property="og:image" content="<?php bloginfo('template_url'); ?>/images/img_ogp.png" />
126
+ <meta property="og:url" content="<?php echo get_bloginfo('url').$_SERVER['REQUEST_URI']; ?>" />
127
+ <meta property="og:description" content="<?php bloginfo('description'); ?>" />
128
+ <?php } ?>
129
+
130
+ <!-- /OGP -->
131
+ <meta property="og:site_name" content="<?php bloginfo('name'); ?>">
132
+ <meta property="og:locale" content="ja_JP" />
133
+ <meta property="fb:app_id" content="App-ID(15文字の半角数字)">
134
+ <meta name="twitter:card" content="summary_large_image">
135
+ <meta name="twitter:site" content="@twitterID">
136
+ ```

1

コードの修正

2017/05/06 09:11

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -23,21 +23,13 @@
23
23
  <meta property="og:type" content="article" />
24
24
  <?php if (has_post_thumbnail()): ?>
25
25
  <?php
26
- $image_id = get_post_thumbnail_id();
26
+ $thumbnail_id = get_post_thumbnail_id($post->ID);
27
- $image_url = wp_get_attachment_image_src($image_id, $size = 'thumbnail');
27
+ $src_info = wp_get_attachment_image_src($thumbnail_id, 'large');
28
+ $src = $src_info[0];
28
29
  ?>
30
+ <meta property="og:image" content="<?php echo $src; ?>" />
29
31
  <?php elseif(post_custom('background')): ?>
30
- <?php
31
- $wp_query = new WP_Query();
32
- $param = array(
33
- 'post_type' => 'page',
34
- 'name' => 'home',
35
- );
36
- $wp_query->query( $param );
37
- if ( $wp_query->have_posts() ): $wp_query->the_post();
38
- ?>
39
- <meta property="og:image" content="<?php the_field('background'); ?>" />
32
+ <meta property="og:image" content="<?php get_field( 'background', 345 ); ?>" />
40
- <?php endif; ?>
41
33
  <?php else: ?>
42
34
  <meta property="og:image" content="<?php bloginfo('template_url'); ?>/images/img_ogp.png" />
43
35
  <?php endif; ?>
@@ -48,7 +40,11 @@
48
40
  <?php } elseif (is_home()) { ?>
49
41
  <meta property="og:title" content="<?php bloginfo('name'); ?>" />
50
42
  <meta property="og:type" content="blog" />
43
+ <?php if(post_custom('background')): ?>
44
+ <meta property="og:image" content="<?php get_field( 'background', 345 ); ?>" />
45
+ <?php else: ?>
51
46
  <meta property="og:image" content="<?php bloginfo('template_url'); ?>/images/img_ogp.png" />
47
+ <?php endif; ?>
52
48
  <meta property="og:url" content="<?php bloginfo('url'); ?>" />
53
49
  <meta property="og:description" content="<?php bloginfo('description'); ?>" />
54
50
  <?php } else { ?>