我希望有人可以在这里查看我的代码并提供任何建议,这样我就可以了解是否有更好的方法来完成相同的任务,因为我是自学成才的,我觉得我缺乏一些最佳实践知识并且倾向于工作直到我可以让它做我想做的事。
无论如何,我正在为 wordpress 附件模板制作一个功能,该功能将自动检测军事人员成员的等级,其图像显示在 wordpress 画廊中。目的是有人可以单击个人图像,它会显示带有关于军衔的简要说明的 wordpress 附件页面,然后根据图像标题中列出的军衔将军衔徽章放在图像旁边。该功能有效。我只是想改进它并了解这是否是实现此目标的最佳方法,或者我是否应该遵循更好的编码实践。感谢您的建议!
<?php $a = get_the_excerpt(); ?>
<?php
if (substr_count($a, 'Gen') > 0) {
$rankimage = 'http://myurl.com/images/Gen.png';
$rank = 'General';
}
elseif (substr_count($a, 'Col') > 0) {
$rankimage = 'http://myurl.com/images/Col.png';
$rank = 'Colonel';
}
elseif (substr_count($a, 'LTC') > 0) {
$rankimage = '';
$rank = 'Lieutenant Colonel';
}
elseif (substr_count($a, 'Maj') > 0) {
$rankimage = '';
$rank = 'Major';
}
elseif (substr_count($a, 'Cpt') > 0) {
$rankimage = '';
$rank = 'Captain';
}
elseif (substr_count($a, 'Lt') > 0) {
$rankimage = '';
$rank = 'First Lieutenant';
}
elseif (substr_count($a, '2Lt') > 0) {
$rankimage = '';
$rank = 'Second Lieutenant';
}
elseif (substr_count($a, 'SMA') > 0) {
$rankimage = '';
$rank = 'Sergeant Major of the Army';
}
elseif (substr_count($a, 'CSM') > 0) {
$rankimage = '';
$rank = 'Command Sergeant Major';
}
elseif (substr_count($a, 'SM') > 0) {
$rankimage = '';
$rank = 'Sergeant Major';
}
elseif (substr_count($a, '1Sgt') > 0) {
$rankimage = '';
$rank = 'First Sergeant';
}
elseif (substr_count($a, 'MSgt') > 0) {
$rankimage = '';
$rank = 'Master Sergeant';
}
elseif (substr_count($a, 'SFC') > 0) {
$rankimage = '';
$rank = 'Sergeant First Class';
}
elseif (substr_count($a, 'SSgt') > 0) {
$rankimage = '';
$rank = 'Staff Sergeant';
}
elseif (substr_count($a, 'Sgt') > 0) {
$rankimage = '';
$rank = 'Sergeant';
}
elseif (substr_count($a, 'Cpl') > 0) {
$rankimage = '';
$rank = 'Corporal';
}
elseif (substr_count($a, 'Spc') > 0) {
$rankimage = '';
$rank = 'Specialist';
}
elseif (substr_count($a, 'PFC') > 0) {
$rankimage = '';
$rank = 'Private First Class';
}
elseif (substr_count($a, 'Pvt') > 0) {
$rankimage = 'http://myurl.com/images/01Pvt.png';
$rank = 'Private';
}
?>
<?php
if ($rank != null){
echo "<table border='1' width='100%'>
<tbody>
<tr>
<td style='text-align: center;' colspan='2'>Rank Detected! $a is a $rank! <img src=$rankimage></td></tr>";
//I placed this if here to close the table in the event that the wordpress image had no description containing the closing tags. I nested it inside the if loop so that it doesn't have to run on pages that don't detect a rank in the image caption.
if (get_the_content() == null){
echo "</td></tr></tbody></table>";
}
}
else{
}
?>
我在下面添加了附加代码。
$date_list = Array(
Array( 'age', "$birthday", 'now' ),
Array( 'membership', "$datejoined", 'now' ),
Array( 'promoted', "$lastpromo", 'now' ),
);
foreach ( $date_list as $date_set ) {
$var = $date_set[0];
$start = $date_set[1];
$end = $date_set[2];
$datetime1 = date_create($start);
$datetime2 = date_create($end);
$interval = date_diff($datetime1, $datetime2);
if ( substr_count( $var, 'age' ) > 0 ){
$age = $interval->format('%y');
}
elseif ( substr_count( $var, 'membership' ) > 0 ){
$years = $interval->format('%y');
$months = $interval->format('%m');
$membership = floor(($years*12)+$months);
if($membership > 1){
$suffix = 'Months';
}
elseif($membership == 1){
$suffix = 'Month';
}
else{
$membership = "< 1";
$suffix = 'Month';
}
}
elseif ( substr_count( $var, 'promoted' ) > 0 ){
$years = $interval->format('%y');
$months = $interval->format('%m');
$test = $interval->format('%a');
$promoted = floor(($years*12)+$months);
if($promoted > 1){
$suffix = 'Months ago';
}
elseif($promoted == 1){
$suffix = 'Month ago';
}
else{
$promoted = "< 1";
$suffix = 'ago';
}
}
}