Title: conditional code in functions.php
Last modified: August 30, 2016

---

# conditional code in functions.php

 *  Resolved [ddlange](https://wordpress.org/support/users/ddlange/)
 * (@ddlange)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/conditional-code-in-functionsphp/)
 * I modified the functions.php file to change the comment title from “Leave a Reply”
   to “Submit your contribution.” Is it possible to add conditional code so that
   different pages can have different comment titles? The code I added is:
 *     ```
       function comment_reform ($arg) {
       $arg['title_reply'] = __('Please submit your contribution:');
       return $arg;
       }
       add_filter('comment_form_defaults','comment_reform');
       ```
   

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [wpfan1000](https://wordpress.org/support/users/wpfan1000/)
 * (@wpfan1000)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/conditional-code-in-functionsphp/#post-6452471)
 * Hi,
 * Sure you can do that.
 * For example, you can get the id of a post or page and make your function conditional
   on that.
 * [https://codex.wordpress.org/Function_Reference/get_the_ID](https://codex.wordpress.org/Function_Reference/get_the_ID)
 * $postid = get_the_ID();
    if ($postid == 6)……..
 * In this approach you have to select which posts/page with which id’s should have
   a particular comment title.
 * This works within the loop only.
 * Not knowing how you want to identify which posts or pages I cannot provide more
   specifics……
 * Another way would be to associate certain categories to posts and have a different
   comment title per category.
 * [https://startpage.com/do/search?cmd=process_search&cat=web&query=wordpress+get+category&language=english&no_sugg=1&ff=&abp=-1&nj=1](https://startpage.com/do/search?cmd=process_search&cat=web&query=wordpress+get+category&language=english&no_sugg=1&ff=&abp=-1&nj=1)
 * Hope this helps
 *  Thread Starter [ddlange](https://wordpress.org/support/users/ddlange/)
 * (@ddlange)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/conditional-code-in-functionsphp/#post-6452514)
 * Thanks! It’s pages only, 3 of them, and they have the IDs
    sitename/?p=255, sitename/?
   p=8, sitename/?p=9.
 * So would the code be something like:
 * function comment_reform ($arg) {
    $pageid = get_the_ID(); if ($pageid == 255)
   $arg[‘title_reply’] = __(‘Please submit your question:’); return $arg; if ($pageid
   == 9) $arg[‘title_reply’] = __(‘Please submit your comment:’); return $arg; }
 *  [wpfan1000](https://wordpress.org/support/users/wpfan1000/)
 * (@wpfan1000)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/conditional-code-in-functionsphp/#post-6452520)
 * You’re welcome – glad I could help.
 * Glad also you know how to find out page ids already 🙂
 * yep, you can do it that way or
 *     ```
       if ( ($pageid == 8) OR ($pageid == 9) ) {
       $arg['title_reply'] = __('Please submit your contribution:');
       return $arg;
       }
       ```
   
 * In case more than one page needs the same comment modification.
 * Note the outside brackets that enclose all three comparisons – they gotta be 
   there…..
 * Also each if statement needs culry brackets to contain what it controls….
 * One other thing is that getting page id does not work outside the loop, here’s
   how to if you need to:
 * [https://startpage.com/do/search?cmd=process_search&cat=web&query=wordpress+get+id+outside+loop&language=english&no_sugg=1&ff=&abp=-1&nj=1](https://startpage.com/do/search?cmd=process_search&cat=web&query=wordpress+get+id+outside+loop&language=english&no_sugg=1&ff=&abp=-1&nj=1)
 *  Thread Starter [ddlange](https://wordpress.org/support/users/ddlange/)
 * (@ddlange)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/conditional-code-in-functionsphp/#post-6452554)
 * Something is not working right! Here’s the code I entered:
 *     ```
       function comment_reform ($arg) {
       $pageid = get_the_ID();
       if ($pageid == 8)
       $arg['title_reply'] = __('Please submit your comment:');
       return $arg;
       if ($pageid == 9)
       $arg['title_reply'] = __('Please submit your question:');
       return $arg;
       if ($pageid == 255)
       $arg['title_reply'] = __('Please submit your contribution:');
       return $arg;
       }
       add_filter('comment_form_defaults','comment_reform');
       ```
   
 * I see the correct text for page 8 but “Leave a reply” for 9 and 255. I got the
   page IDs from the shortlinks. Did I leave out a comma? a curly bracket? Something
   else?
 * Thanks!
 *  [wpfan1000](https://wordpress.org/support/users/wpfan1000/)
 * (@wpfan1000)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/conditional-code-in-functionsphp/#post-6452555)
 * Also each if statement needs culry brackets to contain what it controls….
 * And the function needs to return the $arg; so leave it outside the if statements
 * See if you can see the page id being printed somewhere in the page to see if 
   you are actually getting the right page id
    then comment the echo line out….
 *     ```
       function comment_reform ($arg) {
   
       $pageid = get_the_ID();
   
       echo $pageid;
   
       if ($pageid == 8) {
       $arg['title_reply'] = __('Please submit your comment:');
       }
   
       if ($pageid == 9) {
       $arg['title_reply'] = __('Please submit your question:');
       }
   
       if ($pageid == 255) {
       $arg['title_reply'] = __('Please submit your contribution:');
       }
   
       return $arg;
       }
       add_filter('comment_form_defaults','comment_reform');
       ```
   
 *  Thread Starter [ddlange](https://wordpress.org/support/users/ddlange/)
 * (@ddlange)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/conditional-code-in-functionsphp/#post-6452556)
 * Thanks, I will as soon as WP comes back with my site. All I did was click Update
   after I made editing changes and WP vanished!
 *  Thread Starter [ddlange](https://wordpress.org/support/users/ddlange/)
 * (@ddlange)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/conditional-code-in-functionsphp/#post-6452559)
 * Brilliant! Thank you ever so much. And I’ve learned a bit about conditional statements
   in PHP.
 * I had to go onto the hosting site to edit out my error there. Whew!
 * Thanks again. It looks great!
 *  [wpfan1000](https://wordpress.org/support/users/wpfan1000/)
 * (@wpfan1000)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/conditional-code-in-functionsphp/#post-6452609)
 * You are welcome 🙂
    Glad I could help and they you got it working…

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘conditional code in functions.php’ is closed to new replies.

## Tags

 * [comment title](https://wordpress.org/support/topic-tag/comment-title/)
 * [conditional code](https://wordpress.org/support/topic-tag/conditional-code/)
 * [leave a reply](https://wordpress.org/support/topic-tag/leave-a-reply/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 2 participants
 * Last reply from: [wpfan1000](https://wordpress.org/support/users/wpfan1000/)
 * Last activity: [10 years, 9 months ago](https://wordpress.org/support/topic/conditional-code-in-functionsphp/#post-6452609)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
