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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler