admin管理员组文章数量:1417662
Currently, my homepage contains a few of the latest posts. Therefore, when the homepage is shared via social media (i.e. Facebook), the thumbnail image which is displayed comes from the top image in the latest post.
What I am trying to do, is to set it so that when the homepage is shared, a specific, default image will be displayed. However, the image must only be displayed when the homepage is shared, and should not otherwise appear on the site. What would be the best way to do this?
Currently, my homepage contains a few of the latest posts. Therefore, when the homepage is shared via social media (i.e. Facebook), the thumbnail image which is displayed comes from the top image in the latest post.
What I am trying to do, is to set it so that when the homepage is shared, a specific, default image will be displayed. However, the image must only be displayed when the homepage is shared, and should not otherwise appear on the site. What would be the best way to do this?
Share Improve this question asked Jun 27, 2017 at 17:48 ThredolsenThredolsen 1751 gold badge1 silver badge8 bronze badges3 Answers
Reset to default 6Several options:
Out-of-the-box plugin
If you're using Yoast WordPress SEO, you have a built-in setting for Facebook images. Under your SEO > Social menu, go to the Facebook tab and select an image under "Frontpage settings." You can also set an image as a fallback, for posts that have no featured image, under "Default settings."
Other SEO plugins may have similar capabilities.
Child theme
You can create a child theme or modify a custom theme. First, make sure the theme supports a site logo. If not, add custom logo support.
Next, edit or copy header.php
into your new child theme. Inside the <head></head>
tags, include a check for if(is_front_page())
or if(is_home())
depending on your needs. Sounds like either would work in your case.
If the condition is met, grab the custom logo URL
$image = wp_get_attachment_image_url(get_theme_mod('custom_logo'), 'large');
and output it within Open Graph tags:
<meta property="og:image" content="<?php echo $image; ?>" />
<meta name="twitter:image" content="<?php echo $image; ?>" />
Custom plugin
You could also create your own plugin, if you don't want to use Yoast WordPress SEO or fiddle with the theme. The risk is that an existing theme or plugin may already output a featured image, so you'd want to check your page source and make sure nothing else is setting a featured image. I assume this is the case since your current theme and plugins are setting the most-recently-published post's image when you try to share it.
Basically, you would add an action for the wp_head
hook. Your action would output the Open Graph and Twitter image data just like you would if you chose the first option, working with the theme.
You'd have to decide whether to hard-code the image into your plugin, or whether you would create an options page somewhere in wp-admin where you could change the image whenever you wanted.
Different social platforms use different methods.
For Facebook, you need a set of <meta>
tags with og
properties - e.g. og:url
, og:type
, og:description
and og:image
. og:image
should contain the full path to the image you want to be displayed.
Twitter is similar but has their own 'card' mechanism which requires its own set of meta
tags - e.g. name="twitter:card"
and name="twitter:image"
I believe there are plugins to handle this. I have my own simple function which uses the thumbnail of the page, or a fallback if this isn't set.
OG meta tags is probably what you are looking for, in this case :
<meta property="og:image"content="path/to/foo.jpg" />
put inside a if is_home()
and hopefully it will work
本文标签: Default image for homepage when shared in social media
版权声明:本文标题:Default image for homepage when shared in social media 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745280472a2651398.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论