1

これは私が持っている標準の HTML コードで、基本的に 2 つの異なる方法で投稿を表示する Wordpress ループにマージしようとしています。ユーザーが投稿を表示する2つの方法のいずれかを選択するために、より多くのフィールドプラグインを使用しています。1) 大を選択した場合、投稿は div クラス「大リンク」でラップされます 2) ユーザーが「小」を選択した場合、クラス「groupOflinks」の div が作成され、投稿は div クラス「smallLink」でラップされます'。1 つの「groupOflinks」div 内に保持できるのは、4 つの「smallLink」div/投稿のみです。小さい投稿が 4 つ以上ある場合は、新しい「groupOflinks」div が作成され、プロセスが繰り返されます。

マージしようとしているHTMLコードは次のとおりです(多くのコメントがあります):

<!-- Display one post within this container only & if there are more posts that have been chosen as Large, wrap them in the 'largeLink' div -->

<div class="largeLink">
    <!-- post 1 content here -->
</div>

<div class="largeLink">
    <!-- post 2 content here -->
</div>

<!-- If a post is defined as small within the admin panel (using more fields plugin) 
     display them within this 'groupoflinks' container 
     If 4 posts are already in this 'groupoflinks' container create a new container and populate with another 4 posts 
     Repeat -->

<div class="groupOfLinks">
    <!-- This is a container that holds 4 posts only & if there are more posts that have been assigned as 'Small' create a new 'groupOflinks' container and populate with the next 4 -->
    <div class="smallLink">
        <!-- Post 4 content here -->
    </div>
    <div class="smallLink">
        <!-- Post 5 content here -->
    </div>
    <div class="smallLink">
        <!-- Post 6 content here -->
    </div>
    <div class="smallLink">
        <!-- Post 7 content here -->
    </div>
</div>

<div class="largeLink">
    <!-- post 3 content here -->
</div>

<div class="largeLink">
    <!-- post 8 content here -->
</div>

これは、これまでのところうまくいかない私の WP ループです。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php // Take the value defined with the 'Layout' field and change style accordingly
$layouttype = get_post_meta($post->ID, 'layout-type', true) ?>
<?php if ($layouttype == 'Small') { ?>

<div class="groupOfLinks">

    <!--LOOP STYLE 1 GOES HERE-->
    <?php $temp_query = $wp_query;  // store it
        $args = array(
        'paged' => $paged, // paginates
        'post_type'=>'post',
        'order' => 'DESC'
        );
        $wp_query = new WP_Query($args);
        while ($wp_query->have_posts()) : $wp_query->the_post();?>

        <div class="smallLink">
                        <!-- Post content here -->
                        <h1><?php the_title(); ?></h1>
            <?php the_content(''); ?>
                    </div>


        <?php if (isset($wp_query)) {$wp_query = $temp_query;} // restore loop
              endwhile; ?>

</div>

<?php } else { ?>

<!--LOOP STYLE 2 GOES HERE-->

<div class="largeLink">
        <!-- post 1 content here -->
        <h1><?php the_title(); ?></h1>
        <?php the_content(''); ?>
    </div>


<?php } ?>


<?php endwhile; ?>

<div>
<div><?php next_posts_link('&laquo; Older Entries') ?></div>
<div><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div>

<?php else : ?>

<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn’t here.</p>

<?php endif; ?>
4

1 に答える 1

2

数分後に会議がありますので、時間の都合上、正しい方向に進みましょう。急いで書いたので、多少の調整が必要になる場合があります。ループから始めて、いくつかの簡単な変更を加えてみましょう。

<?php
//Set a counter to determine if small post has been used
$small_posts = 0;
if(have_posts()) :
    while(have_posts()): the_post();
    $layouttype = get_post_meta(get_the_ID(), 'layout-type', true);
    if($layouttype == 'Small') :
        if(($small_posts % 4) == 0) :
            $tmp_query = clone $wp_query; ?>//this line was edited
            <div class="groupOfLinks">
                <div class="smallLink">
                    <h1><?php the_title(); ?></h1>
                    <?php the_content(); ?>
                </div>
                <?php $i = 1;
                while(have_posts() && $i <=4) : the_post(); 
                    $layouttype = get_post_meta(get_the_ID(), 'layout-type', true);
                    if($layouttype == 'Small') : ?>
                        <div class="smallLink">
                            <h1><?php the_title(); ?></h1>
                            <?php the_content(); ?>
                        </div>
                        <?php $i++; 
                   endif;
               endwhile; ?>
               </div>
               <?php $wp_query = clone $tmp_query;//this line was edited
        endif;//End counter check
        //Whether we needed to enter the subloop or not, the counter is incremented
        $small_posts++;
    else: //Else for if($layouttype == 'Small') ?>
        <div class="largeLink">
            <h1><?php the_title(); ?></h1>
            <?php the_content(); ?>
        </div>
   <?php endif;
   endwhile; ?>
<div>
    <div><?php next_posts_link('&laquo; Older Entries') ?></div>
    <div><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div>
<?php else : ?>
    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn’t here.</p>
<?php endif; ?>
于 2012-05-23T15:17:33.650 に答える