我正试图为我的博客(WordPress)制作一个小插件,但我遇到了以下两个问题。
-
我想从插件中获取自定义类别中的最新三篇帖子,但现在它只获取最后一篇,并将其复制三次。我该怎么解决?
-
我想制作一个动态标题。这意味着我希望能够从管理控制面板更改插件的标题。我怎样才能做到这一点?
更新:
多亏了你们,我成功地展示了帖子的图片,但它没有显示在正确的位置。
这是正确的HTML
<li>
<div class="projects">
<ul class="projects sticker">
<li><h2><?php the_title(); ?></h2></li>
<li><p><a href="">details</a></p></li>
</ul>
<img src="" />
</div>
</li>
现在就是这样显示的
<li>
<div class="projects">
<ul class="projects sticker">
<li><h2><?php the_title(); ?></h2></li>
<li><p><a href="">details</a></p></li>
</ul>
</div>
基本上,我必须把img标签放在列表中,并分配
这是我到目前为止的代码
$args = array('numberposts' => '3', 'category' => $cat_id);
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent ) {
echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >'
.'<li>' .'<div class="projects-wrapper">' .'<ul class="projects-sticker">' .'<li>' .'<h2>' . $recent["post_title"] .'</li>' .'</h2>' .'<li><p><a href="">details</a></p></li></ul>' .'<img src="'.the_post_thumbnail('thumbnail').'" />' .'</div>' .'</li>'.'</a>';
要获得最近的帖子,请更好地使用wp_get_recent_posts。这是它的片段。
$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a>';
echo get_the_post_thumbnail($recent["ID"], 'thumbnail');
}
wp_reset_query();
订购职位
您使用的showposts => 3
不是有效的参数。若要使用posts_per_page => 3
,请删除numberposts
。您还可以随机订购:必须为'orderby' => 'date'
的'orderby' => 'rand'
在WP_Query页面上可以找到更多参数。
所以使用:
$args = array('posts_per_page' => 3, 'orderby' => 'date', 'category' => $cat_id);
小工具标题
查看Widget API,扩展Widget类,并在form
函数中添加字段。例如,通过这种方式,您还可以使帖子的数量可变。