[英]Display latest posts from a specific tag in Wordpress
我正在嘗試更改我的Wordpress主題,以顯示特定標簽的最新帖子。 目前它列出了最新的帖子。
這是它使用的部分代碼:
<?php
if ( is_home() ) {
$args=array(
'showposts'=> (int) get_option('aggregate_homepage_posts'),
'paged'=>$paged,
'category__not_in' => (array) get_option('aggregate_exlcats_recent')
);
if (get_option('aggregate_duplicate') == 'false') {
global $ids;
$args['post__not_in'] = $ids;
}
query_posts($args);
global $paged;
}
$i = 0;
?>
<?php
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$i++;
$et_is_latest_post = ( $paged == 0 && ( is_home() && $i <= 2 ) ) || !is_home();
?>
<div class="post entry clearfix<?php if ( $et_is_latest_post ) echo ' latest'; ?>">
<?php
$thumb = '';
$width = $et_is_latest_post ? 170 : 67;
$height = $et_is_latest_post ? 110 : 67;
$classtext = 'post-thumb';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
$thumb = $thumbnail["thumb"];
?>
<?php if($thumb <> '' && get_option('aggregate_thumbnails_index') == 'on') { ?>
<div class="thumb thumbcont">
<a href="<?php the_permalink(); ?>">
<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
<span class="overlay"></span>
</a>
<a class="thumb<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>" href="<?php bloginfo('url'); ?>/<?php echo $category[0]->category_nicename; ?>"><?php echo $category[0]->cat_name; ?></a>
</div> <!-- end .post-thumbnail -->
<?php } ?>
<h3 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php get_template_part('includes/postinfo'); ?>
<p class="postsummery">
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,20);
?>
</p>
<a href="<?php the_permalink(); ?>" class="more"><span><?php esc_html_e('Read More','Aggregate'); ?></span></a>
</div> <!-- end .post-->
<?php endwhile; ?>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { ?>
<?php get_template_part('includes/navigation','entry'); ?>
<?php } ?>
<?php else : ?>
<?php get_template_part('includes/no-results','entry'); ?>
<?php endif; wp_reset_query(); ?>
在進行一些研究時,我發現了一些有關如何執行此操作的詳細信息: http : //codex.wordpress.org/Template_Tags/get_posts
但是,由於我對PHP編碼的了解非常有限,並且缺乏對當前代碼如何與所提供的示例配合使用的了解,因此我找不到解決方法。 你能指導我嗎?
您只需在數組(在第3行之后,為什么要導致該數組包含參數,然后將其傳遞給調用數據庫的get posts函數)中,簡單地添加'tag'=>您想要列出的標簽,需要選擇的可以在這里找到http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters完整的代碼如下所示
<?php
if ( is_home() ) {
$args=array(
'showposts'=> (int) get_option('aggregate_homepage_posts'),
'paged'=>$paged,
'tag'=>"the tag u want shown",
'category__not_in' => (array) get_option('aggregate_exlcats_recent')
);
if (get_option('aggregate_duplicate') == 'false') {
global $ids;
$args['post__not_in'] = $ids;
}
query_posts($args);
global $paged;
}
$i = 0;
?>
<?php
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$i++;
$et_is_latest_post = ( $paged == 0 && ( is_home() && $i <= 2 ) ) || !is_home();
?>
<div class="post entry clearfix<?php if ( $et_is_latest_post ) echo ' latest'; ?>">
<?php
$thumb = '';
$width = $et_is_latest_post ? 170 : 67;
$height = $et_is_latest_post ? 110 : 67;
$classtext = 'post-thumb';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
$thumb = $thumbnail["thumb"];
?>
<?php if($thumb <> '' && get_option('aggregate_thumbnails_index') == 'on') { ?>
<div class="thumb thumbcont">
<a href="<?php the_permalink(); ?>">
<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
<span class="overlay"></span>
</a>
<a class="thumb<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>" href="<?php bloginfo('url'); ?>/<?php echo $category[0]->category_nicename; ?>"><?php echo $category[0]->cat_name; ?></a>
</div> <!-- end .post-thumbnail -->
<?php } ?>
<h3 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php get_template_part('includes/postinfo'); ?>
<p class="postsummery">
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,20);
?>
</p>
<a href="<?php the_permalink(); ?>" class="more"><span><?php esc_html_e('Read More','Aggregate'); ?></span></a>
</div> <!-- end .post-->
<?php endwhile; ?>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { ?>
<?php get_template_part('includes/navigation','entry'); ?>
<?php } ?>
<?php else : ?>
<?php get_template_part('includes/no-results','entry'); ?>
<?php endif; wp_reset_query(); ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.