【问题标题】:Insert a DIV Container inside a Modified Wordpress Loop在修改后的 Wordpress 循环中插入 DIV 容器
【发布时间】:2014-04-12 05:43:41
【问题描述】:

我正在处理一个修改过的 wordpress 循环,其中有一个 if 和 else 条件。代码在下面。

我想要做的是添加一个容器来容纳每个满足条件的帖子。这将对每个条件进行分组。

http://pastebin.com/1R2jsWkY

<div class="container">

<?php if (have_posts()) : ?>

<?php
$i = 1;
while (have_posts()) {
    the_post();

// Add a DIV CONTAINER that holds the FIRST CONDITION
    if ($i <= 3) {
        the_title();
        the_content();
// Add a DIV CONTAINER that holds the FIRST CONDITION



// Add a DIV CONTAINER that holds the SECOND CONDITION
    } elseif ($i <= 9) {
        the_title();
        the_permalink();
// Add a DIV CONTAINER that holds the SECOND CONDITION


// Add a DIV CONTAINER that holds the THIRD CONDITION
    } elseif ($i <= 13) {
        the_title();
        the_permalink();
// Add a DIV CONTAINER that holds the THIRD CONDITION


// Add a DIV CONTAINER that holds the FOURTH CONDITION
    } elseif ($i <= 15) {
        the_title();
// Add a DIV CONTAINER that holds the FOURTH CONDITION


// Add a DIV CONTAINER that holds the FIFTH CONDITION
    } else {
        the_title();
// Add a DIV CONTAINER that holds the FIFTH CONDITION
    }
    $i++;
}

?>

<?php else : ?>

<h2>No Posts Found</h2>

<?php endif; ?>

</div>

如果我在我的 cmets 所在的位置添加 echo '&lt;div class="FirstCondition"&gt;';,就会发生这种情况。

它只会重复我添加的 div 容器。我需要的是添加一个简单的 DIV 容器来保存所有符合条件的帖子。

最终的输出是这样的:

<div class="FirstCondition">
    Wordpress Published Post that meets the criteria for the First Condition
    Wordpress Published Post that meets the criteria for the First Condition
    Wordpress Published Post that meets the criteria for the First Condition
    Wordpress Published Post that meets the criteria for the First Condition
</div>

<div class="SecondCondition">
    Wordpress Published Post that meets the criteria for the Second Condition
    Wordpress Published Post that meets the criteria for the Second Condition
    Wordpress Published Post that meets the criteria for the Second Condition
    Wordpress Published Post that meets the criteria for the Second Condition
</div>

<div class="ThirdCondition">
    Wordpress Published Post that meets the criteria for the Third Condition
    Wordpress Published Post that meets the criteria for the Third Condition
    Wordpress Published Post that meets the criteria for the Third Condition
    Wordpress Published Post that meets the criteria for the Third Condition
</div>

<div class="FourthCondition">
    Wordpress Published Post that meets the criteria for the Fourth Condition
    Wordpress Published Post that meets the criteria for the Fourth Condition
    Wordpress Published Post that meets the criteria for the Fourth Condition
    Wordpress Published Post that meets the criteria for the Fourth Condition
</div>

<div class="FifthCondition">
    Wordpress Published Post that meets the criteria for the Fifth Condition
    Wordpress Published Post that meets the criteria for the Fifth Condition
    Wordpress Published Post that meets the criteria for the Fifth Condition
    Wordpress Published Post that meets the criteria for the Fifth Condition
</div>

这可能吗?如何?请帮忙。

【问题讨论】:

    标签: php html wordpress while-loop containers


    【解决方案1】:

    请尝试以下方法:

    第 1 步:循环

    $i=0;
    $items = array();
    if ( have_posts() ) : 
        while ( have_posts() ) : the_post();                    
            switch( ++$i )
            {
                case ( $i <= 3 ):
                    $items['firstcondition'][]  = get_mypart( 'part');
                    break;
                case ( $i <= 9 ):
                    $items['secondcondition'][] = get_mypart( 'part');
                    break;
                case ( $i <= 13 ):
                    $items['thirdcondition'][]  = get_mypart( 'part');
                    break;
                case ( $i <= 15 ):
                    $items['fourthcondition'][] = get_mypart( 'part');
                    break;
                default:
                    $items['fifthcondition'][]  = get_mypart( 'part');
            }
        endwhile; 
    endif;
    

    第 2 步:输出

    输出我们可以使用的部分:

    foreach( $items as $key => $item )
    {
        printf( '<div class="%s">%s</div>', $key, join( ' ', $item ) );
    }
    

    第 3 步:我们的自定义函数

    我们定义我们的自定义函数来返回对应的模板:

    function get_mypart( $part )
    {
        ob_start();
        get_template_part( 'content', sanitize_file_name( $part ) ); 
        return ob_get_clean();
    }
    

    或使用这个答案的好主意here

    function get_mypart( $part )
    {
        return file_get_contents( locate_template( "content-" . sanitize_file_name( $part ) ) );
    }
    

    我们还应该考虑用以下代码包装我们的循环:

    if( function_exists( 'get_mypart' ) ){...}
    

    在我们尝试在循环中使用它之前确保我们的函数存在。

    在我们定义函数时也做类似的检查,只是为了确保它不存在。

    第 4 步:模板部分

    我们在当前主题目录下创建文件content-part.php

    <?php
    /**
     *  Content Part
     *  @file content-part.php
     */
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <h1 class="entry-title"><?php the_title(); ?></h1>
        <div class="entry-content">
            <?php the_content(); ?>
        </div>
    </article>
    

    如果您想要与每个条件对应的不同模板,您可以创建文件:

       content-part1.php
       content-part2.php
       content-part3.php
       content-part4.php
       content-part5.php
    

    并将get_mypart( 'part' )修改为get_mypart( 'part1' )

    第五步:

    喝杯咖啡,享受生活;-)

    ps:Image borrowed from Wikipedia.

    --希望这会有所帮助。

    【讨论】:

    • 尼斯令人印象深刻!!!这正是我正在寻找的!感谢@birgire 分享您的才华 :) 非常感谢您的帮助!
    • 它说我只能在 17 小时后奖励你的 +50……但我保证,我会的!再次感谢您!
    【解决方案2】:

    循环时将div的输出存入变量,循环结束后输出。

    # set the variables
    $firstdiv = '';
    $seconddiv = '';
    
    # inside the loop
    while (have_posts()) {
        if ($i <= 3) {$firstdiv .= the_content();} 
        elseif($i <= 9) {$seconddiv .= the_content();}
        elseif($i <= 13) {$seconddiv .= the_content();}
        else{
             $lastdiv .= the_content();
        }
    }
    
    # after the loop
    echo $firstdiv;
    echo $seconddiv;
    

    【讨论】:

    • 让我试试这种方法.. 感谢您的回答 Toly。
    • 你能做到吗?我不知道把它放在我的 wordpress 循环中的什么地方。这是我的 wp 循环的代码。 pastebin.com/1R2jsWkY好吗?抱歉,我对 PHP 的了解非常有限。
    • 抱歉,这里不是 wordpress 用户。我不确定您的函数 the_post()、the_title() 和 the_permalink() 是做什么的,但如果他们可以选择返回内容而不是输出内容 -只需将它们返回的内容存储到变量中即可。
    • 哦,你显然也有 the_content() 函数。好吧,无论这些函数中的哪一个产生输出 - 您都需要像我在回答中向您展示的那样连接(添加)它。
    • 我明白了,非常感谢您的帮助。我只是不知道如何集成您的代码。
    【解决方案3】:

    检查这个它与您的问题很接近。

        <div class="container">
    <?php if (have_posts()) { ?>
     <?php
     $i = 1; while (have_posts()) : the_post()   ?>
        <div class="firstpost">
        <?php
        if ($i <= 3) {
            the_title();
            the_content();
    ?></div>
     <?php } elseif ($i <= 9){ ?>
        <div class="secondpost" >
            <?php
            the_title();
            the_permalink(); ?>
        </div><?php
        } elseif ($i <= 13){ ?>
        <div class="secondpost" >
            <?php
            the_title();
            the_permalink(); ?>
        </div><?php
      } elseif ($i <= 15){ ?>
        <div class="secondpost" >
            <?php
            the_title();
            the_permalink(); ?>
        </div><?php
    
        } 
        else {?>
           <div class="defalutclass" >
            <?php
            the_title();
           ?>
        </div><?php
        }
        $i++;?>
    
    <h2>No Posts Found</h2> 
    
    <?php endwhile; ?>
     <?php }  ?>
    </div>
    

    谢谢

    【讨论】:

    • 嗨,Narendra,谢谢你的回答,但是如果有,通过使用dreamweaver,
    • 我认为它不起作用......这就是我运行您的代码时发生的情况。 i.stack.imgur.com/hBqo4.png div 第一个帖子应该分组或保持帖子#1 到 3,然后 div 第二个帖子应该分组或保持帖子 4 到 9 等等..
    猜你喜欢
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多