• Resolved ddlange

    (@ddlange)


    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)
  • 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.www.ads-software.com/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

    Hope this helps

    Thread Starter ddlange

    (@ddlange)

    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;
    }

    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

    Thread Starter ddlange

    (@ddlange)

    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!

    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

    (@ddlange)

    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

    (@ddlange)

    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!

    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.