在 2 列中按分类法列出自定义 post 类型中的所有 post
List all posts in custom post type by taxonomy in 2 columns
我正在尝试按分类法获取所有自定义帖子,然后将前两个税项放在一列中,其余的放在第二列中。但我的代码循环一切,我得到多个 div。想法?
这是我的代码:
<div class="row">
<?php
$post_type = 'myposttype';
$tax = 'mytaxonomy';
$tax_terms = get_terms($tax,'hide_empty=0');
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args=array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
?>
<div class="column1">
<?php
if( $my_query->have_posts() ) {
if( ($tax_term->name == 'first_taxterm') || ($tax_term->name == 'second_taxterm') ) {
echo "<h2 class=\"tax_term-heading\" id=\"".$tax_term->slug."\"> $tax_term->name </h2>";
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
} else {?>
</div>
<div class="column2">
<?php
echo "<h2 class=\"tax_term-heading\" id=\"".$tax_term->slug."\"> $tax_term->name </h2>";
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile; }?>
</div><?php
}
wp_reset_query();
}
}
?>
-
尝试根据您的 $tax
数量削减您的 wp_query
没办法,所以我使用了这样的多重查询...
<?php
$args=array(
'trend-categories' => 'flowers',
'post_type' => 'trend',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h2> TITLE BIG </h2><ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a class="hey" href="<?php the_permalink() ?>" rel="bookmark" <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
}
wp_reset_query();
?>
</ul>
我正在尝试按分类法获取所有自定义帖子,然后将前两个税项放在一列中,其余的放在第二列中。但我的代码循环一切,我得到多个 div。想法?
这是我的代码:
<div class="row">
<?php
$post_type = 'myposttype';
$tax = 'mytaxonomy';
$tax_terms = get_terms($tax,'hide_empty=0');
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args=array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
?>
<div class="column1">
<?php
if( $my_query->have_posts() ) {
if( ($tax_term->name == 'first_taxterm') || ($tax_term->name == 'second_taxterm') ) {
echo "<h2 class=\"tax_term-heading\" id=\"".$tax_term->slug."\"> $tax_term->name </h2>";
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
} else {?>
</div>
<div class="column2">
<?php
echo "<h2 class=\"tax_term-heading\" id=\"".$tax_term->slug."\"> $tax_term->name </h2>";
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile; }?>
</div><?php
}
wp_reset_query();
}
}
?>
-
尝试根据您的 $tax
数量削减您的 wp_query没办法,所以我使用了这样的多重查询...
<?php
$args=array(
'trend-categories' => 'flowers',
'post_type' => 'trend',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h2> TITLE BIG </h2><ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a class="hey" href="<?php the_permalink() ?>" rel="bookmark" <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
}
wp_reset_query();
?>
</ul>