最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

templates - Is there a more efficient way to loop through pages?

programmeradmin1浏览0评论

I have a site that uses a set of 3 images for the header of its subpages. Each page has a different set of images. From what I could find, my best way to accomplish this would be to place an if statement within my header-subpage.php. The following is a snippet of my code.

<?php
if ( is_page( 'available-homes' ) ) {
    get_template_part( 'sections/banner-images', 'available-homes' );
} elseif ( is_page( 'design-services' ) ) {
    get_template_part( 'sections/banner-images', 'design-services' );
} elseif ( is_page( 'about-us' ) ) {
    get_template_part( 'sections/banner-images', 'about-us' );
} ........

} elseif ( is_page( 'portfolio' ) ) {                          
    get_template_part( 'sections/banner-images', 'portfolio' );
} elseif ( is_page( 'galleries' ) ) {
    get_template_part( 'sections/banner-images', 'galleries' );
} else {
    echo "page images undefined...";
}

Is there a more efficient way for me to do this? I am still learning PHP, but it seems like there might be a better more efficient way to loop through the pages than what I have done.

Thank you for any suggestions.

I have a site that uses a set of 3 images for the header of its subpages. Each page has a different set of images. From what I could find, my best way to accomplish this would be to place an if statement within my header-subpage.php. The following is a snippet of my code.

<?php
if ( is_page( 'available-homes' ) ) {
    get_template_part( 'sections/banner-images', 'available-homes' );
} elseif ( is_page( 'design-services' ) ) {
    get_template_part( 'sections/banner-images', 'design-services' );
} elseif ( is_page( 'about-us' ) ) {
    get_template_part( 'sections/banner-images', 'about-us' );
} ........

} elseif ( is_page( 'portfolio' ) ) {                          
    get_template_part( 'sections/banner-images', 'portfolio' );
} elseif ( is_page( 'galleries' ) ) {
    get_template_part( 'sections/banner-images', 'galleries' );
} else {
    echo "page images undefined...";
}

Is there a more efficient way for me to do this? I am still learning PHP, but it seems like there might be a better more efficient way to loop through the pages than what I have done.

Thank you for any suggestions.

Share Improve this question edited Feb 16, 2021 at 22:52 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Feb 16, 2021 at 21:40 coeyflyercoeyflyer 1
Add a comment  | 

2 Answers 2

Reset to default 1

Alternative to if... else... you can use the control structure switch functionality. But if you use if... elseif... or switch is mainly a matter of preference. The performance is the same.

if else

if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}

switch

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}

Switch Case acts similar to Elseif control structure. The main difference is that Switch Case can be written in a way that it compares a given value with a set of predefined values without evaluating a set of conditions.

Opinion

In my opinion, `switch is more readable and this is also important for maintenance. If you need this check for a template more often you should think about a function, so that you don't write recurring code.

I've noticed that the second part of your template names are the same as the page slugs. If that's the case then just use the page slug as the second argument of get_template_part() and you won't need any loops, conditions or switches at all:

<?php
$slug = get_queried_object()->post_name;

get_template_part( 'sections/banner-images', $slug );

And if the template for that slug doesn't exist it will just fall back to sections/banner-images.php.

We also note that is_page() can check for array of slugs.

发布评论

评论列表(0)

  1. 暂无评论