WordPress.org

Ready to get started?Download WordPress

Forums

conditional functions? (4 posts)

  1. charismaarts
    Member
    Posted 2 years ago #

    Hello: I am using twenty eleven child theme on latest version of wordpress. I am pretty experienced in doing custom coding but all of my experience comes from using the "thesis" theme which works differently from most other themes. Anyway; I am trying to have a function only run in certain pages. I wrote the function and put it in my "functions.php" file and it worked but made change to the whole site. But when I added a "is_page" statement to try and restrict to one page only the code stopped working. Is this not doable in twentyeleven? Or is there some other way to conditionally run functions on some pages and not others?

    Please let me know,

    Gerard

  2. shirazdrum
    Member
    Posted 2 years ago #

    Themes don't make any difference in the way the php is executed. People need to understand that the template is only a visual middle-man for your browser. Thesis and twenty Eleven in this regard are exactly the same.

    <?php if (is_page('your_page') || is_page('your_page')) { ?>
    // do something
    <?php } ?>
  3. charismaarts
    Member
    Posted 2 years ago #

    Thanks for answering. Here is my code:

    <?php
    
    function test_code() {
    
    if (is_page(10428)) {
    
    echo "***TESTING A FUNCTION****";
    }
    }
    
    add_action('after_setup_theme','test_code');
    
    ?>

    when I remove the "is_page" statement and also its closing "}" this function works fine; but putting this condition on it; it doesnt work at all; do you see anything wrong with my code?

    Thanks,

    Gerard

  4. shirazdrum
    Member
    Posted 2 years ago #

    <?php
    function test_code()
    {
       //whatever you want it to do
    }
    ?>
    <?php if (is_page('your_page') || is_page('your_page')) {
    // output function
    echo test_code();
    }else{
    //do this
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.