【问题标题】:display child posts based on category terms根据类别术语显示子帖子
【发布时间】:2016-06-13 11:45:13
【问题描述】:

我有一个名为 city guide 的自定义帖子类型。子帖子文章显示在父页面中。子帖子具有名为 newyork 和 paris 的自定义类别。

<div class="container">
<?php while( have_posts() ): the_post();  ?>
<div class="row main-content-wrap">
  <div class="col-md-12 long-post-sections-wrapper">
<?php while( $city_guide->have_posts() ): $city_guide->the_post(); 
$terms = get_the_terms( $post->ID , 'city-guide-cities-category' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->name ;

} }
 ?> 

<div <?php post_class('city-guide-row row' . classes_related($wp_query->current_post) ); ?>>
    <div class="col-sm-8">
        <h3 id="<?php echo esc_attr( get_post()->post_name ); ?>"><?php the_title(); ?></h3>

        <?php the_post_thumbnail(); ?>
        <div class="img-caption"><?php echo get_post(get_post_thumbnail_id())->post_excerpt; ?></div>
        <?php the_content(); ?>
    </div>

    <div class="col-sm-4 map-container">

        <!-- google map -->
        <div class="map-wrapper">
            <div class="mobile-map-overlay" onClick="style.pointerEvents='none'"></div>
            <iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0"width="100%" height="260" src="https://maps.google.com/maps?hl=en&q=<?php echo get_field('address',get_the_ID()); ?>&ie=UTF8&t=roadmap&z=6&iwloc=B&output=embed&z=16"></iframe>
        </div>

        <h5>Address</h5>
        <?php echo get_field('address'); ?><br/>
        <?php echo get_field('address_2'); ?>

        <h5>Contact</h5>
        <?php if(get_field('contact_number')): 
            echo '<a href="tel:' . get_field('contact_number') . '">' . get_field('contact_number') . '</a>';
        endif; ?><br/>
        <?php if(get_field('website')): 
            echo '<a href="' . get_field('website') . '" target="_blank">' . get_field('website') . '</a>';
        endif; ?><br/>
        <?php if(get_field('email')): 
            echo '<a href="mailto:' . get_field('email') . '">' . get_field('email') . '</a>';
        endif; ?>

    </div>
    <div class="clearfix"></div>
<hr class="city-row-separator"/>
<?php endwhile; ?>
</div>
</div>
<?php endwhile; ?>

我在里面打印了 print $term->name ;,所以每个子文章都显示了自己的类别。

我想要实现的是先显示 newyork 帖子,然后显示 paris

if ( $term->name == 'newyork'){

}
elseif ( $term->name == 'paris'){

}

没有达到我的预期。我该如何实现?请帮助

编辑:

试过了,什么都没有。

 <?php 
foreach ($NY_array as $key => $value) {
$args = array(
'post__in' => $NY_Posts,
);

$posts = get_posts($args);

foreach ($posts as $p) : ?>
<div <?php post_class('city-guide-row row' . classes_related($wp_query->current_post) ); ?>>
    <div class="col-sm-8">
        <h3 id="<?php echo esc_attr( get_post()->post_name ); ?>"><?php $p->post_title; ?></h3>

        <?php the_post_thumbnail(); ?>
        <div class="img-caption"><?php echo get_post(get_post_thumbnail_id())->post_excerpt; ?></div>
        <?php the_content(); ?>
    </div>

    <div class="col-sm-4 map-container">

        <!-- google map -->
        <div class="map-wrapper">
            <div class="mobile-map-overlay" onClick="style.pointerEvents='none'"></div>
            <iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0"width="100%" height="260" src="https://maps.google.com/maps?hl=en&q=<?php echo get_field('address',get_the_ID()); ?>&ie=UTF8&t=roadmap&z=6&iwloc=B&output=embed&z=16"></iframe>
        </div>

        <h5>Address</h5>
        <?php echo get_field('address'); ?><br/>
        <?php echo get_field('address_2'); ?>

        <h5>Contact</h5>
        <?php if(get_field('contact_number')): 
            echo '<a href="tel:' . get_field('contact_number') . '">' . get_field('contact_number') . '</a>';
        endif; ?><br/>
        <?php if(get_field('website')): 
            echo '<a href="' . get_field('website') . '" target="_blank">' . get_field('website') . '</a>';
        endif; ?><br/>
        <?php if(get_field('email')): 
            echo '<a href="mailto:' . get_field('email') . '">' . get_field('email') . '</a>';
        endif; ?>

    </div>
    <div class="clearfix"></div>
    <?php get_template_part( 'templates/shop-the-story-city-guide', 'single'   );  ?>
</div>
<hr class="city-row-separator"/>
<?php 
endforeach;
}
?>

【问题讨论】:

    标签: php wordpress if-statement conditional-statements


    【解决方案1】:

    让我根据您当前的代码指导您完成简单的代码

    虽然不是标准解决方案,但可以完成 要求

    运行循环并以 $NY_array$PA_array 的形式将纽约和巴黎的帖子收集到单独的数组中 以及它们的帖子 ID 也在单独的数组中 $NY_Posts$PA_Posts

    编辑开始

     <?php 
    while( have_posts() ): the_post();
            $terms = get_the_terms( $post->ID , 'city-guide-cities-category' );
             // Loop over each item since it's an array
            if ( $terms != null ){
                foreach( $terms as $term ) {
                    if ( $term->name == 'newyork'){
                        $NY_array[] = array('term_id'=> $term->term_taxonomy_id, 'count' => $term->count);
                        $NY_Posts[] = $post->ID;
                    } 
                    elseif ( $term->name == 'paris'){
                        $PA_array[] = array('term_id'=> $term->term_taxonomy_id, 'count' => $term->count);
                        $PA_Posts[] = $post->ID;
                    }
                }
            }
        endwhile; ?>
    

    编辑结束

    现在运行 2 个单独的循环,首先为纽约,然后为巴黎

    <?php 
    foreach ($NY_array as $key => $value) {
        $args = array(
        'post__in' => $NY_Posts,
        );
    
        $posts = get_posts($args);
    
        foreach ($posts as $p) :
            //do the code here 
            //below code replaced by 
                //$p->post_title;
                //$p->post_excerpt
                //get_field('address',$p->ID); 
                //get_field('address_2',$p->ID); 
                //get_field('contact_number',$p->ID); and so on 
                // try to print print_r($p) if you need to print some data from Post like content, title, featured image etc
        endforeach;
    }
    ?>
    

    同样适用于 PA_array

    <?php foreach ($PA_array as $key => $value) {
        $args = array(
        'post__in' => $PA_Posts,
        );
    
        $posts = get_posts($args);
    
        foreach ($posts as $p) :
            //do the code here 
            //below code replaced by 
                //$p->post_title;
                //$p->post_excerpt
                //get_field('address',$p->ID); 
                //get_field('address_2',$p->ID); 
                //get_field('contact_number',$p->ID); and so on 
                // try to print print_r($p) if you need to print some data from Post like content, title, featured image etc
        endforeach;
    }?>
    

    【讨论】:

    • 你能告诉我哪一部分不明白,我可以编辑一下
    • post_title; ?>

      这给了我 helloworld 作为标题
    • 你能不能试试上面的print_r($NY_Posts,) foreach($NY_array as $key =&gt; $value) {} 让我知道输出
    • 好的..请更正if ( $term-&gt;name == 'newyork'){ $NY_array[] = array('term_id'=&gt; $term-&gt;term_taxonomy_id, 'count' =&gt; $term-&gt;count); $NY_Posts[] = $post-&gt;ID; } elseif ( $term-&gt;name == 'paris'){ $PA_array[] = array('term_id'=&gt; $term-&gt;term_taxonomy_id, 'count' =&gt; $term-&gt;count); $PA_Posts[] = $post-&gt;ID; } 或查看编辑后的答案...第一个代码块已被编辑
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    相关资源
    最近更新 更多