这个PHP代码是否有效?

时间:2014-03-09 22:16:45

标签: php wordpress performance switch-statement

这似乎是一个常见的问题,但有各种不同的答案,所以这里就是这样。我正在帮助某人构建一个wordpress网站,我做了一个switch语句来检查用户所在的类别页面以显示图片和描述。我还写了一个非常简单的函数来查看该类别是否有任何帖子以显示消息。

为了更好地解释代码: 顶部的这段代码使用了一些Wordpress函数和对象(根据我的理解)来获取当前页面的Category Slug:

$cat = get_category( get_query_var( 'cat' ) );
    $cat_id = $cat->cat_ID;
    $cat_name = $cat->name;
    $cat_slug = $cat->slug;

我将所有图片重命名为与类别slug相同的名称,以便为所有语句重用相同的变量。

下一段代码会看到该页面是否有任何帖子:

function add_page_title_header( ){
        function check_if_posts(){
            if(!have_posts()){
                echo '<span id="stillworking">There currently isn\'t any content for this category, but there will be soon. For now try another!</span>';
            }
        }

其余代码(嵌入在下面,switch语句)使用从页面获取的Category slug来输入图片,然后使用硬编码描述。 (在用这种方式解释之后,我意识到通过再次使用相同的变量,我不需要为每个变量都使用switch语句,我将在下面的答案中解决这个问题)

$cat = get_category( get_query_var( 'cat' ) );
$cat_id = $cat->cat_ID;
$cat_name = $cat->name;
$cat_slug = $cat->slug;
switch ($cat_slug) {
    case 'ultracarnivorous':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">A puppet show starring cryptids.</span>';
        check_if_posts();
        break;
    case 'things-of-interest':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">Interesting things of all kinds.</span>';
        check_if_posts();
        break;
    case 'tim-and-james':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">Editorial posts by Tim and James, the website\'s creators.</span>';
        check_if_posts();
        break;
    case 'the-tin-age-of-comics':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">A web comic using royalty free comic book art.</span>';
        check_if_posts();
        break;
    case 'chainsaw-podcast':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">A podcast by Tim and James, the website\'s creators.</span>';
        check_if_posts();
        break;
    case 'songink':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">Short stories inspired by songs.</span>';
        check_if_posts();
        break;
    case 'takeaways':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">A short, accessible thing to do this week.</span>';
        check_if_posts();
        break;
    case 'chainsaw-gaming':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">Playing, reviewing and inventing games of all kinds.</span>';
        check_if_posts();
        break;
    case 'chainsaw-radio-theatre':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">A fake, old-timey radio show full of factoids.</span>';
        check_if_posts();
        break;
    case 'canonically-incorrect':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">Posts by our lovable friend, Jason.</span>';
        check_if_posts();
        break;
    case 'chainsaw-phraseology':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">Fun with words and language.</span>';
        check_if_posts();
        break;
    case 'chicken-fried-awesome':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">Food reviews with a twist.</span>';
        check_if_posts();
        break;
    case 'transmission-received':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">A daily, back and forth video conversation.</span>';
        check_if_posts();
    break;
    case 'link-burger':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">A group of links to enjoy in one sitting.</span>';
        check_if_posts();
        break;
    case 'chainsaw-reviews':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">Reviews of all kinds by the website\'s creators.</span>';
        check_if_posts();
        break;
    case 'multifarious-machinations':
        echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
        echo '<span id="catdescription">A catch-all category for posts that don\'t seem to fit anywhere else.</span>';
        check_if_posts();
        break;



    default:
    echo "This is awkward, you shouldn't be seeing this page!";
}
    }

    add_filter( 'thematic_page_title', 'add_page_title_header' );

如果你看到任何明显的错误或记忆 - 猪类型的东西,请告诉我。我还计划在网站全部完成时利用浏览器缓存,我写的功能是否可能会产生问题呢?通常在发布帖子时,它会重置某些缓存元素,但只有在缓存清除后,此功能才会再次运行吗?有没有办法避免这种情况?

我过去曾经无数次使用过这个网站,但这标志着我的第一个问题,对此非常苛刻。

您可以通过访问http://chainsawcreative.com并点击左侧的任何类别来查看(尽管是测试版)操作。一旦设计完成,其他优化就会发挥作用,但我现在只关注PHP的这一部分。

1 个答案:

答案 0 :(得分:3)

如果您可以像加入其他字段一样从数据库加载描述,那么这是最简单的。否则,如果您需要对描述进行硬编码,并且输出将始终相同,除了描述文本之外,使用存储描述文本的数组更简单,然后不复制相同的输出代码。

E.g。

$descriptions = array(
   'ultracarnivorous'   =>  "A puppet show starring cryptids.",
   'things-of-interest' =>  "Interesting things of all kinds.",
   'tim-and-james'      =>  "Editorial posts by Tim and James, the website's creators."
);
echo '<span id="titlecenter"><img src="'. home_url() . '/images/big/' . $cat_slug . '.png"></span>';
echo '<span id="catdescription">' . htmlspecialchars($descriptions[$cat_slug]) . '</span>';
check_if_posts();