当前位置 : 主页 > 网络编程 > PHP >

WordPress前台随机显示一句话

来源:互联网 收集:自由互联 发布时间:2021-06-28
将代码另存为一个php文件,后台开启插件 post_content; // Here we split it into lines $lyrics = explode( "\n", $lyrics ); // And then randomly choose a line return wptexturize( $lyrics[ mt_rand( 0, count( $lyrics ) - 1 ) ] );}
将代码另存为一个php文件,后台开启插件
 post_content;
 
    // Here we split it into lines
    $lyrics = explode( "\n", $lyrics );
 
    // And then randomly choose a line
    return wptexturize( $lyrics[ mt_rand( 0, count( $lyrics ) - 1 ) ] );
}
 
// This just echoes the chosen line, we'll position it later
function nihao() {
    $chosen = nihao_get_lyric();
    echo "

$chosen [MORE]

"; } // Now we set that function up to execute when the admin_notices action is called add_action( 'head_notices', 'nihao' ); // We need some CSS to position the paragraph function nihao_css() { echo " "; } add_action( 'head_notices', 'nihao_css' ); ?>
在header.php或index.php或footer.php等任何你想要的位置放入以下代码
 
插件说明
原插件是直接将要显示的内容放在插件文件内,不方便编辑,
所以采用直接调用一个文章或者页面的内容的方式显示,插件代码17行显示的就是ID为2的发布内容。

$lyrics = get_post( 2 )->post_content;

演示地址:http://gaobukai.com/nihao
网友评论