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

时间:2014-04-12 05:43:42

标签: php html wordpress while-loop containers

我正在修改一个改进的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>

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

enter image description here

它只重复我添加的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>

这可能吗?怎么样?请帮忙。

3 个答案:

答案 0 :(得分:4)

请尝试以下方法:

第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' )等。

第5步:

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

Coffee

ps:Image borrowed from Wikipedia

- 希望这有帮助。

答案 1 :(得分: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;

答案 2 :(得分:0)

检查一下它是否接近你的问题。

    <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>

由于