WordPress按标签查询相关帖子
我试图通过使用与当前帖子/页面相同的标签来查询相关帖子,但这也必须在我已经用来生成网格的代码格式内工作。
<?php
$c = 1; //init counter
$bpr = 3; //boxes per row
if(have_posts()) : while (have_posts()) : the_post(); ?>
<div class="postgrid" id="post-<?php the_ID(); ?>">
<div class="postthumb">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('grid-post-image'); ?></a><div class="borderthumb"></div><div class="posttitle"><h1><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
<p><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Click for more</a></p></div>
</div>
</div>
<?php
if($c == $bpr) :
?>
<?php
$c = 0;
endif;
?>
<?php
$c++;
endwhile;
endif;
?>
我发现了这个: Wordpress 按标签查询相关帖子
这看起来很有希望,但是当我尝试集成时它就像......
<?php
$c = 1; //init counter
$bpr = 3; //boxes per row
$test = "";
$posttags = get_the_tags();
$test = '';
$sep = '';
if ($posttags) {
foreach($posttags as $tag) {
$test .= $sep . $tag->name;
$sep = ",";
}
}
query_posts('tag=' .$test . '&showposts=-1'); if(have_posts()) : while (have_posts()) : the_post(); ?>
不幸的是它没有产生任何东西。有什么帮助吗?
谢谢!我认为这两个脚本是冲突的,而且我不是 php 高手。
I am trying to query related posts by using the tags the same to the current post/page, but this also had to work within the code format I am already using to generate a grid.
<?php
$c = 1; //init counter
$bpr = 3; //boxes per row
if(have_posts()) : while (have_posts()) : the_post(); ?>
<div class="postgrid" id="post-<?php the_ID(); ?>">
<div class="postthumb">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('grid-post-image'); ?></a><div class="borderthumb"></div><div class="posttitle"><h1><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
<p><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Click for more</a></p></div>
</div>
</div>
<?php
if($c == $bpr) :
?>
<?php
$c = 0;
endif;
?>
<?php
$c++;
endwhile;
endif;
?>
I found this:
Wordpress Querying Related Posts by tag
Which seemed promising, but when I tried to integrate it like..
<?php
$c = 1; //init counter
$bpr = 3; //boxes per row
$test = "";
$posttags = get_the_tags();
$test = '';
$sep = '';
if ($posttags) {
foreach($posttags as $tag) {
$test .= $sep . $tag->name;
$sep = ",";
}
}
query_posts('tag=' .$test . '&showposts=-1'); if(have_posts()) : while (have_posts()) : the_post(); ?>
it unfortunately generated nothing. any help?
Thanks! I think the two scripts are conflicting and I'm no php whizz.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自
query_posts
的规范:*您不应使用
query_posts()
创建辅助列表(例如,页面底部的相关帖子列表,或侧边栏小部件中的链接列表)。相反,您应该创建 WP_Query 的新实例或使用get_posts()
。*尝试
get_posts()
:您需要确保
$test
实际上是一个有效的标签或一组标签。From the spec for
query_posts
:*You should not use
query_posts()
to create secondary listings (for example, a list of related posts at the bottom of the page, or a list of links in sidebar widget). Instead, you should make a new instance of WP_Query or useget_posts()
.*Try
get_posts()
:You'll need to make sure that
$test
is actually a valid tag or set of tags.