我正在寻找他们在这里使用的东西:
http://www.jennyreviews.com/as-seen-on-tv/triple-joint-formula/
查看“相关产品评论”我检查了源代码,但找不到列出的插件代码。也许是一个小部件?
我正在寻找他们在这里使用的东西:
http://www.jennyreviews.com/as-seen-on-tv/triple-joint-formula/
查看“相关产品评论”我检查了源代码,但找不到列出的插件代码。也许是一个小部件?
@Scott B,
看起来他们正在使用基于类别的相关帖子查询。当显示单个项目(帖子)时,查询将查找类别id,然后显示来自同一类别的帖子。
Add The following code to your sidebar or even to the bottom of single.php depending on where you want to display the "Related Posts"
<!--Begin Related Posts-->
<?php
if ( is_single() ) :
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
$posts = get_posts(\'numberposts=4&exclude=\' . $GLOBALS[\'current_id\'] . \'&category=\'. $category->term_id);
//To change the number of posts, edit the \'numberposts\' parameter above
if(count($posts) > 1) {
?>
<div class="widget" id="more-category">
<h3 class="widgettitle"><?php _e(\'More in\',\'\'); ?> ‘<?php echo $category->name; ?>’</h3>
<ul>
<?php foreach($posts as $post) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php } ?>
<?php endforeach; ?>
<?php endif; ?>
<!--/related posts-->
这样做的目的是首先获取当前显示的帖子的类别,从查询中省略当前项目,然后进行检查以确保该类别中有1篇以上的帖子。如果有,那么它将输出“更多在您的类别名称”后面的永久链接到帖子。如果要显示特色图像,可以将最后一部分更改为如下所示:
<div class="widget" id="more-category">
<h3 class="widgettitle"><?php _e(\'More in\',\'\'); ?> ‘<?php echo $category->name; ?>’</h3>
<ul>
<?php foreach($posts as $post) : ?>
<li><?php the_post_thumbnail(); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
</div>
我在这里指出了@Scott B所显示的代码中的一个错误。
Global$post实际上是WordPress用于在博客上显示数据的数组。但在这段代码中,$post变量中的数组值将转到当前类别中的最后一篇文章。所以在成功显示帖子列表之后。当你看到评论时,它总是针对列表中提到的最后一篇文章。因此,如果您访问当前类别中的第一篇帖子,评论框将是新生成的帖子列表中的最后一篇帖子。在考虑了一些之后,我找到了一个解决方案,并添加了另一个变量来存储全局$post数组。最后,我将这些值分配回global$post。通过这种方式,global post将其返回当前值,从而返回正确的注释框。如果有人需要,下面是更正的代码。
<?php
////////////////////////////////////////////////////////////////
// THE CODE FOR RELATED ARTICLES FROM CURRENT CATAGORY STARTS //
////////////////////////////////////////////////////////////////
if ( is_single() );
global $post;
$current_post = $post;
$category = get_the_category($post->ID);
$posts = get_posts ( \'numberposts=5&exclude=\'
. $GLOBALS[\'current_id\']
. \'&category=\'
. $category->term_id
. \'&post=\'
. $post->term_id
);
//To change the number of posts, edit the \'numberposts\' parameter above
if(count($posts) > 1)
{
?>
<div class="widget more-category">
<h2>
Related Articles
</h2>
<ul>
<?php
foreach($posts as $post)
{
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php
}
?>
</ul>
</div>
<?php
}
$post = $current_post;
//////////////////////////////////////////////////////////////
// THE CODE FOR RELATED ARTICLES FROM CURRENT CATEGORY ENDS //
//////////////////////////////////////////////////////////////
?>
以下是通过标签发布的与小功能列表相关的帖子
// related post with wordpress-tags
// wordpress > 2.3
function fbbl_related_posts($limit = 5) {
global $wpdb, $post, $table_prefix;
if ($post->ID) {
$retval = \'<ul>\';
// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
}
$tagslist = implode(\',\', $tagsarray);
// Do the query
$q = "SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy =\'post_tag\' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID
AND p.post_status = \'publish\'
AND p.post_date_gmt < NOW()
GROUP BY tr.object_id
ORDER BY count DESC, p.post_date_gmt DESC
LIMIT $limit;";
$related = $wpdb->get_results($q);
if ( $related ) {
foreach($related as $r) {
$retval .= \'<li><a rel="nofollow" title="\' . wptexturize($r->post_title) . \'" href="\' . get_permalink($r->ID) . \'">\' . wptexturize($r->post_title) . \'</a></li>\';
}
} else {
$retval .= \'<li>\' . __(\'no related posts\', TEXTDOMAIN) . \'</li>\';
} $retval .= \'</ul>\';
echo $retval;
}
return;
}
备选方案IV您可以使用类别:/**
* related post with category
* @param: int $limit limit of posts
* @param: bool $catName echo category name
* @param: string $title string before all entries
* Example: echo fb_cat_related_posts();
*/
if ( !function_exists(\'fb_get_cat_related_posts\') ) {
function fb_get_cat_related_posts( $limit = 5, $catName = FALSE, $title = \'<h3>Ähnliche Beiträge</h3>\' ) {
global $post;
$limit = (int) $limit;
$output = \'\';
$output .= $title;
$category = get_the_category();
$category = (int) $category[0]->cat_ID;
if ( $catName )
$output .= __( \'Category: \', FB_BASIS_TEXTDOMAIN ) . get_cat_name($category) . \' \';
$output .= \'<ul>\';
$args = array(
\'numberposts\' => $limit + 1,
\'category\' => $category,
);
$recentposts = get_posts( $args );
foreach($recentposts as $catpost) {
if ($catpost->ID !== $post->ID) {
setup_postdata($catpost);
$output .= \'<li><a href="\' . get_permalink($catpost->ID) . \'">\' . get_the_title($catpost->ID) . \'</a></li>\';
}
}
$output .= \'</ul>\';
return $output;
}
}
"Yet Another Related Posts Plugin“应该做你想做的。只需配置相关的帖子模板以显示帖子缩略图和标题。
作为另一个相关帖子插件的作者,我当然会推荐该插件。:)(感谢史蒂夫和道格指出这一点。)我一次又一次得到的反馈是,从YARPP收到的“相关”结果往往更好地反映了“相关”的直观概念,这主要是因为YARPP的算法不仅关注共享类别,还关注共享标签、标题中的共享关键字和内容中的共享关键字。
提供了有关上述自定义模板功能的教程here. 如果您对如何将YARPP集成到您的设置中有任何疑问,我(和许多其他YARPP用户)可以在wordpress上提供帮助。YARPP的组织论坛部分。
我对插件创作还很陌生,调试也很困难。我用了很多echo,它又脏又丑。我确信有更好的方法可以做到这一点,也许是一个带有调试器的IDE,我可以在其中运行整个站点,包括插件?