WordPress的内容可见性不起作用(草稿、公共等)

时间:2020-05-15 作者:tom84

我有一个旧的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>

我必须改变的是,公共帖子和草稿不会显示在前端(针对未注册用户)。

我希望有人能帮忙!非常感谢!

1 个回复
最合适的回答,由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

相关推荐

Get Current User ID Inside a Loop返回0表示短码

我有一个函数,它对给定作者的所有帖子都有一个循环。我使用get_current_user_id() 但这在循环中似乎不起作用,或者可能是它的短代码问题。我的函数在shortcode的帮助下运行。当前用户总是返回0,因此它会显示我网站上所有帖子的元数据。function get_meta_value_by_meta_key(){ $author_id = \'get_current_user_id()\'; // do stuff to get user I $author