1

这是我拥有的标准 HTML 代码,我正在尝试将其合并到一个 Wordpress 循环中,该循环基本上以两种不同的方式显示帖子。我正在使用更多字段插件供用户选择显示帖子的两种方式之一。1) 如果他们选择大,则帖子将使用 div 类“大链接”进行包装 2) 如果用户选择“小”,则会创建一个具有“groupOflinks”类的 div,并将帖子使用 div 类“smallLink”包装'。一个 'groupOflinks' div 中只能保存 4 个 'smallLink' div's/posts。如果有超过 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 回答