Support » Plugins and Hacks » PHP include for certain URLs via IF statement

  • I want to show certain content in my sidebar for certain URLs.

    The following example is for using an include for the homepage, but I can’t get it to work in WordPress:

    <!-- begin homepage ads -->
        <?php
            $homepage = "/";
            $currentpage = $_SERVER['REQUEST_URI'];
            if($homepage==$currentpage) {
            include('homepage-ads.php');
            }
        ?>
        <!-- end homepage ads -->

    How would I get it to include a .php file if it’s NOT the homepage?

    Can I set a string of commands, in layman’s terms:

    if is: /page1 , include page1_ads.php
    if is: /green-page , include green-page_ads.php
    if is: /another-page , include another-page_ads.php

Viewing 1 replies (of 1 total)
  • sparro

    (@jeanfayette)

    Here you are:

    <?php if (is_page('1')) { include("page1_ads.php"); }
    
    elseif (is_page('green-page')) { include("green-page-ads.php"); }
    
    elseif (is_page('another-page')) { include("another-page-ads.php"); }
    
    else { include("everything-else.php"); 
    
    } ?>

    The final else statement isn’t necessary, though can be handy as a fallback.

    The code above assumes that the ad pages are placed in your theme folder, or wherever the sidebar file is.

Viewing 1 replies (of 1 total)
  • The topic ‘PHP include for certain URLs via IF statement’ is closed to new replies.