我在一个网站上工作,客户需要一个静态页面来显示特定类别的信息。然后在静态信息下,她希望该类别中的所有帖子都列在2列列表中。
所以我需要有一个按字母顺序排列的列表,链接到选定类别中的所有帖子。我有这样做的代码,但我需要能够将列表分解为列。这是我显示帖子的代码。。。
<?php
if (is_page() ) {
$category = get_post_meta($posts[0]->ID, \'category\', true);
}
if ($category) {
$cat = get_cat_ID($category);
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$post_per_page = -1; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
\'category__in\' => array($cat),
\'orderby\' => \'title\',
\'order\' => \'asc\',
\'paged\' => $paged,
\'posts_per_page\' => $post_per_page,
\'caller_get_posts\' => $do_not_show_stickies
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<_li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><_/li>
我知道我的HTML技能不是最好的,但这里是整个模板页面。。。
https://gist.github.com/889340
最合适的回答,由SO网友:t31os 整理而成
Updated answer:
使用两个浮动列表来模拟列,方法与之前相同。
<?php
/*
Template Name: PageOfPosts
*/
get_header(); ?>
<div id="content">
<div class="t">
<div class="b">
<div class="l">
<div class="r">
<div class="bl">
<div class="br">
<div class="tl">
<div class="tr">
<div class="pad">
<?php while( have_posts() ) : the_post(); ?>
<?php
$category = get_post_meta( get_the_ID(), \'category\', true);
$img_style = \'style="width:606;height:34;position: absolute; padding-top:1px; padding-left: 2px; z-index:9999; background: no-repeat;"\';
?>
<div class="post" id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<img src="../wp-content/themes/DD4L/images/leafhr.png" <?php? echo $img_style;?>>
<br />
<div class="entry">
<?php the_content(); ?>
<?php wp_link_pages(array(\'before\' => \'Pages: \', \'next_or_number\' => \'number\')); ?>
</div>
<?php edit_post_link(\'Edit this entry.\', \'<p>\', \'</p>\'); ?>
</div>
<?php endwhile; ?>
<br /><br />
<?php if( !empty( $category ) ) : ?>
<?php
$args = array(
\'orderby\' => \'title\',
\'order\' => \'asc\',
\'nopaging\' => true,
\'ignore_sticky_posts\' => true,
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'terms\' => array( $category ),
\'field\' => \'slug\'
)
)
);
$category_query = new WP_Query( $args );
?>
<?php if( $category_query->have_posts() ) : ?>
<?php
$total = $category_query->post_count;
$quart = $total / 4;
if( floor( $quart ) != $quart )
$quart = ceil( $quart );
$counter = 0;
?>
<div class="float-container">
<ul class="alignleft">
<?php while( $category_query->have_posts() ) : $category_query->the_post(); $counter++; ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li>
<?php if( $quart == $counter ) : $counter = 0; ?>
</ul>
<ul class="alignleft">
<?php endif; ?>
<?php endwhile; ?>
</ul>
<div class="clear"></div>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
<div class="clear"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
有关以前的注释和代码,请参见编辑修订。