我有一个旧的WordPress安装。不幸的是,公共帖子和草稿都显示在前面。我想这是因为wordpress循环。使用以下代码:
<section class="container container-fluid">
<div id="layout" style="padding: 0 -15px;">
<?php
if (has_tag(\'Karriere\'))
ini_set("display_errors", 1);
$args = array(\'offset\' => 0,
\'numberposts\' => 12,
\'limit\' => 12,
\'post_type\' => "post",
\'orderby\' => \'post_date\',
\'order\' => \'DESC\',
\'category\' => \'26,60\',
\'tag\' => \'Karriere\');
//$args = array(\'numberposts\' => 2);
$recent_posts = wp_get_recent_posts($args, ARRAY_A);
$_SESSION[\'home_offset\'] = count($recent_posts);
foreach( $recent_posts as $recent ){
$post_uri = get_permalink($recent[\'ID\']);
//$post_image = wp_get_attachment_url( get_post_thumbnail_id($recent[\'ID\']) );
$post_image = wp_get_attachment_image_src( get_post_thumbnail_id($recent[\'ID\']), \'vorschaubild\');
$post_image = $post_image[0];
$post_categoryList = array();
$post_categories = get_the_category($recent[\'ID\']);
foreach($post_categories as $post_category){
$post_categoryList[] = $post_category->slug;
}
$content = explode("<!--more-->", $recent[\'post_content\']);
$content = $content[0];
$content = preg_replace(\'/<a href=\\"(.*?)\\">(.*?)<\\/a>/\', "\\\\2", $content);
$content = preg_replace(\'/<div id=\\"(.*?)\\" class=\\"(.*?)\\">>(.*?)<\\/div>/\', "", $content);
?>
<div>
<div class="wrapper-karriere-box" style="padding: 0 5px;">
<? //item echo htmlspecialchars($content); ?>
<a href="<?=$post_uri?>" title="<?=$recent["post_title"]?>">
<div class="newsBox" data-categories="<?=implode(",", $post_categoryList)?>">
<?php
if($post_image !== false && $post_image !== NULL){
?>
<img src="<?=$post_image?>" style="width: 100%;" />
<?php } ?>
<div class="news-box-headline">
<h3><?=$recent["post_title"]?></h3>
<p><?=$content?></p>
</div>
</div>
</a>
</div>
</div>
<?php } ?>
</div>
我必须改变的是,公共帖子和草稿不会显示在前端(针对未注册用户)。
我希望有人能帮忙!非常感谢!
最合适的回答,由SO网友:user141080 整理而成
要仅查看“发布”帖子,必须指定“post\\u状态”。
文件wp_get_recent_posts 函数表示,如果未指定“post\\u status”,函数将使用默认值“draft、publish、future、pending、private”。
$defaults = array(
\'numberposts\' => 10,
\'offset\' => 0,
\'category\' => 0,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\',
\'include\' => \'\',
\'exclude\' => \'\',
\'meta_key\' => \'\',
\'meta_value\' => \'\',
\'post_type\' => \'post\',
\'post_status\' => \'draft, publish, future, pending, private\',
\'suppress_filters\' => true,
);
要区分登录用户和未登录用户,可以使用函数
is_user_logged_in