Skip to:
Content
Pages
Categories
Search
Top
Bottom

Help with an if/then statement

  • how would i write out this if statement? i’m really bad with this:

    if (this is the last page of posts)

    then (print <h2 class=’post-form’>POST A REPLY</h2>)

    basically, i want “post a reply” only to appear on the last page of posts but i can’t seem to get it.

Viewing 17 replies - 1 through 17 (of 17 total)
  • <?php if( #conditional# ) {
    //some code
    } ?>

    Unfortunately determining if this is the last page is a little bit difficult. You need code like this (taken from template-functions.php):

    global $page, $topic;
    $add = topic_pages_add();
    $last_page = get_page_number( $topic->topic_posts + $add );
    if( $page == $last_page ) {
    //some code
    }

    which is way more code than it should be. You should be able to use (but can’t yet)

    if( bb_last_page() ) {
    // some code
    }

    so I’m submitting a trac ticket so that hopefully in the next version you can do that.

    In your case, the // some code should be:

    print( '<h2 class="post-form">POST A REPLY</h2>' );

    Worth noting, in PHP strings (ie. text in code) are marked by opening and closing with ' or ". If you open a string with ' then " will be ignored and the other way round, too. So you could have used

    print( "<h2 class='post-form'>POST A REPLY</h2>" );

    Also in PHP functions have to be like this: function_name( #arguments );

    where #arguments can be things like a string, or a variable you set earlier. Some functions do without the brackets but all of them work fine with brackets, so it’s best to always use them. :)

    thanks. after much editing of template-functions.php, i got it to work. this is what did it:

    function post_form( $h2 = ” ) {

    global $bb, $page, $topic, $forum;

    $add = topic_pages_add();

    if ( empty($h2) && false !== $h2 ) {

    if ( is_topic() )

    $h2 = __(”);

    elseif ( is_forum() )

    $h2 = __(‘New Topic in this Forum’);

    elseif ( is_tag() || is_front() )

    $h2 = __(‘CREATE A THREAD’);

    }

    $last_page = get_page_number( $topic->topic_posts + $add );

    if ( $page != $last_page ) {

    echo “<h2 class=’post-form’>$h2</h2>n”;

    }

    else

    echo “<h2 class=’post-form’>POST A REPLY</h2>n”;

    It’s a very bad idea to edit template-functions.php or any other core files. What will you do when the next version comes? You should always put custom code in your template or plugin. Did my code not work in your template?

    i didn’t know that :/

    i tried your code but i couldn’t get it to work because i didn’t fully understand it. i think i pieced it together wrong or left out something.

    do i just paste the whole thing into topic.php? or some other file?

    Don’t worry about it :)

    I’m sorry I wasn’t clearer, that was raw PHP code. So that PHP code runs, you have to put <?php and ?> tags around it.

    <?php
    global $page, $topic;
    $add = topic_pages_add();
    $last_page = get_page_number( $topic->topic_posts + $add );
    if( $page == $last_page ) {
    print( '<h2 class="post-form">POST A REPLY</h2>' );
    }
    ?>

    This just worked for me in my template’s topic.php.

    i will try that right now, thank you.

    can i still download 0.8.1? that’s the version i have and i want to do a diff on templates-functions.php

    The newest version is ALWAYS an improvement. A BIG improvement. There is NEVER a situation where you shouldn’t upgrade.

    You have a problem and you’re not running the latest version. What do you do? Go upgrade.

    You don’t have a problem. You’re not running the latest version. What do you do? GO UPGRADE.

    Upgrading might not even fix this problem, it’s not sure. But it does fix several security exploits and adds a bunch of featurs. Go upgrade!

    What’s worst is that you’re on a support forum asking for help and you’re not even on the latest version.

    If I wasn’t clear … GO UPGRADE

    i tried upgrading before and as you can imagine, it broke a lot my stuff so i had to go back down because i couldn’t figure out how to fix it all. and just so you know, i always upgrade my wordpress the day it comes out but this time i was a little out of my league with bbpress. i don’t waste people’s time, i’ve tried everything i know how.

    i got the upgrade to work, thank you for your advice. and your php code worked great. now the only thing i need to know is, how do i change the word “reply” to “post a reply” without altering the template-functions.php? or any other predetermined text for that matter?

    Cool. You’ll find it is an improvement. :)

    That’s really simple. Just change post_form() to post_form('Take some Pizza') or whatever else you want in there.

    (The only reason I know this is I looked into template-functions.php and searched for post_form, then saw this:

    function post_form( $h2 = '' ) {

    which tells me it wants $h2 as an argument. $h2 is the <h2>text here</h2> as you can imagine.)

    excellent! thank you very much.

    one more question in regards to the same thing:

    function post_form( $h2 = ” ) {

    global $bb, $page, $topic, $forum;

    $add = topic_pages_add();

    if ( empty($h2) && false !== $h2 ) {

    if ( is_topic() )

    $h2 = __(‘Reply’);

    elseif ( is_forum() )

    $h2 = __(‘New Topic in this Forum’);

    elseif ( is_bb_tag() || is_front() )

    $h2 = __(‘Add New Topic’);

    }

    how do i change the text that says ‘add new topic’ like i did with your “post_form(‘Take some Pizza’)”. i can’t find where to change that.

    also, if i wanted to add two line breaks above the <h2>, how would i do that other than in template-functions.php?

    thanks so much for your help.

    You do the same thing, but in different templates. If you look at the code, if $h2 is empty (ie. you didn’t tell it what to set) then it checks where it is – in a topic, in a forum, on a tag page or on the front page. But you can always tell it what to put instead. So if you want something else on a tag page, open the tag pages (tags.php, tag-single.php I think) in your template, look for <?php post_form(); ?> and change it to <?php post_form('No pizza for you!'); ?>. That’s a change that will happen only on the pages whose template you modified.

    To add line breaks in HTML you use <br />. But I’m not sure you can have that inside an <h2> element. You can probably just put that right above the <?php post_form(); ?>, like so:

    <br />
    <br />
    <?php post_form('Macaroni Cheese'); ?>

    sweet, thanks.

    now the only one i’m getting hung up on is the “add new topic” page because the page url is http://www.example.com/bbpress/?new=1 and i can’t see which page that actually is referring to (i can usually see something like “/topic.php?id=35&page&replies=2” in the url to know what page to edit. i know the actual form itself resides in post-form.php but the <h2> i’m trying to alter isn’t actually in that file. same goes with the line breaks before it, they would go in the same place.

    i can see where it is in the template-functions.php:

    elseif ( is_bb_tag() || is_front() )

    $h2 = __(‘Add New Topic’);

    i simply can’t find it elsewhere in the code and i’m going mad and blind!

    all your help is much appreciated.

    ps: one more thing! do you know where would i change the “you must log in to post” text?

    It’s in front-page.php at the very bottom. You can see at the start it checks if $forums is set, and if it isn’t (line 75, where it says else : $forums) it delivers the page to make a new topic (and in the background $forums isn’t set if the url has ?new=1). That’s where the post_form() is too. Finding that one out took a while :P

    I don’t know. Where is the you must log in to post text?

    YES!!! oh, you’ve made my week, thank you so much :)

    the log in text is here in template-functions.php:

    if ( ( is_topic() && bb_current_user_can( ‘write_post’, $topic->topic_id ) && $page == $last_page ) || ( !is_topic() && bb_current_user_can( ‘write_topic’, $forum->forum_id ) ) ) {

    echo “<form class=’postform’ name=’postform’ id=’postform’ method=’post’ action='” . bb_get_option(‘uri’) . “bb-post.php’>n”;

    bb_load_template( ‘post-form.php’, array(‘h2’ => $h2) );

    bb_nonce_field( is_topic() ? ‘create-post_’ . $topic->topic_id : ‘create-topic’ );

    if ( is_forum() )

    echo “<input type=’hidden’ name=’forum_id’ value=’$forum->forum_id’ />n”;

    else if ( is_topic() )

    echo “<input type=’hidden’ name=’topic_id’ value=’$topic->topic_id’ />n”;

    do_action(‘post_form’);

    echo “n</form>”;

    } elseif ( !bb_is_user_logged_in() ) {

    echo ‘<p>’;

    printf(__(‘You must log in to post.’), attribute_escape( bb_get_option(‘uri’) . ‘bb-login.php’ ));

    echo ‘</p>’;

    }

    do_action(‘post_post_form’);

    }

    this is the last piece of the puzzle for me :D

    I removed it in css

    just added display: none

    because it is useless

    You should always put custom code in your template or plugin. Did my code not work in your template? If you want to create a custom designed jewel, so that replica cartier necklaces,you can choose the metal and the gemstone that make the perfect piece of cartier love ring white gold for you,you should take some things into consideration.First of all, ask for an expert gemologist’s advice in this matter.

Viewing 17 replies - 1 through 17 (of 17 total)
  • You must be logged in to reply to this topic.
Skip to toolbar