Php 在Wordpress中显示特定标签的最新帖子
我正试图改变我的Wordpress主题,以显示来自特定标签的最新帖子。它目前列出了所有最新的帖子 以下是它使用的部分代码:Php 在Wordpress中显示特定标签的最新帖子,php,wordpress,tags,posts,Php,Wordpress,Tags,Posts,我正试图改变我的Wordpress主题,以显示来自特定标签的最新帖子。它目前列出了所有最新的帖子 以下是它使用的部分代码: <?php if ( is_home() ) { $args=array( 'showposts'=> (int) get_option('aggregate_homepage_posts'), 'paged'=>$paged, 'category__not_i
<?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(); ?>
在做一些研究时,我发现了一些如何做到这一点的细节:
然而,我对PHP编码的知识非常有限,并且不了解我当前的代码如何与提供的示例一起工作,我找不到一种方法来实现它。你能指导我吗?你只需在数组(第3行)后面添加“tag”=>你想要列出的标记 这是一个包含参数的数组,该数组将被传递给调用数据库的get posts函数,您可以在这里找到要选择的参数 完整的代码看起来是这样的
<?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(); ?>
您只需在数组后添加'tag'=>所需的标记u(在第3行,请您详细说明您的想法,我将如何做?如果我的标签是hello,我是否添加“tag”=>hello?@Breezer刚刚尝试过,效果非常好!请您回答这个问题,我会将其标记为正确。这将为您的努力提供几分;)Tack!