在修改后的 Wordpress 循环中插入 DIV 容器

Kar*_*sca 2 html php wordpress containers while-loop

我正在研究一个修改过的 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>
Run Code Online (Sandbox Code Playgroud)

如果我echo '<div class="FirstCondition">';在我的评论所在的地方添加一个,就会发生这种情况。

在此处输入图片说明

它只重复我添加的 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>
Run Code Online (Sandbox Code Playgroud)

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

bir*_*ire 5

请尝试以下操作:

第 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;
Run Code Online (Sandbox Code Playgroud)

第 2 步:输出

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

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

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

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

function get_mypart( $part )
{
    ob_start();
    get_template_part( 'content', sanitize_file_name( $part ) ); 
    return ob_get_clean();
}
Run Code Online (Sandbox Code Playgroud)

或者在这里使用这个答案的好主意:

function get_mypart( $part )
{
    return file_get_contents( locate_template( "content-" . sanitize_file_name( $part ) ) );
}
Run Code Online (Sandbox Code Playgroud)

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

if( function_exists( 'get_mypart' ) ){...}
Run Code Online (Sandbox Code Playgroud)

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

在定义函数时也做类似的检查,以确保它不存在。

第 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>
Run Code Online (Sandbox Code Playgroud)

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

   content-part1.php
   content-part2.php
   content-part3.php
   content-part4.php
   content-part5.php
Run Code Online (Sandbox Code Playgroud)

并修改get_mypart( 'part' )get_mypart( 'part1' )等。

第 5 步:

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

咖啡

ps:图片借自维基百科

- 希望这可以帮助。