我正在使用SitePoint post创建一个带有自定义帖子类型的帖子系列,并为帖子序列号设置了自定义文件夹。
下面的功能是提供特定分类中发布的所有帖子的列表。问题是它还列出了删除的&隐藏的帖子也是:
function sitepoint_post_series_content_filter($content) {
$slug = "sitepoint-postseries";
if ($slug != get_post_type()) {
return $content;
}
$post_series_list = get_option("post_series_" . get_the_ID() . "_ids", "");
$post_series_list_array = explode(',', $post_series_list);
$post_series_serial_number = array();
foreach ($post_series_list_array as $key => $value) {
$serial_number = get_post_meta($value, "sitepoint-postseries-serial-number", true);
$post_series_serial_number[$value] = $serial_number;
}
asort($post_series_serial_number);
$html = "<ul class='sitepoint-post-series'>";
foreach ($post_series_serial_number as $key => $value) {
$post = get_post($key);
$title = $post->post_title;
$html = $html . "<li><h3><a href='" . get_permalink($key) . "'>" . $title . "</a></h3></li>";
}
$html = $html . "</ul>";
return $content . $html;
}
add_filter("the_content", "sitepoint_post_series_content_filter");
如何排除已删除的&amp;此列表中的隐藏帖子。
答案 0 :(得分:0)
用以下代码替换您的代码。
function sitepoint_post_series_content_filter($content) {
$slug = "sitepoint-postseries";
if ($slug != get_post_type()) {
return $content;
}
$post_series_list = get_option("post_series_" . get_the_ID() . "_ids", "");
$post_series_list_array = explode(',', $post_series_list);
$post_series_serial_number = array();
foreach ($post_series_list_array as $key => $value) {
$serial_number = get_post_meta($value, "sitepoint-postseries-serial-number", true);
$post_series_serial_number[$value] = $serial_number;
}
asort($post_series_serial_number);
$finalArray = array();
foreach ($post_series_serial_number as $keyPostID => $valueID) {
if('publish' == get_post_status($keyPostID)){
$finalArray[$keyPostID] = $keyPostID;
}
}
$html = "<ul class='sitepoint-post-series'>";
foreach ($finalArray as $key => $value) {
$post = get_post($key);
$title = $post->post_title;
$html = $html . "<li><h3><a href='" . get_permalink($key) . "'>" . $title . "</a></h3></li>";
}
$html = $html . "</ul>";
return $content . $html;
}
add_filter("the_content", "sitepoint_post_series_content_filter");