我有循环问题

时间:2017-10-05 05:41:22

标签: php wordpress

在我的代码中,我正在获取代表类别ID的数据。但问题是<div id="<?php echo $term_id = $term->term_id;?>" class="tabcontent">。 $ term_id在这个div中不会改变。这是我的代码。

<?php
                         //loop the names of the slugs for the portfolio_categories
$terms = get_terms( array ( 'taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC' ));
foreach ($terms as $term) {
  $slug= $term->slug;
  $term_id = $term->term_id;
$args = array(                          
'post_type' => 'Portfolio',
'tax_query' => array(
    array(
        'taxonomy' => 'portfolio_categories',
        'terms' => $slug,
        'field' => 'slug',
    )
),
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => -1
);
}
?>
<?php 
$posts_query = new WP_Query( $args );
 if (have_posts()) :?>
<div id="<?php echo $term_id = $term->term_id;?>" class="tabcontent">
<?php
while (   $posts_query->have_posts() ) : $posts_query->the_post();?>
   <?php echo '<div class="col-1-3">';?>
   <?php echo $term_id = $term->term_id;?>     
   <?php $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "size" );?>
<div class="wrap-col" >
<a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>   
<img src="<?php echo $thumbnail[0];?>"></a> </div>
 </div>
 <?php echo '</div>';?>
<?phpendwhile;endif;?>
 </div>

2 个答案:

答案 0 :(得分:1)

大家好我已经解决了这个问题。问题只是.div没有在for循环中管理。所以,如果你想通过自定义类别获得自定义帖子,这就是答案。

<?php
$taxonomy = 'portfolio_categories';
$args = array(
    'hide_empty' => false
);
global $terms;
global $terms;
$terms = get_terms( 'portfolio_categories', $args );
?>
   <div class="tab">
    <?php $i = 0; ?>
        <?php foreach ( $terms as $term ) { ?>
        <?php   $i++; ?>
       <button class="tablinks" onclick="openCity(event, '<?php echo $term->term_id;?>')" 

        <?php if ($i == 1) { echo 'id="defaultOpen"'; } ?> ><?php echo $term->name;?></button>
        <?php } ?>
</div>
<?php
//loop the names of the slugs for the portfolio_categories
$terms = get_terms(array('taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC'));
foreach ($terms as $term) {
    $slug = $term->slug;
    $term_id = $term->term_id;
    $args = array(
        'post_type' => 'Portfolio',
        'tax_query' => array(
            array(
                'taxonomy' => 'portfolio_categories',
                'terms' => $slug,
                'field' => 'slug',
            )
        ),
        'orderby' => 'ID',
        'order' => 'DESC',
        'posts_per_page' => -1
    );
//}//Instead closing of here
?>
<?php
$posts_query = new WP_Query($args);
if (have_posts()) :
?>
    <div id="<?php echo $term_id = $term->term_id; ?>" class="tabcontent">
    <?php while ($posts_query->have_posts()) : $posts_query->the_post(); ?>

        <?php echo '<div class="col-1-3">'; ?>
        <?php echo $term_id = $term->term_id; ?>     
        <?php $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "size"); ?>
        <div class="wrap-col" >
        <a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>   
        <img src="<?php echo $thumbnail[0]; ?>"></a> </div>

        <?php echo '</div>'; ?>
        <?php
    endwhile;
endif;?>
  </div>  
<?php }//Close foreach here
?>

答案 1 :(得分:0)

为了得到每个词 - &gt; term_id在最后关闭foreach

//loop the names of the slugs for the portfolio_categories
$terms = get_terms(array('taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC'));
foreach ($terms as $term) {
    $slug = $term->slug;
    $term_id = $term->term_id;
    $args = array(
        'post_type' => 'Portfolio',
        'tax_query' => array(
            array(
                'taxonomy' => 'portfolio_categories',
                'terms' => $slug,
                'field' => 'slug',
            )
        ),
        'orderby' => 'ID',
        'order' => 'DESC',
        'posts_per_page' => -1
    );
//}//Instead closing of here
?>
<?php
$posts_query = new WP_Query($args);
if (have_posts()) :
?>
    <div id="<?php echo $term_id = $term->term_id; ?>" class="tabcontent">
    <?php while ($posts_query->have_posts()) : $posts_query->the_post(); ?>
        <?php echo '<div class="col-1-3">'; ?>
        <?php echo $term_id = $term->term_id; ?>     
        <?php $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "size"); ?>
        <div class="wrap-col" >
        <a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>   
        <img src="<?php echo $thumbnail[0]; ?>"></a> </div>
         </div>
        <?php echo '</div>'; ?>
        <?php
    endwhile;
endif;

}//Close foreach here
?>
 </div>