WordPress.org

Ready to get started?Download WordPress

Forums

PHP include for certain URLs via IF statement (2 posts)

  1. fitzpatrick512
    Member
    Posted 5 years ago #

    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

  2. sparro
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.