我正在尝试创建一个“添加到字符串,如果条件”满足功能,这是我的实际代码:
if ( ! function_exists( '_s_posted_on' ) ) :
function _s_posted_on( $params = array() ) {
$output = 's'; /** Holds the string of the code output by the arguments. */
if ( in_array('time', $params) ) : /** Begin main loop */
/**
Show the time of the post
*/
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
$posted_on = sprintf(
/* translators: %s: post date. */
esc_html_x( 'Posted on %s', 'post date', '_s' ),
'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
);
$output .= '<span class="posted-on">' . $posted_on . '</span>';
elseif ( in_array('author', $params) ) :
/**
Show the post's author
*/
$author = sprintf(
/* translators: %s: post author. */
esc_html_x( 'by %s', 'post author', '_s' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);
$output .= '<span class="author"> ' . $author . '</span>';
elseif ( in_array('category', $params) ) :
/**
Show the post's category.
*/
$categories = (array) wp_get_post_terms( get_the_ID(), 'category' );
$category_string = sprintf (
esc_html_x( 'in %s', 'post_category', '_s' ),
'<a class="category">' . $categories[0] -> name . '</a>'
);
$output .= '<span class="category">' . $category_string . '</span>';
else :
$output = 'No info about the post!';
endif; /** End main loop */
echo '<div class="post-meta-info"' . $output . '</div>';
}
endif;
调用_s_posted_on('time', 'category');
只输出time
的代码,但它应该从time
and category
输出代码,因为这两个条件都满足。
伪代码如下所示:
if (string_exists_in_params($string, $params)):
add_to_my_string --> $string2;
elseif (string_exists_in_params($string1, $params)):
add_to_string --> $string1;
elseif (string_exists_in_params($string2, $params)):
add_to_string --> $string2;
问题是循环每次碰到一个条件就会中断,所以说我用Qazxswpoi调用我的函数_s_posted_on
,我的$string1, $string2
应该是output
和$string1
的结果但不幸的是,它停在$string2
并且不再进一步检查$string1
。
简而言之,我正在尝试添加一个字符串,基于某些条件,可以存在2个条件,但循环在第一个条件遇到并停止时停止。
您可以尝试使用$string2
旗帜的多个ifs
else
$flag = false;
if (string_exists_in_params($string, $params)):
add_to_my_string --> $string2;
$flag = true;
if (string_exists_in_params($string1, $params)):
add_to_string --> $string1;
$flag = true;
if (string_exists_in_params($string2, $params)):
add_to_string --> $string2;
$flag = true;
if ($flag == false):
$output = 'No info about the post!';
的意思是,如果先前的条件没有通过,并且此许可确实通过,则执行此操作。听起来你只想在彼此之后有多个elseif
语句,这是完全正常和好的。
所以你可以做到
if
建议代码:
if($a == true) {
// Execute this if $a is true.
}
if($b == true) {
// Execute this if $b is true, no matter if the first if
// statement was executed or not.
}
要点/解释:
if(!function_exists('_s_posted_on')){
function _s_posted_on($params=array()){
$params=array_intersect(array('time','author','category'),$params); // filter and sort
if(empty($params)){ // this provides a shortcut whereby NO additional conditions or function calls are required
$output = 'No info about the post!';
}else{
$output=''; // I don't understand why your string should start with `s` so I am omitting it
foreach($params as $param){ // iterate upto 3 times total
if($param=='time'){ // function-less conditional check
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
$time_string = sprintf( $time_string,
esc_attr(get_the_date('c')),
esc_html(get_the_date()),
esc_attr(get_the_modified_date('c')),
esc_html(get_the_modified_date())
);
$posted_on = sprintf(
/* translators: %s: post date. */
esc_html_x('Posted on %s','post date','_s'),
'<a href="'.esc_url(get_permalink()).'" rel="bookmark">'.$time_string.'</a>'
);
$output.='<span class="posted-on">'.$posted_on.'</span>';
}elseif($param=='author'){ // function-less conditional check
$author = sprintf(
/* translators: %s: post author. */
esc_html_x( 'by %s', 'post author', '_s' ),
'<span class="author vcard"><a class="url fn n" href="'.esc_url(get_author_posts_url(get_the_author_meta('ID'))).'">'.esc_html(get_the_author()).'</a></span>'
);
$output.='<span class="author"> '.$author.'</span>';
}else{ // can only be `category` due to filtration step and prior conditions
$categories=(array) wp_get_post_terms(get_the_ID(),'category');
$category_string=sprintf(
esc_html_x('in %s','post_category','_s'),
'<a class="category">'.$categories[0]->name.'</a>'
);
$output.='<span class="category">'.$category_string.'</span>';
} // end `param` conditional
} // end foreach
} // end `empty` conditional
echo '<div class="post-meta-info">'.$output.'</div>'; // (generally, return is used in functions instead of echoing)
// I added this greater than sign^ <-- it seems rather important!
}
}
中存在有限数量的合格元素,并且因为元素的顺序很重要,所以我建议从一开始就使用$params
。通过将“可操作”元素按顺序简化所有其他进程向下脚本。 (如果您手动生成这些元素,则可以省略此步骤 - 假设您没有添加“不可操作”元素。)以下是我的过滤器/排序步骤的演示:array_intersect()
time
然后author
- 这是有效/最佳实践的问题。category
字符串以$output
开头,所以我把它删除了。s
电话。 in_array()
内部的if-elseif-else
块现在不进行函数调用,事实上,甚至不需要检查foreach()
,因为它在逻辑上是有保证的。category
添加到>
。<div class="post-meta-info"
而不是return
。如果你想回应echo
更多的专业人士会推荐$output
。我对你的项目并不了解,所以也许你有充分的理由按照自己的方式去做。