dsyq40772 2015-02-03 09:07
浏览 427
已采纳

Yo SEO的WP SEO - 如何设置不同的og:每个类别的图像

I would like to set individual facebook open graph meta images (og:image) for specific posts in Wordpress.

For example, set og:image to cat_1_image.jpg if the post is in category 1, and set it to cat_2_image.jpg if the post is in category with id = 2.

Pseudo-Code:

if($category->term_id == 1)
    og:image = 'cat_1_image.jpg';
elseif($category->term_id == 2)
    og:image = 'cat_2_image.jpg';
elseif($category->term_id == 3) // CatID 3 means 'Post has images, no default og:image, please'
    og:image = '';

Now, I've looked at the various hooks of wpseo, like wpseo_pre_analysis_post_content and wpseo_opengraph_image but this does not seem to lead in the right direction.

Can anybody please explain to me how to achieve different og:images for different post categories?!

The reason for my hack: WP SEO does not pick up images which are inserted into a post using the gallery shortcode. It will fall back to include the default image (as specified under WP SEO options) as the og:image. Hence I want to disable the inclusion of the default image for posts of a certain category (i.e. category 'has_images') and let the facebook scraper pick up the gallery images - they DO get picked up if no default og:image is present!

  • 写回答

2条回答 默认 最新

  • doutangkao2789 2015-02-03 10:42
    关注

    You could try something like this (not tested). It uses the wpseo_opengraph_image Yoast filter for the opengraph image.

    add_filter('wpseo_opengraph_image', 'category_image');
    function category_image($image) {
       global $post;
       if( in_category( 'category1', $post->ID ) ) {
           $image = get_stylesheet_directory_uri().'/images/cat_1_image.jpg';
       } elseif(in_category( 'category2', $post->ID )) {
           $image = get_stylesheet_directory_uri().'/images/cat_2_image.jpg';
       }
       return $image;
    }
    

    In this example i'm guessing your images are in your themes images folder.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C