背景-我在函数中创建了一个短代码。php,它将带有链接和描述的单个随机标记添加到我的侧边栏中的小部件中。这是我现有的代码。。。
function skips_get_random_tags() {
$args = array(\'exclude\' => \'\');
$alltags = get_tags( $args );
shuffle($alltags);
$count=0;
if ($alltags) {
foreach($alltags as $tag) {
$count++;
return \'<H5>Random App: <a href="\'.get_tag_link($tag->term_id).\'">\'.$tag->name.\'</a></H5><p class="random-tag-description skips-answer">\'.$tag->description.\'</p>\';
if( $count >0 ) break;
}
}
}
add_shortcode( \'random-tag\', \'skips_get_random_tags\' );
它工作得很好,只是每次加载页面时都会运行(受缓存周期的限制),并且因此会在每个页面上生成不同的结果。我想安排它每天运行一次,并且一直在阅读关于Cron的文章,这似乎是解决方案。查看了编解码器后,我认为我需要如下编辑代码(似乎只是添加一些CRON代码来触发代码上方的函数并将其链接到函数)。。。function prefix_activation() {
wp_schedule_event( time(), \'daily\', \'skips_daily_event_hook\' );
}
add_action( \'skips_daily_event_hook\', \'skips_get_random_tags\' );
/**
* On the scheduled action hook, run the function.
*/
function skips_get_random_tags() {
$args = array(\'exclude\' => \'\');
$alltags = get_tags( $args );
shuffle($alltags);
$count=0;
if ($alltags) {
foreach($alltags as $tag) {
$count++;
return \'<H5>Random App: <a href="\'.get_tag_link($tag->term_id).\'">\'.$tag->name.\'</a></H5><p class="random-tag-description skips-answer">\'.$tag->description.\'</p>\';
if( $count >0 ) break;
}
}
}
add_shortcode( \'random-tag\', \'skips_get_random_tags\' );
我怀疑我走了很长一段路,但我不想触发一个我无法阻止的CRON-任何有CRON经验的人都能对我的方法和建议的代码发表评论吗?