Modifying a Loop to Show More

时间:2013-02-19 作者:Danny Michel

此循环显示主题选项中指定的特定类别中超过4个帖子标题的一篇特色帖子。

我想做的是显示4个标题中的所有4个帖子,在4个标题之上。

这4个标题来自这段代码

<a class="listtitle" href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf( esc_attr__( \'Permalink to %s\', \'wpnewspaper\' ), the_title_attribute( \'echo=0\' ) ); ?>" ><?php the_title(); ?></a>
这里是完整的循环代码。

<div class="col">
<?php gab_dynamic_sidebar(\'Subnews1\');?>

<?php
$postnr = of_get_option(\'of_wn_nr8\') + of_get_option(\'of_wn_nr8a\');
if (intval($postnr) > 0 ) { ?>
    <span class="catname"><a href="<?php echo get_category_link(of_get_option(\'of_wn_cat8\'));?>"><?php echo get_cat_name(of_get_option(\'of_wn_cat8\')); ?></a></span>
    <?php
    $count = 1;
    $args = array(
     \'post__not_in\'=>$do_not_duplicate,
      \'posts_per_page\' => $postnr,
      \'cat\' => of_get_option(\'of_wn_cat8\', 1)
    );
    $gab_query = new WP_Query();$gab_query->query($args); 
    while ($gab_query->have_posts()) : $gab_query->the_post(); if (of_get_option(\'of_dnd\') == 1) { $do_not_duplicate[] = $post->ID; }
    ?>
    <div class="featuredpost<?php if($count == of_get_option(\'of_wn_nr8\') or $count == $postnr) { echo \' lastpost\'; } ?>">


        <?php if ($count <= of_get_option(\'of_wn_nr8\')) { ?>

            <h2 class="posttitle">
                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf( esc_attr__( \'Permalink to %s\', \'wpnewspaper\' ), the_title_attribute( \'echo=0\' ) ); ?>" ><?php the_title(); ?></a>
            </h2>                       
            <div class="opinion_wrapper">
            <?php
                gab_media(array(   \'imgtag\' => 1,   \'link\' => 1,
                    \'name\' => \'wn-subnews\',
                    \'enable_video\' => 0,
                    \'catch_image\' => of_get_option(\'of_catch_img\', 0),
                    \'enable_thumb\' => 1,
                    \'resize_type\' => \'c\',
                    \'media_width\' => 120, 
                    \'media_height\' => 103, 
                    \'thumb_align\' => \'alignleft\',
                    \'enable_default\' => 0
                ));
            ?>  
            <div class="opinion_marker"><!-- opinion marker --></div>                   
            </div>
            <p><?php echo string_limit_words(get_the_excerpt(),27); ?>&hellip;</p>

            <?php gab_postmeta(); ?>
        <?php } else { ?>

            <a class="listtitle" href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf( esc_attr__( \'Permalink to %s\', \'wpnewspaper\' ), the_title_attribute( \'echo=0\' ) ); ?>" ><?php the_title(); ?></a>

        <?php } ?>

    </div><!-- .featuredpost -->
    <?php $count++; endwhile; wp_reset_query(); ?>
<?php } ?>

1 个回复
最合适的回答,由SO网友:Jen 整理而成

你试过设置吗$count 第8行等于4?像这样:

<div class="col">
<?php gab_dynamic_sidebar(\'Subnews1\');?>

<?php
$postnr = of_get_option(\'of_wn_nr8\') + of_get_option(\'of_wn_nr8a\');
if (intval($postnr) > 0 ) { ?>
    <span class="catname"><a href="<?php echo get_category_link(of_get_option(\'of_wn_cat8\'));?>"><?php echo get_cat_name(of_get_option(\'of_wn_cat8\')); ?></a></span>
    <?php
    $count = 4;
    // etc

结束

相关推荐

Multiple loops are not reset

我可爱的同事们再次设计了一款让我很难在Wordpress中找到它的东西。令人惊讶的是,这是我以前做过多次的事情;在一个页面上有最近的帖子和页面内容。主页是一个页面。在页面内容上方有三篇最近的帖子。对于帖子,我需要在<!--more--> 标签这个循环似乎工作得很好。稍后,在模板中,我循环查看实际页面内容。无论我做什么,它总是给我不可靠的结果。这是我的索引的精简版本。php/页。php模板(它们恰好相同):<div id=\"content\"> <?php g