我正在研究一个修改过的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">';,就会发生这种情况。
它只重复我添加的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>

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

最佳答案

请尝试以下操作:
第一步:循环

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

第二步:输出
要输出我们可以使用的部件:
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();
}

或者用这个答案的好主意:
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:
喝杯咖啡,享受生活;-)
附言:here
--希望这有帮助。

07-24 09:46
查看更多