1

I'm modifying a breadcrumb script to append a '...' if the title is over 40 characters long. I got that part worked out fine, but what I can't get right is how to nest it in this massive script to leave it along if it's under 40 characters long.

Do I even need an else here? I have never encountered a script where there were multiple ifs without an else or elseif

    } elseif ( is_single() && !is_attachment() ) {
      if ( get_post_type() != 'post' ) {
        $post_type = get_post_type_object(get_post_type());
        $slug = $post_type->rewrite;
        echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>';
        if ($showCurrent == 1) echo ' ' . $delimiter . ' ' . $before . substr(the_title('', '', FALSE), 0, 40) . $after;
      } else {
        $cat = get_the_category(); $cat = $cat[0];
        $cats = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
        if ($showCurrent == 0) $cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
        echo $cats;
        if (($showCurrent == 1) && (strlen($post->post_title) > 40)) echo $before . substr(the_title('', '', FALSE), 0, 40) . '...' . $after;
      } elseif {
        if (($showCurrent == 1) && (strlen($post->post_title) < 40)) echo $before . get_the_title() . $after;

      }
4

3 回答 3

0

逻辑不清楚。您还没有处理长度正好是 40 个字符的情况。

尝试构造您的代码,以便您只需要评估一次相同的表达式。也许这就是你的意思?

} elseif ( is_single() && !is_attachment() ) {
  if ( get_post_type() != 'post' ) {
   ...
  } else {
    ...
    echo $cats;

    if (($showCurrent == 1) && (strlen($post->post_title) > 40)) 
      echo $before . substr(the_title('', '', FALSE), 0, 40) . '...' . $after;
    } 
    else {
      echo $before . get_the_title() . $after;
    }
  }
}  // Close the elseif
于 2012-10-24T20:02:36.517 回答
0

最后一个elseif是无效的,因为它前面已经有一个 else。

我还要说,您应该养成使用标准化方式编写 if / if-else 语句的习惯。在我看来,您的代码可能搞砸了,因为您有一些没有花括号的 if 语句和一些带有花括号的语句。例如,如果您要在所有情况下都使用花括号,并为它们使用适当的缩进,那么您在 else 条件之后使用 and elseif 的问题就会很明显地被发现。

举个例子:

if ( get_post_type() != 'post' ) {
    $post_type = get_post_type_object(get_post_type());
    $slug = $post_type->rewrite;
    echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>';
    if ($showCurrent == 1) {
         echo ' ' . $delimiter . ' ' . $before . substr(the_title('', '', FALSE), 0, 40) . $after;
    }
} else {
    $cat = get_the_category(); $cat = $cat[0];
    $cats = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
    if ($showCurrent == 0) {
        $cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
    }
    echo $cats;
    if (($showCurrent == 1) && (strlen($post->post_title) > 40)) {
        echo $before . substr(the_title('', '', FALSE), 0, 40) . '...' . $after;
    }
} else if {
    if (($showCurrent == 1) && (strlen($post->post_title) < 40)) {
        echo $before . get_the_title() . $after;
    }
}

对我来说,尽管它稍微冗长一些,但阅读起来更清晰。很明显,最后一个 else-if 是没有意义的。

顺便说一句,当你修正你的逻辑时,一定要考虑如果字符串正好是 40 个字符长会发生什么。

于 2012-10-24T19:54:13.927 回答
0

看起来您可能缺少一些大括号

} elseif ( is_single() && !is_attachment() ) {
      if ( get_post_type() != 'post' ) {
        $post_type = get_post_type_object(get_post_type());
        $slug = $post_type->rewrite;
        echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>';

        if ($showCurrent == 1) { echo ' ' . $delimiter . ' ' . $before . substr(the_title('', '', FALSE), 0, 40) . $after; }

      } else {
        $cat = get_the_category(); $cat = $cat[0];
        $cats = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');

        if ($showCurrent == 0) { $cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
        echo $cats; }

        if (($showCurrent == 1) && (strlen($post->post_title) > 40)) { echo $before . substr(the_title('', '', FALSE), 0, 40) . '...' . $after; }

      } elseif {

        if (($showCurrent == 1) && (strlen($post->post_title) < 40)) { echo $before . get_the_title() . $after; }

      }
于 2012-10-24T19:57:16.050 回答