相当于wordpress/php中的ExpressionEngine开关标签


Equivalent of ExpressionEngine switch tag in wordpress/php?

在ExpressionEngine中,而在他们的'循环'版本中,我可以像这样为任何元素添加标记:

<li class="{switch='one|two|three|four|five|six'}">

li的第一次迭代将是类1,下一次是类2,在6之后再次循环。我需要这个类似的功能在一个wordpress网站,但不知道如何实现这一点。是否有一个内置的wordpress功能,或者我需要在php中编写某种功能?

目前,使用这个尝试使用@Leonard的解决方案,但类' 4 '被反复重复,而不是循环

<?php
$argsGallery = array(
    'post_type' => 'gallery',
    'orderby'       => 'menu_order',
    'order'         => 'ASC'
);
$the_query = new WP_Query( $argsGallery );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();?>
            <div class="<?php cycle('four|three|five|two|six|four'); ?> columns">
                <div class="thumb">
                    <a class="jackbox" 
                        data-group="images" 
                        data-thumbnail="<?php the_field('image'); ?>" 
                        data-title="Image One" 
                        data-description="#description_1" 
                        href="<?php the_field('image'); ?>"
                    >
                        <div class="jackbox-hover jackbox-hover-black">
                            <p><?php the_field('image_description'); ?> </p>
                        </div>
                        <img 
                            src="<?php the_field('image'); ?>" 
                            alt="responsive lightbox" 
                        />
                    </a>
                </div>
            </div>
<?php
    endwhile;
    wp_reset_query();
    wp_reset_postdata();
?>

在寻找完全相同的东西时发现了这个问题…在你发布后20分钟就登上了谷歌的榜首。疯了……无论如何!

我已经提出了一个函数,我已经测试(虽然很快),你可以在你的function .php和它的工作与标准的Wordpress循环。它可能需要适应某些需求,但希望这是一个好的起点。

它使用来自$wp_query数组的current_post计数,并计算出它需要在循环值中的位置。

function cycle($input, $delimiter = '|', $query = false) {
    if($query == false):
        global $wp_query ;
        $current_post = $wp_query->current_post + 1;
    else:
        $current_post = $query->current_post + 1;
    endif;

    $switches = explode($delimiter, $input);
    $total_switches = count($switches) ;
    $current_set = ceil( $current_post / $total_switches)  ;
    $i = (($current_post - ($current_set * $total_switches)) + $total_switches) - 1 ;
    echo $switches[$i];
}

则可以在STANDARD循环中使用,如下所示:

 <?php cycle('first|second|third|fourth'); ?>

如果需要,也可以自定义分隔符:

 <?php cycle('first*second*third', '*'); ?>

或者,如果您将它与CUSTOM wp_query一起使用,则必须像这样使用查询作为第三个参数:

 <?php cycle('first|second|third', '|', $the_query); ?>

我相信有一种更整洁的方式来提供自定义查询,我会继续寻找和更新,如果/当我找到一种方法!

这被称为循环函数。不幸的是,PHP本身没有这样的函数。

您将需要自定义写入一个,通过:

  • 创建函数。下面是一个很好的例子:
  • 直接在循环中使用计数器和模数。下面是一个人为的例子:

    $cycles = array('one', 'two', 'three');
    for ($i = 0; $i < 9; ++$i) {
      echo $cycles[$i % 3];
    }
    

我对WP有足够的了解,我从来没有见过这样的事情。也许这就是为什么ExpressionEngine已经有了这样的内置解决方案。在WP中,你必须在手边做。这里有一个你自己的想法:

UPDATE: @jason-mccreary的回答我知道我错了。我对cycles一无所知。但这里我有一个片段,似乎是这样工作的:

function cycle($chunks)
{   
    if ( ! is_array($array = explode('|', $chunks)))
        return;
    static $i    = 0;
    echo $array[$i ++];
    if ($i >= count($array))
        $i   = 0;
}
?>
<?php for ($i = 0; $i < 9;  ++ $i) : ?>
    <li class="<?php echo cycle('one|two|three|four|five|six'); ?>"></li>
<?php endfor; ?>

:

<li class="one"></li>
<li class="two"></li>
<li class="three"></li>
<li class="four"></li>
<li class="five"></li>
<li class="six"></li>
<li class="one"></li>
<li class="two"></li>
<li class="three"></li>
相关文章:
  • 没有找到相关文章