2015-05-02 3 views
0

это мой content.php Я пытаюсь перечислить 2 разных тега на моей домашней странице. 5 из каждого тега. Пример 5 тегов из тега: Футбол вниз из него 5 тегов из тега: Баскетбол, и вот мой content.php Спасибо заранее.Wordpress: последнее сообщение от тегов

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 

<?php if (has_post_thumbnail()) : ?> 
    <div class="entry-thumb col-md-4 col-sm-4 col-xs-12"> 
     <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" > 
      <?php the_post_thumbnail('home-thumb'); ?> 
     </a>    
    </div> 
    <?php $has_thumb = "col-md-8 col-sm-8 col-xs-12"; ?> 
<?php else : ?> 
    <?php $has_thumb = ""; ?> 
<?php endif; ?> 

<div class="entry-summary <?php echo $has_thumb; ?>"> 
    <header class="entry-header"> 
     <?php the_title(sprintf('<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url(get_permalink())), '</a></h1>'); ?> 
    </header><!-- .entry-header -->  
    <div class="post-info"> 
     <?php if ('post' == get_post_type()) : ?> 
      <?php areview_posted_on(); ?> 
     <?php endif; ?> 
     <span class="cat-link"> 
      <?php 
       $category = get_the_category(); 
       if($category[0]){ 
        echo '<i class="fa fa-folder"></i><a href="' . esc_url(get_category_link($category[0]->term_id)) . '">' . esc_attr($category[0]->cat_name) . '</a>'; 
       } 
      ?> 
     </span>    
     <?php if(function_exists('yasr_get_overall_rating') && function_exists('cfs') && ($cfs->get('show_stars') == 1)) { 
      echo do_shortcode('[yasr_overall_rating]'); 
     } ?> 
    </div>  
    <?php the_excerpt(); ?> 
</div><!-- .entry-content --> 

<div class="buttons-area"> 
    <?php if (function_exists('cfs') && ($cfs->get('button_link') !='') && ($cfs->get('button_title') !='') && ($cfs->get('button_index') == 1)) : ?> 
     <a href="<?php echo esc_url($cfs->get('button_link')); ?>" class="buy-button" target="_blank"><?php echo esc_html($cfs->get('button_title')); ?></a> 
    <?php endif; ?> 
    <a href="<?php the_permalink(); ?>" class="read-more"><?php echo __('Read more', 'areview'); ?></a> 
</div> 

ответ

1

Вы должны цикл теги и сделать запрос для каждого из них, и для каждого поста вставить существующий HTML, например:

<?php 

$tags = array(
    'Football', 
    'Basketball' 
); 

foreach ($tags as $tag) { 
    query_posts(array(
     'post_type' => 'post' 
     'posts_per_page' => 5, 
     'tax_query' => array(
      'taxonomy' => 'post_tag', 
      'field' => 'name', 
      'terms' => $tag 
     ) 
    )); 

    if (have_posts()) { 
     while(have_posts()) { 
      the_post(); 
      ?> 

      ///.... insert your existing code here 

      <?php  
     } 
    } 

    wp_reset_query(); 
} 

?> 

Только не забудьте вставить ваш существующий html вместо блока ///.... insert your existing code here.

+0

был бы очень хороший ответ, если вы не используете 'query_posts'. Вы должны ** никогда не использовать 'query_posts'. Он разбивает основной запрос –

0

Попробуйте

<div class="relatedposts"> 
<h3>Related posts</h3> 
<?php 
    $args=array(
     'post_status' => 'publish', 
     'tag' => 'football,basketball', //Tag slug 
     'posts_per_page'=>4, // Number of related posts to display. 
    ); 

    $my_query = new wp_query($args); 

    while($my_query->have_posts()) { 
     $my_query->the_post(); 
    ?> 

    <div class="relatedthumb"> 
     <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br /> 
     <?php the_title(); ?> 
     </a> 
    </div> 

    <?php } 
    wp_reset_query(); 
    ?> 
</div> 
0

Спасибо всем !! Наконец, я получил его, используя этот код:

get_header(); ?> \t 
 

 
\t <div id="primary" class="content-area"> 
 
\t \t <main id="main" class="site-main" role="main"> \t \t 
 

 
\t \t <?php if (have_posts()) : ?> 
 

 
\t \t \t <?php /* Start the Loop */ ?> 
 
\t \t \t <div class="relatedposts"> 
 
<h3 class="widget-title">Football</h3> 
 
<div class="decoration-bar"></div><br> 
 
<?php 
 
    $args=array(
 
     'post_status' => 'publish', 
 
     'tag' => 'football', //Tag slug 
 
     'posts_per_page'=>5, // Number of related posts to display. 
 
    ); 
 

 
    $my_query = new wp_query($args); 
 

 
    while($my_query->have_posts()) { 
 
     $my_query->the_post(); 
 
    ?> 
 
\t 
 
\t <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
 

 
\t <?php if (has_post_thumbnail()) : ?> 
 
\t \t <div class="entry-thumb col-md-4 col-sm-4 col-xs-12"> 
 
\t \t \t <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" > 
 
\t \t \t \t <?php the_post_thumbnail('home-thumb'); ?> 
 
\t \t \t </a> \t \t \t 
 
\t \t </div> 
 
\t \t <?php $has_thumb = "col-md-8 col-sm-8 col-xs-12"; ?> 
 
\t <?php else : ?> 
 
\t \t <?php $has_thumb = ""; ?> 
 
\t <?php endif; ?> 
 

 
\t <div class="entry-summary <?php echo $has_thumb; ?>"> 
 
\t \t <header class="entry-header"> 
 
\t \t \t <?php the_title(sprintf('<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url(get_permalink())), '</a></h1>'); ?> 
 
\t \t </header><!-- .entry-header --> \t \t 
 
\t \t <div class="post-info"> 
 
\t \t \t <?php if ('post' == get_post_type()) : ?> 
 
\t \t \t \t <?php areview_posted_on(); ?> 
 
\t \t \t <?php endif; ?> 
 
\t \t \t <span class="cat-link"> 
 
\t \t \t \t <?php 
 
\t \t \t \t \t $category = get_the_category(); 
 
\t \t \t \t \t if($category[0]){ 
 
\t \t \t \t \t \t echo '<i class="fa fa-folder"></i><a href="' . esc_url(get_category_link($category[0]->term_id)) . '">' . esc_attr($category[0]->cat_name) . '</a>'; 
 
\t \t \t \t \t } 
 
\t \t \t \t ?> 
 
\t \t \t </span> \t \t \t \t 
 
\t \t \t <?php if(function_exists('yasr_get_overall_rating') && function_exists('cfs') && ($cfs->get('show_stars') == 1)) { 
 
\t \t \t \t echo do_shortcode('[yasr_overall_rating]'); 
 
\t \t \t } ?> 
 
\t \t </div> \t \t 
 
\t \t <?php the_excerpt(); ?> 
 
\t </div><!-- .entry-content --> 
 

 
\t <div class="buttons-area"> 
 
\t \t <?php if (function_exists('cfs') && ($cfs->get('button_link') !='') && ($cfs->get('button_title') !='') && ($cfs->get('button_index') == 1)) : ?> 
 
\t \t \t <a href="<?php echo esc_url($cfs->get('button_link')); ?>" class="buy-button" target="_blank"><?php echo esc_html($cfs->get('button_title')); ?></a> 
 
\t \t <?php endif; ?> 
 
\t \t <a href="<?php the_permalink(); ?>" class="read-more"><?php echo __('Read more', 'areview'); ?></a> 
 
\t </div> 
 
</article> 
 

 
    <?php } 
 
    wp_reset_query(); 
 
    ?> 
 
</div> 
 

 

 
<center><h4 >---> <a href="/tag/football/">More Posts</a> <---</h4></center> 
 

 

 

 

 
<div class="relatedposts"> 
 
<h3 class="widget-title">Basketball</h3> 
 
<div class="decoration-bar"></div><br> 
 
<?php 
 
    $args=array(
 
     'post_status' => 'publish', 
 
     'tag' => 'basketball', //Tag slug 
 
     'posts_per_page'=>3, // Number of related posts to display. 
 
    ); 
 

 
    $my_query = new wp_query($args); 
 

 
    while($my_query->have_posts()) { 
 
     $my_query->the_post(); 
 
    ?> 
 
\t 
 
\t <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
 

 
\t <?php if (has_post_thumbnail()) : ?> 
 
\t \t <div class="entry-thumb col-md-4 col-sm-4 col-xs-12"> 
 
\t \t \t <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" > 
 
\t \t \t \t <?php the_post_thumbnail('home-thumb'); ?> 
 
\t \t \t </a> \t \t \t 
 
\t \t </div> 
 
\t \t <?php $has_thumb = "col-md-8 col-sm-8 col-xs-12"; ?> 
 
\t <?php else : ?> 
 
\t \t <?php $has_thumb = ""; ?> 
 
\t <?php endif; ?> 
 

 
\t <div class="entry-summary <?php echo $has_thumb; ?>"> 
 
\t \t <header class="entry-header"> 
 
\t \t \t <?php the_title(sprintf('<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url(get_permalink())), '</a></h1>'); ?> 
 
\t \t </header><!-- .entry-header --> \t \t 
 
\t \t <div class="post-info"> 
 
\t \t \t <?php if ('post' == get_post_type()) : ?> 
 
\t \t \t \t <?php areview_posted_on(); ?> 
 
\t \t \t <?php endif; ?> 
 
\t \t \t <span class="cat-link"> 
 
\t \t \t \t <?php 
 
\t \t \t \t \t $category = get_the_category(); 
 
\t \t \t \t \t if($category[0]){ 
 
\t \t \t \t \t \t echo '<i class="fa fa-folder"></i><a href="' . esc_url(get_category_link($category[0]->term_id)) . '">' . esc_attr($category[0]->cat_name) . '</a>'; 
 
\t \t \t \t \t } 
 
\t \t \t \t ?> 
 
\t \t \t </span> \t \t \t \t 
 
\t \t \t <?php if(function_exists('yasr_get_overall_rating') && function_exists('cfs') && ($cfs->get('show_stars') == 1)) { 
 
\t \t \t \t echo do_shortcode('[yasr_overall_rating]'); 
 
\t \t \t } ?> 
 
\t \t </div> \t \t 
 
\t \t <?php the_excerpt(); ?> 
 
\t </div><!-- .entry-content --> 
 

 
\t <div class="buttons-area"> 
 
\t \t <?php if (function_exists('cfs') && ($cfs->get('button_link') !='') && ($cfs->get('button_title') !='') && ($cfs->get('button_index') == 1)) : ?> 
 
\t \t \t <a href="<?php echo esc_url($cfs->get('button_link')); ?>" class="buy-button" target="_blank"><?php echo esc_html($cfs->get('button_title')); ?></a> 
 
\t \t <?php endif; ?> 
 
\t \t <a href="<?php the_permalink(); ?>" class="read-more"><?php echo __('Read more', 'areview'); ?></a> 
 
\t </div> 
 
</article> 
 

 
    <?php } 
 
    wp_reset_query(); 
 
    ?> 
 
</div> 
 
<center><h4 >---> <a href="/tag/basketball/">More Posts</a> <---</h4></center> 
 
<div class="relatedposts"> 
 
<h3 class="widget-title">Handball</h3> 
 
<div class="decoration-bar"></div><br> 
 
<?php 
 
    $args=array(
 
     'post_status' => 'publish', 
 
     'tag' => 'handball', //Tag slug 
 
     'posts_per_page'=>2, // Number of related posts to display. 
 
    ); 
 

 
    $my_query = new wp_query($args); 
 

 
    while($my_query->have_posts()) { 
 
     $my_query->the_post(); 
 
    ?> 
 
\t 
 
\t <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
 

 
\t <?php if (has_post_thumbnail()) : ?> 
 
\t \t <div class="entry-thumb col-md-4 col-sm-4 col-xs-12"> 
 
\t \t \t <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" > 
 
\t \t \t \t <?php the_post_thumbnail('home-thumb'); ?> 
 
\t \t \t </a> \t \t \t 
 
\t \t </div> 
 
\t \t <?php $has_thumb = "col-md-8 col-sm-8 col-xs-12"; ?> 
 
\t <?php else : ?> 
 
\t \t <?php $has_thumb = ""; ?> 
 
\t <?php endif; ?> 
 

 
\t <div class="entry-summary <?php echo $has_thumb; ?>"> 
 
\t \t <header class="entry-header"> 
 
\t \t \t <?php the_title(sprintf('<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url(get_permalink())), '</a></h1>'); ?> 
 
\t \t </header><!-- .entry-header --> \t \t 
 
\t \t <div class="post-info"> 
 
\t \t \t <?php if ('post' == get_post_type()) : ?> 
 
\t \t \t \t <?php areview_posted_on(); ?> 
 
\t \t \t <?php endif; ?> 
 
\t \t \t <span class="cat-link"> 
 
\t \t \t \t <?php 
 
\t \t \t \t \t $category = get_the_category(); 
 
\t \t \t \t \t if($category[0]){ 
 
\t \t \t \t \t \t echo '<i class="fa fa-folder"></i><a href="' . esc_url(get_category_link($category[0]->term_id)) . '">' . esc_attr($category[0]->cat_name) . '</a>'; 
 
\t \t \t \t \t } 
 
\t \t \t \t ?> 
 
\t \t \t </span> \t \t \t \t 
 
\t \t \t <?php if(function_exists('yasr_get_overall_rating') && function_exists('cfs') && ($cfs->get('show_stars') == 1)) { 
 
\t \t \t \t echo do_shortcode('[yasr_overall_rating]'); 
 
\t \t \t } ?> 
 
\t \t </div> \t \t 
 
\t \t <?php the_excerpt(); ?> 
 
\t </div><!-- .entry-content --> 
 

 
\t <div class="buttons-area"> 
 
\t \t <?php if (function_exists('cfs') && ($cfs->get('button_link') !='') && ($cfs->get('button_title') !='') && ($cfs->get('button_index') == 1)) : ?> 
 
\t \t \t <a href="<?php echo esc_url($cfs->get('button_link')); ?>" class="buy-button" target="_blank"><?php echo esc_html($cfs->get('button_title')); ?></a> 
 
\t \t <?php endif; ?> 
 
\t \t <a href="<?php the_permalink(); ?>" class="read-more"><?php echo __('Read more', 'areview'); ?></a> 
 
\t </div> 
 
</article> 
 

 
    <?php } 
 
    wp_reset_query(); 
 
    ?> 
 
</div> 
 
<center><h4 >---> <a href="/tag/handball/">More Posts</a> <---</h4></center> 
 

 
\t \t \t 
 

 
\t \t <?php else : ?> 
 

 
\t \t \t <?php get_template_part('content', 'none'); ?> 
 

 
\t \t <?php endif; ?> 
 

 
\t \t </main><!-- #main --> 
 
\t </div><!-- #primary --> 
 

 
<?php get_sidebar(); ?> 
 
<?php get_footer(); ?>

Смежные вопросы