duanchu7271 2014-01-29 01:14
浏览 17
已采纳

在Wordpress PHP循环中匹配多个属性术语

This seems like a simple enough problem but I'm a novice at PHP and I've been working on this for hours. I'm looping through posts in an archive and showing a different logo for each post based on a certain attribute. Here's my existing function in functions.php:

function show_logo() {
global $post;
$attribute_names = array( 'pa_product-type'
 );

foreach ( $attribute_names as $attribute_name ) {
    $taxonomy = get_taxonomy( $attribute_name );
    if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
        $terms = wp_get_post_terms( $post->ID, $attribute_name );
        $terms_array = array();

        if ( ! empty( $terms ) ) {
            foreach ( $terms as $term ) {
               if ( $term->name == 'L1' ) {
                  // Show L1 Logo
               }
               elseif ( $term->name == 'M1' ) {
                  // Show M1 Logo
               }
               elseif ( $term->name == 'H1' ) {
                  // Show H1 Logo
               }
               else {
                  $full_line = '<span>'. $term->name . '</span>';
               }
               array_push( $terms_array, $full_line );
            }

            echo implode( $terms_array );
        }
    }
  }
}

All I want to do is do show a different logo if the post matches multiple terms (e.g. 'L1' AND 'M1'). I have tried many very different things but I have no idea if I'm even on the right track. Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • doujiao9426 2014-01-29 02:04
    关注

    This should be fairly easy, I'm just not exactly sure the full context of all the data involved.

    Assuming you are only showing one logo per post, here is one approach:

    Right before foreach ( $terms as $term ) { create three boolean variables:

    $hasL1 = false;
    $hasM1 = false;
    $hasH1 = false;
    

    Then when one of your term names matches, instead of just showing the logo set the appropriate variable equal to true, ie $hasL1 = true;

    After the foreach is complete, either before or after echo implode( $terms_array ); depending on what makes sense, setup a new if/elseif/else block to decide what logo to show as follows:

    if ($hasL1 && $hasM1 && $hasH1) { // Pick logo for all 3 }
    elseif ($hasL1 && $hasM1) { // Pick logo for pair }
    elseif ($hasL1 && $hasH1) { // Pick logo for pair }
    elseif ($hasH1 && $hasM1) { // Pick logo for pair }
    elseif ($hasL1) { // Pick logo }
    elseif ($hasM1) { // Pick logo }
    elseif ($hasH1) { // Pick logo }
    else { // default logo }
    

    Of course there are many other ways to implement this too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 dev运行环境,c语言,报错是为什么?怎么改
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能