即使满足过去条件,继续沿着PHP中'elseif'的路径行进?

问题描述 投票:1回答:3

我正在尝试创建一个“添加到字符串,如果条件”满足功能,这是我的实际代码:

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的代码,但它应该从timeand 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个条件,但循环在第一个条件遇到并停止时停止。

php wordpress if-statement
3个回答
2
投票

您可以尝试使用$string2旗帜的多个ifs

else

1
投票

$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

0
投票

建议代码:

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()
  • 总是尝试在条件块中编写早期退出条件(并针对该问题循环)。如果没有可操作的元素,那么直接跳到最后 - 不要检查Demo然后time然后author - 这是有效/最佳实践的问题。
  • 我不知道为什么你的category字符串以$output开头,所以我把它删除了。
  • 我能够完全消除s电话。 in_array()内部的if-elseif-else块现在不进行函数调用,事实上,甚至不需要检查foreach(),因为它在逻辑上是有保证的。
  • 我把缺少的category添加到>
  • 通常,作为最佳实践,函数通常写入<div class="post-meta-info"而不是return。如果你想回应echo更多的专业人士会推荐$output。我对你的项目并不了解,所以也许你有充分的理由按照自己的方式去做。
© www.soinside.com 2019 - 2024. All rights reserved.