Php 类别中最新帖子的Wordpress短码

Php 类别中最新帖子的Wordpress短码,php,wordpress,Php,Wordpress,我需要创建一个自定义的短代码,以允许我动态地为给定类别中的顶级帖子填写3个值(帖子标题、帖子日期、帖子链接) 我想的是这样的:[top_post cat=“5”] 它将输出如下内容: <div class="three_fourth "> <h2>[POST_TITLE] - [POST_DATE]</h2> </div> <div class="one_fourth last"> <a class="button" href="

我需要创建一个自定义的短代码,以允许我动态地为给定类别中的顶级帖子填写3个值(帖子标题、帖子日期、帖子链接)

我想的是这样的:[top_post cat=“5”]

它将输出如下内容:

<div class="three_fourth ">
<h2>[POST_TITLE] - [POST_DATE]</h2>
</div>
<div class="one_fourth last">
<a class="button" href="[LINK_TO_POST]" style="background-color: #c62b02">
</div>

[发布标题]-[发布日期]
这可行吗


谢谢您的帮助。

在functions.php中,类似这样的内容

function shortcodeFunction( $atts ) {
  extract( shortcode_atts( array(
    'cat' => '' //the attr in the shortcode
  ), $atts ) );
  if($cat){ //$cat is extracted from $atts
      $args = array(
      'category' => $cat,
      'orderby' => 'post_date',
      'order' => 'DESC', // Show only latest, rejig as needed
      'posts_per_page' => 1 // Only show 1
    )
    $allPosts = get_posts($args);
    $return = '';
    foreach($allPosts as $p){ // Bog standard get post foreach
      $thePost = get_post($p);
      $date = $thePost->post_date;
      $date = strtotime($date);
      $date = gmdate('jS F Y',$date);
      $link = get_permalink($thePost->ID);
      $return .= '<div class="three_fourth "><h2>'.$thePost->post_title.' - '.$date.'</h2></div><div class="one_fourth last"><a class="button" href="'.$link.'" style="background-color: #c62b02"></div>';
    }
} return $return;
} else {
    return false;   
}
add_shortcode( 'top_post', 'shortcodeFunction' );
function shortcodeFunction($atts){
提取(短码)附件(数组)(
'cat'=>''//短代码中的属性
)美元(附件);;
如果($cat){/$cat是从$atts中提取的
$args=数组(
“类别”=>$cat,
'orderby'=>'post_date',
'order'=>'DESC',//仅显示最新版本,根据需要重新调整
“每页帖子”=>1//仅显示1
)
$allPosts=get_posts($args);
$return='';
foreach($allPosts作为$p){//Bog标准get post foreach
$thePost=get_post($p);
$date=$thePost->post\u日期;
$date=strottime($date);
$date=gmdate('jsf Y',$date);
$link=get\u permalink($thePost->ID);
$return.='.$thePost->post_title.-'.$date.';
}
}return$return;
}否则{
返回false;
}
添加快捷代码('top_post','shortcodeFunction');

您可以使用插件来完成此操作


我不得不做一些调整,因为它无法按原样加载,但这很有效。非常感谢。好极了很高兴听到这个消息!