HAS_CATEGORY未正常工作

时间:2017-01-06 作者:Alex

在我的website 我想用一个星形图标标记特色文章,它通过检查一篇文章是否有“特色”类别来工作。然而,对于回溯的文章,它似乎总是返回真的,这真的很奇怪。(例如,计算机->编程最佳实践下面的所有内容都被错误地标记为特色!)。更让人尴尬的是,这些过时的文章甚至都不需要使用它,而且对于每一篇特色文章来说,都有足够奇怪的两次。

这是我的函数代码。php:

function showPosts($number, $category="", $description = false, $disambiguation = null, $featured = false, $collapsible = true, $thumbnail = false, $languageFlag = true)
{
    global $post;

            $args = array(
              \'numberposts\'       =>   $number,
              \'category_name\'     =>   $category
            );

    $myposts = get_posts($args); 

    if( $myposts )
    {   
        // hidden posts due to language setting
        $hiddenPosts = 0;

        // get cookie data (prefered language(s))
        if(isset($_COOKIE["post-data"])) :
            $allowedLanguages = get_tangboshi_cookies();
        else:
            $allowedLanguages = array();
        endif;

        // if language setting has not been set, allow all
        // not very elegant to have all the stuff in a single array (post-data), with multiple arrays one could use array.empty();

        if( !(in_array("posts-english", $allowedLanguages) || in_array("posts-german", $allowedLanguages)) ) :
            array_push($allowedLanguages, "posts-english");
            array_push($allowedLanguages, "posts-german");
        endif;

        foreach( $myposts as $post ) 
        {
            setup_postdata($post);

            $id = convertText(get_the_title())."-".$disambiguation;
            $postID = get_the_ID();

            $status = get_post_meta($postID, "status", true);

            switch ($status)
            {
                case "": $statusText = ""; break;
                default: $statusText = \'<span class="caption-draft"> - \'.$status.\'</span>\'; break;
            }

            if ( $featured ) : 
                $id .= "-featured";
            endif;

            // same problem with has_category(\'Featured\')
            if ( in_category(\'Featured\') )
            {
                // every featured article mentioned twice; BUG!!
                // backdated always get start...
                $icon = \'star\';
            }


            // get language of current post
            $language = get_post_meta($postID, "language", true);

            // if language not mentioned, default is English
            if ( !$language )
            {
                $language = "english";
            }

            // filter out posts with wrong language

            if ( in_array("posts-".$language, $allowedLanguages) )
            {
                $isAllowedLanguage = true;
            }
            else
            {
                $isAllowedLanguage = false;
            }

            if($languageFlag):
                switch($language):              
                    case "english": $flagicon = \'<span class="flag-icon flag-icon-squared flag-icon-us"></span> \';break;
                    case "german":  $flagicon = \'<span class="flag-icon flag-icon-squared flag-icon-de"></span> \';break;
                    case "french":  $flagicon = \'<span class="flag-icon flag-icon-squared flag-icon-fr"></span> \';break;
                    case "chinese": $flagicon = \'<span class="flag-icon flag-icon-squared flag-icon-cn"></span> \';break;
                    default:        throw("Uncaught exception: undefined post language.");
                endswitch;

                $languageFlagText = $flagicon;
            endif;

            if( $collapsible && $isAllowedLanguage )
            {
                $postArgs = array(
                    "id"            =>  $id,
                    "caption"       =>  $languageFlagText.get_the_title().$statusText,
                    "icon"          =>  $icon,
                    "initialState"  =>  "out",
                    "function"      =>  "showPostDescription",
                    "args"          =>  $thumbnail,
                    "style"         =>  "modern-post"
                );

                $ret .= call_user_func_array("createPanelWidget", $postArgs);
            }
            elseif ( !$collapsible )
            {
                $ret.= "<p>Non-collapsible detected. Unimplemented. Please set collapsible to true.</p>";
            }
            else
            {
                $hiddenPosts++;
            }


            the_post();
        }

        wp_reset_query();
    }

    if($hiddenPosts > 0) : $ret.= "<p>Hidden posts due to your language setting: ".$hiddenPosts.".</p>"; endif;

    return $ret;
}

function createPanelWidget($id, $caption, $icon, $initialState, $function = "noop", $args = null, $functionHandlesArray = false, $style = \'modern\', $nestedLevel = 1)
{
    if( $function == "createPanelWidget" )
    {
        $newLevel = $nestedLevel + 1;
        $args["nestedLevel"] = $newLevel;
    }

    if ( is_array($args) && !$functionHandlesArray )
    {
        $tmp = call_user_func_array($function, $args); 
    }
    else
    {
        $tmp = call_user_func($function, $args);
    }

    // prevent empty panels from being created
    if(!isset($tmp)) : return; endif;

    $ret .= \'
        <div class="panel-group">
            <div class="panel panel-level-\'.$nestedLevel.\'" id="\'.$id.\'-panel">
                <div class="panel-heading panel-\'.$style.\'">
                    <a data-toggle="collapse" href="#\'.$id.\'">
                        <div class="panel-caption-wrapper">
                            <div class="panel-caption">
                                <p>
                                    \';
                                    if(isset($icon))
                                    {
                                        $ret .= \'<span class="fa fa-\'.$icon.\'"></span>\';
                                    }
    $ret .= \'
                                    \'.$caption.\'
                                </p>
                            </div>
                            <div class="panel-toggle-icon">
                                <span class="fa-stack fa-lg">
                                    <span class="fa fa-square fa-stack-2x"></span>
                                    <span class="toggle-icon"></span>
                                </span>
                            </div>
                        </div>
                    </a>      
                </div>
                <div id="\'.$id.\'" class="panel-collapse collapse \'.$initialState.\'">
                    <div class="panel-body panel-\'.$style.\'">\';
                            $ret .= $tmp;
    $ret .=  \'
                    </div>
                </div>
            </div>
        </div>
    \'; 

    return $ret; 
}

EDIT:

在我的index.php 我打电话"createMainPanel(...)", showPosts(...) 作为call\\u userf\\u func\\u数组的回调调用panelAllPostsParams(...):

function createMainPanel()
{
    $catsArgs = array(
        \'parent\'        =>  0,
        \'orderby\'       =>  \'count\',
        \'order\'         =>  \'DESC\',
        \'hide_empty\'    =>  true,
        \'exclude\'       =>  array(12) // 12 is Featured
    );

    $cats = get_categories($catsArgs);

    //var_dump($cats);

    $ret .= \'<div id="primary" class="content-area">\';
    $ret .= \'<main id="main" class="site-main" role="main">\';

    //$ret .= \'<div class="warning">Since Wed. 28 Dec, 2016 23:30 UTC+1 this page is under maintenance. Expect a lot of stuff to return in the next few days.</div>\';

    $ret .= addLoadingIcon(500, 400);

    $ret .= "<ul class=\'nav nav-tabs\' id=\'main-panel-cats\'>";


    foreach( $cats as $index => $cat )
    {
        $catName    =   $cat->cat_name;
        $catRef     =   convertText($catName);

        if( $index == 0 ){ $active = " active "; }
        else{ $active = ""; }
        $icon = "fa fa-".getIcon($catName);

        $ret .= "<li class=\'".$active."\'><a href=\'#".$catRef."\' data-toggle=\'tab\'><span class=\'".$icon."\'></span> ".$catName."</a></li>";   
    }

    $ret .= "</ul>";

    $ret .= \'<div class="tab-content">\';

    foreach( $cats as $cat )
    {
        static $counter = 0;

        $description    =   category_description($cat);
        $catName        =   $cat->cat_name;

        //set first tab to active tab, if there\'s stuff in localstorage javascript will change this later
        if( !$counter ){ $active = " active "; }
        else{ $active = ""; }

        $icon = "fa fa-".getIcon($catName);

        $catRef = convertText($catName);

        $ret    .= \'<div id="\'.$catRef.\'" class="tab-pane\'.$active.\'">\';
        $ret    .= \'<div class="row">\';
        $ret    .= \'<div class="col-md-12">\';

        $panelHeadlineParams = array(
            \'id\'                =>      \'desc-\'.$catRef, 
            \'caption\'           =>      \'What can I expect from <span class="\'.$icon.\'"></span> \'.$catName.\' articles?\', 
            \'icon\'              =>      \'info-circle\', 
            \'initialState\'      =>      \'out\', 
            \'function\'          =>      \'showString\', 
            \'args\'              =>      $description
        );

        $panelFeaturedParams = array(
            \'id\'                =>      \'featured-\'.$catRef, 
            \'caption\'           =>      \'Featured articles in <span class="\'.$icon.\'"></span> \'.$catName, 
            \'icon\'              =>      \'star\', 
            \'initialState\'      =>      \'out\', 
            \'function\'          =>      \'showPosts\', 
            \'args\'              =>      array(
                \'number\'            =>      null, 
                \'category\'          =>      strtoupper($catRef),
                \'description\'       =>      false,
                \'disambiguation\'    =>      $catRef,
                \'featured\'          =>      true
            )
        );

        $panelAllPostsParams = array(
            \'id\'                =>      \'posts-\'.$catRef, 
            \'caption\'           =>      \'All posts about <span class="\'.$icon.\'"></span> \'.$catName, 
            \'icon\'              =>      \'pencil\', 
            \'initialState\'      =>      \'in\', 
            \'function\'          =>      \'showPosts\', 
            \'args\'              =>      array(
                \'number\'            =>      null, 
                \'category\'          =>      strtoupper($catRef),
                \'description\'       =>      true,
                \'disambiguation\'    =>      $catRef
            )
        );

        $ret .= call_user_func_array("createPanelWidget", $panelHeadlineParams);
        $ret .= call_user_func_array("createPanelWidget", $panelFeaturedParams);
        $ret .= call_user_func_array("createPanelWidget", $panelAllPostsParams);

        $ret    .= \'</div><!-- col -->\';
        $ret    .= \'</div><!-- row -->\';
        $ret    .= \'</div><!-- tab-pane -->\';

        $counter++;
    }


    $ret .= \'</div> <!-- tab-content -->\';
    $ret .= \'</main>\';
    $ret .= \'</div> <!-- #primary -->\';

    echo $ret;
}

2 个回复
最合适的回答,由SO网友:Mark Kaplun 整理而成

循环每次迭代后,您都不会重置图标变量,因此,一旦它成为“星”,它将始终保持不变。

SO网友:montrealist

我怀疑你没有使用get_posts 正确(看起来你在混音WP_Query 方法,例如the_post 在中,结果来自get_posts). 请阅读以下内容的区别WP_Queryget_posts, 例如here.

您是否尝试将当前帖子ID传递到in_category?

if ( in_category(\'Featured\', $post->ID) )