找到你要的答案

Q:Wordpress: Recent post by tags

Q:WordPress:最近张贴的标签

this is my content.php I am trying to list 2 diffrent tag on my home page. 5 post from each tag. Example 5 tags from tag:Football down of it 5 tags from tag:Basketball and here is my content.php Thanks in advance.

<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>

这是我的content.php我想我主页上的2个不同的标签列表。5张贴从每个标签。实例5标签从标签:足球了它从标签5标签:篮球,这是我的content.php谢谢。

<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>

answer1: 回答1:

You should loop the tags and do a query for each one, and for each post insert your existing HTML, like this:

<?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();
}

?>

Just don't forget to insert your existing html instead of the ///.... insert your existing code here block.

你应该环标记和每一个做一个查询,并为每个岗位插入现有的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代替/ / /…在这里插入您现有的代码。

answer2: 回答2:

Try this

<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>

试试这个

<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>
answer3: 回答3:

Thank you everyone!! Finally I got it using this code:

get_header(); ?>	

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

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

			<?php /* Start the Loop */ ?>
			<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();
    ?>
	
	<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>
</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();
    ?>
	
	<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>
</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();
    ?>
	
	<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>
</article>

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

			

		<?php else : ?>

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

		<?php endif; ?>

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

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

谢谢大家!终于我用这个代码了:

get_header(); ?>	

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

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

			<?php /* Start the Loop */ ?>
			<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();
    ?>
	
	<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>
</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();
    ?>
	
	<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>
</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();
    ?>
	
	<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>
</article>

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

			

		<?php else : ?>

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

		<?php endif; ?>

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

<?php get_sidebar(); ?>
<?php get_footer(); ?>
wordpress  list  post  tags