duanmianzhou5353 2017-07-25 12:48
浏览 69

我的帖子介于两个If语句之间,我该怎么办?

Currently I'm trying to filter out various things for the meta portion of my WordPress website, and I've found an issue, using the following code was working perfectly until I needed to get a bit more specific.

if (is_singular('post')) {
        $title = get_the_title ();
        print '<title>'.$title.'</title>';
        $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        print '<meta property="og:image" content="'.$feat_image.'"/>';
        print '<meta name="description" content="" />';
    } else {
        print '<title></title>';
        print '<meta name="description" content="" />';         
} 

Note that I edited the meta descriptions above so that they're empty, just for the sake of posting on stack.

Then I wanted to start filtering based on the tags a post has, and obviously this is going to conflict with the already declared (is_singular('post')) function above.

However, I used the code below.

if ( has_tag( 'spanish' )) {
        print '<title>'.$title.'</title>';
        $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        print '<meta property="og:image" content="'.$feat_image.'"/>';
        print '<meta name="description" content="" />';
}

And it is displaying the meta for this portion also in the category we wish for it too, how ever it's also displaying the meta description for the other function as well, what can I do stack?

  • 写回答

1条回答 默认 最新

  • douchun5969 2017-07-25 13:04
    关注

    This might sound simplistic, but firstly you want to document exactly what you want to happen. Maybe make yourself a flow chart or write a couple of paragraphs. Either way, you need to know from a non-code point of view first. This will enable you to answer questions like "Does a tag take precedence over a single post?" Once you know this process, move your conditions away from output to simplify the process. Something like this:

    // defaults
    $title = '...';
    $metaDescription = '...';
    $featuredImage = '...';
    
    // overrides for difference scenarios
    if (is_singular('post')) {
      // set title, etc here
    }
    if (has_tag('spanish')) {
      // override or concatenate title, etc here
    }
    
    // output
    print "<title>$title</title>";
    <!-- etc etc -->
    
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害