我正在创建一个插件(我的第一个插件),它允许站点管理员显示过去X天内使用最多的帖子标签。这个插件是WP开发问题的演变-Display list of most used tags in the last 30 days.
请原谅我不懂正确的术语。
我已经用管理菜单等完成了大部分腿部工作,我唯一剩下的就是实际显示结果。
以下代码片段在我将其放入single中时可以正常工作。php文件。
<div class="recent-popular-tags">
<h3><?php echo $rpt_options[\'heading_tags\']; ?></h3>
<?php $wpdb->show_errors(); ?>
<?php
global $rpt_options;
$rpt_days = $rpt_options[\'number_days\'];
global $wpdb;
$rpt_term_ids = $wpdb->get_col("
SELECT term_id FROM $wpdb->term_taxonomy
INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id=$wpdb->term_relationships.term_taxonomy_id
INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
WHERE DATE_SUB(CURDATE(), INTERVAL $rpt_days DAY) <= $wpdb->posts.post_date");
if(count($rpt_term_ids) > 0){
$rpt_tags = get_tags(array(
\'orderby\' => \'count\',
\'order\' => \'DESC\',
\'number\' => $rpt_options[\'number_tags\'],
\'include\' => $rpt_term_ids,
));
foreach ( (array) $rpt_tags as $rpt_tag ) {
echo \'<span class="rpt-link"><a href="\' . get_tag_link ($rpt_tag->term_id) . \'" rel="tag">\' . $rpt_tag->name . \'</a></span>\';
}
}
?>
我一直在将此代码转换为要附加到的变量$content
.
现在,我只有一个非常简单的显示功能,来测试number_days
和number_tags
设置将显示在设置页面中输入的值。是的。
function rpt_display_content ($content){
global $rpt_options;
if(is_single()){
$rpt_display = \'<p>\' . $rpt_options[\'rpt_tags_days\'] . $rpt_options[\'rpt_tags_number\'] . \'</p>\';
$content .= $rpt_display;
}
return $content;
}
add_filter(\'the_content\', \'rpt_display_content\');
SO网友:Aris Blevins
你似乎很快就能把这一切都解决了。如果将查询包装在函数中,并将逻辑和表示分离:
<?php
function wpse_158425_get_terms(){
global $wpdb, $rpt_options;
$wpdb->show_errors();
$rpt_days = $rpt_options[\'number_days\'];
$rpt_term_ids = $wpdb->get_col("
SELECT term_id FROM $wpdb->term_taxonomy
INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
WHERE DATE_SUB(CURDATE(), INTERVAL $rpt_days DAY) <= $wpdb->posts.post_date
");
if(count($rpt_term_ids) > 0){
$rpt_tags = get_tags(array(
\'orderby\' => \'count\',
\'order\' => \'DESC\',
\'number\' => $rpt_options[\'number_tags\'],
\'include\' => $rpt_term_ids,
));
} else {
$rpt_tags = FALSE;
}
return $rpt_tags;
}
function wpse_158425_display_terms(){
global $rpt_options;
$tags = wpse_158425_get_terms();
$recentTags = \'<div class="recent-popular-tags">\';
$recentTags .= \'<h3>\'.$rpt_options[\'heading_tags\'].\'</h3>\';
if($tags != FALSE){
foreach($tags as $tag){
$recentTags .= \'<span class="rpt-link"><a href="\' . get_tag_link ($rpt_tag->term_id) . \'" rel="tag">\' . $rpt_tag->name . \'</a></span>\';
}
} else {
$recentTags .= \'There are no popular tags right now.\';
}
$recentTags .= \'</div><!--.recent-popular-tags-->\';
return $recentTags;
}
function rpt_display_content ($content){
if(is_single()){
$rpt_display = wpse_158425_display_terms();
$content .= $rpt_display;
}
return $content;
}
add_filter(\'the_content\', \'rpt_display_content\');
?>
您可以通过
wpse_158245_get_terms()
接受变量并以这种方式传递选项,并设置默认变量。
我没有机会运行这个,您可能会遇到一些对象/数组错误,但您应该可以继续。
SO网友:Travis Pflanz
这就是我想到的,它完全按照预期工作。作为一个新手,这是一个比solution posted by Aris Blevins? 如果没有,为什么?
我真的不知道。
<?php
/***** Display Functions *******/
function rpt_display_content ($content){
global $rpt_options;
if(is_single()){
ob_start();
?>
<div class="recent-popular-tags">
<h3><?php echo $rpt_options[\'heading_tags\']; ?></h3>
<?php
$rpt_days = $rpt_options[\'number_days\'];
global $wpdb;
$rpt_term_ids = $wpdb->get_col("
SELECT term_id FROM $wpdb->term_taxonomy
INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id=$wpdb->term_relationships.term_taxonomy_id
INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
WHERE DATE_SUB(CURDATE(), INTERVAL $rpt_days DAY) <= $wpdb->posts.post_date");
if(count($rpt_term_ids) > 0){
$rpt_tags = get_tags(array(
\'orderby\' => \'count\',
\'order\' => \'DESC\',
\'number\' => $rpt_options[\'number_tags\'],
\'include\' => $rpt_term_ids,
));
foreach ( (array) $rpt_tags as $rpt_tag ) {
echo \'<span class="rpt-link"><a href="\' . get_tag_link ($rpt_tag->term_id) . \'" rel="tag">\' . $rpt_tag->name . \'</a></span>\';
}
}
?>
</div>
<?php
$content .= ob_get_clean();
}
return $content;
}
add_filter(\'the_content\', \'rpt_display_content\');
?>