2012-10-24 50 views
1

如果标题长度超过40个字符,我正在修改面包屑脚本以追加'...'。我把那部分工作做得很好,但是我不能正确的解决的就是如何将它嵌入到这个庞大的脚本中,如果它长度不到40个字符,它就会离开它。嵌套elesif语法

我还需要别的吗?我从来没有遇到过,那里有多个if个脚本没有elseelseif

} 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; 

     } 
+1

这不会解决您最实际的问题,但我用了这种情况的东西是在服务器上,我检查字符串的长度为> 40(像你这样做)......如果是这样,加一个类的元素叫做“trimmed”,只打印前40个字符......然后在你的CSS中使用这个规则:'.trimmed:after {content:“...”; } – Ian

+0

整洁的技巧,谢谢分享 –

回答

0

最后elseif是无效的,因为它已经被其他的前面。

我也会说你应该习惯于使用标准化的写作方式if/if-else语句。在我看来,你的代码可能会搞砸了,因为你有一些没有大括号的if语句,有些语句是。例如,如果您在所有情况下都使用花括号,并为它们使用适当的缩进,那么您遇到的问题以及如果遵循其他条件,则会非常清楚地发现。

举个例子:

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; 
    } 
} 

对我来说,这是更清晰的阅读,即使它是稍微详细。同样容易清楚的是,最后的其他 - 如果没有意义。顺便说一句,当你修正你的逻辑时,确保你考虑如果字符串正好是40个字符长度会发生什么。

+0

这段代码不是我的,我只是想知道如何修改它来截断疯狂的长篇文章。我同意这很混乱,但脚本足够长,我决定现在不花时间(这只是一小部分)。我的代码编辑器将最后一个'else if'标记为语法错误。当我完全删除它说它没有问题。我不完全明白为什么 - 如果编辑错了或者我错过了什么地方 –

+0

@kristinachilds正如我所提到的,这不是有效的代码。在它之前已经有了'else'条件来捕获所有'get_post_type()!='post''评估为'false'的情况。 –

+0

我猜想你嵌套在无效的else-if中的if应该是前一个if(($ showCurrent == 1)&&(strlen($ post-if) > post_title)> 40)){'语句。 –

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; } 

     } 
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 
+0

我没有想到!谢谢。虽然'else'仍然被标记为语法错误。也许整个脚本需要在上下文中看到?我试图挽救你的眼睛,因为这只是一小部分(而且代码非常难看)。 HTTP://引擎收录。com/TQ9kd30g我突出了它来帮助你们。 –

+0

您需要关闭封闭的elseif块。我已经添加了上面的大括号。 –

+0

,因为它抛出了下面的其他'elseif',导致更多的问题 –