如何修改此博客循环以显示自定义帖子类型的帖子?

时间:2012-07-31 作者:James Banks

下面是我生成两列博客索引页的代码。我想对其进行修改,以便将其用作公文包自定义帖子类型-query\\u posts(“post\\u type=公文包”)的索引页。

我知道我需要修改代码的第一节,但我不知道要修改什么。你能帮帮我吗?

代码:

 <?php
/*
Template Name: 2 Column Blog Template
*/
get_header();
?>
<section class="container">
<!-- 960 Container -->
    <?php
    $counter = 1; //start counter
    $grids = 2; //Grids per row
    global $query_string; //Need this to make pagination work
    $paged = get_query_var(\'paged\');
             $offset = 0;
                if ($paged != 0 ) {
                    $offset = ($paged-1) * get_query_var(\'posts_per_page\') ;
                }
                query_posts(\'offset=\' . $offset);
                if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
global $more;
$more = 0;
?>
<?php
//Show the first post
if($counter == 1) :
?>
    <article class="eight columns" id="entry-<?php the_ID(); ?>"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
        <?php the_post_thumbnail(); ?>
        </a>
        <h2><a href="<?php the_permalink() ?>">
            <?php the_title(); ?>
            </a></h2>
        <p class="blog_meta">
            By <em><?php the_author() ?></em> on:<?php the_time(\'F jS, Y\') ?>
        </p>
        <p class="blog_preview">
            <?php the_excerpt(); ?>
        </p>
    </article>
<?php
//Show the last post
elseif($counter == $grids) :
?>
    <article class="eight columns" id="entry-<?php the_ID(); ?>"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
        <?php the_post_thumbnail(); ?>
        </a>
        <h2><a href="<?php the_permalink() ?>">
            <?php the_title(); ?>
            </a></h2>
        <p class="blog_meta">
            By <em><?php the_author() ?></em> on:<?php the_time(\'F jS, Y\') ?>
        </p>
        <p class="blog_preview">
            <?php the_excerpt(); ?>
        </p>
    </article>
    <hr class="clear"><?php $counter = 0; endif;?><?php $counter++; endwhile; //sets a clear:both and resets counter for next row of posts ?>
    <nav id="pagination">
        <hr class="pagination_margin">
        <div id="pagination_previous">
            <?php previous_posts_link(\'Previous\'); ?>
        </div>
        <div id="pagination_next">
            <?php next_posts_link(\'Next\'); ?>
        </div>
    </nav>
    <?php endif; ?>
</div>
</section>
<?php
get_footer();
?>

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

修改代码以使用正确的query\\u posts调用。使用

query_posts(\'post_type=portfolio&offset=\' . $offset);
而不是

query_posts(\'offset=\' . $offset);
下面是您修改的代码:

<?php
/*
Template Name: 2 Column Blog Template
*/
get_header();
?>
<section class="container">
<!-- 960 Container -->
    <?php
    $counter = 1; //start counter
    $grids = 2; //Grids per row
    global $query_string; //Need this to make pagination work
    $paged = get_query_var(\'paged\');
             $offset = 0;
                if ($paged != 0 ) {
                    $offset = ($paged-1) * get_query_var(\'posts_per_page\') ;
                }
                query_posts(\'offset=\' . $offset);
                if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
global $more;
$more = 0;
...
query\\u posts的完整codex手册是here

结束

相关推荐