猿问

我可以在 Wordpress 编辑器中将 3 个最近帖子的列表动态输出到默认文本中吗?

我希望有人可以帮助我学习如何实现这一目标。


我的想法是从一个类别(热门帖子)中生成一个包含 3 个最近帖子的列表,作为 wordpress 编辑器中的默认文本。


我查看了几种解决方案来实现类似的东西,并将它们混合到下面的代码中,但它似乎不起作用。


add_filter('default_content', 'tp4567_default_list');

function tp4567_default_list( $content ) {

$content = new WP_Query( 'cat=2&posts_per_page=3' );

return $content;

请问有什么办法可以实现吗?


手掌心
浏览 98回答 1
1回答

开满天机

在functions.php中试试这个代码,它会工作。add_filter( 'default_content', 'wp_my_default_content', 10, 2 );function wp_my_default_content( $content, $post )&nbsp;{// get the posts$posts = get_posts(&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; 'numberposts'&nbsp; &nbsp;=> 3&nbsp; &nbsp; ));// No posts? run away!if( empty( $posts ) ) return '';$content = '<ul>';foreach( $posts as $post ){&nbsp; &nbsp; $content .= sprintf(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; '<li><a href="%s" title="%s">%s</a></li>',&nbsp; &nbsp; &nbsp; &nbsp; get_permalink( $post ),&nbsp; &nbsp; &nbsp; &nbsp; esc_attr( $post->post_title ),&nbsp; &nbsp; &nbsp; &nbsp; esc_html( $post->post_title )&nbsp; &nbsp; );}$content .= '</ul>';return $content;}
随时随地看视频慕课网APP
我要回答