Forums

Help with PHP (if, elseif, else) Statements (7 posts)

  1. nativebiz
    Member
    Posted 1 year ago #

    G'day,

    I would greatly appreciate some assistance with (2) separate php statements.

    I have uploaded gd-star-ratings and need to display different multi-rating blocks based on the category a post is located in.

    Note: These rating blocks are being integrated into the comments section via comments.php and template_functions.php (Hybrido Theme)

    =====================================

    COMMENTS.PHP

    ORIGINAL STATEMENT (Works)

    <?php if (defined("STARRATING_INSTALLED")) : ?>
    <label for="url"><small>Rating</small></label>
    <p><?php wp_gdsr_comment_integrate_multi_rating(2); ?>
    </p><?php endif; ?>

    NEW STATEMENTS

    <?php if (in_category('9') && defined("STARRATING_INSTALLED")) : ?>
    <label for="url"><small>Rating</small></label>
    <p><?php wp_gdsr_comment_integrate_multi_rating(2); ?>
    </p><?php endif; ?>
    <?php if (in_category('18') && defined("STARRATING_INSTALLED")) : ?>
    <label for="url"><small>Rating</small></label>
    <p><?php wp_gdsr_comment_integrate_multi_rating(3); ?>
    </p><?php endif; ?>
    <?php if (in_category('20') && defined("STARRATING_INSTALLED")) : ?>
    <label for="url"><small>Rating</small></label>
    <p><?php wp_gdsr_comment_integrate_multi_rating(4); ?>
    </p><?php endif; ?>

    Not sure if it's okay to do this way WITHOUT using (if, elseif, else) but it seems to be working with each category showing it's respective rating block.

    =====================================

    TEMPLATE_FUNCTIONS.PHP

    ORIGINAL STATEMENT (Works)

    <?php if (defined("STARRATING_INSTALLED")) : ?>
    <div style="float: right">
    <?php  wp_gdsr_comment_integrate_multi_result(get_comment_ID(), 2, 51, "crystal", 16); ?>
    </div>
    <?php endif; comment_text(); ?> 
    
    <div><?php wp_gdsr_render_comment_thumbs(26); ?></div>

    NOTE: (2) represents the multi rating block ID

    NEW STATEMENTS

    <?php if (in_category('9') && defined("STARRATING_INSTALLED")) : ?>
    <div style="float: right">
    <?php  wp_gdsr_comment_integrate_multi_result(get_comment_ID(), 2, 51, "crystal", 16); ?>
    </div>
    <?php endif; comment_text(); ?> 
    
    <div><?php wp_gdsr_render_comment_thumbs(26); ?></div>
    <?php if (in_category('18') && defined("STARRATING_INSTALLED")) : ?>
    <div style="float: right">
    <?php  wp_gdsr_comment_integrate_multi_result(get_comment_ID(), 3, 51, "crystal", 16); ?>
    </div>
    <?php endif; comment_text(); ?> 
    
    <div><?php wp_gdsr_render_comment_thumbs(28); ?></div>
    <?php if (in_category('20') && defined("STARRATING_INSTALLED")) : ?>
    <div style="float: right">
    <?php  wp_gdsr_comment_integrate_multi_result(get_comment_ID(), 5, 51, "crystal", 16); ?>
    </div>
    <?php endif; comment_text(); ?> 
    
    <div><?php wp_gdsr_render_comment_thumbs(30); ?></div>

    The above format does display correct blocks in comments based on category, BUT the (comment text) and (thumbs block - ID 26) are being shown three times for each comment.

    I have spent a few days trying different (if, elseif, else) statements, but I just can't seem to get it right.

    If someone has the time to help with these statememts, it would be greatly appreciated.

    Thanks.

  2. Digital Raindrops
    Member
    Posted 1 year ago #

    UNTESTED
    Use a condition and tidy it up also moving the image output inside the condition.

    <?php
    if( in_category( array( '9','18','20' ) )){
    	if(in_category('9')) {
    		$rating=2;
    		$thumb=26;
    	} elseif(in_category('18')){
    		$rating=3;
    		$thumb=28;
    	} elseif(in_category('20')){
    		$rating=5;
    		$thumb=30;
    	} ?>
    	<div style="float: right">
    		<?php  wp_gdsr_comment_integrate_multi_result(get_comment_ID(), $rating, 51, "crystal", 16); ?>
    	</div>
    	<?php comment_text(); ?>
    	<div><?php wp_gdsr_render_comment_thumbs($thumb); ?></div>
    	<?php
    } else {
    	comment_text();
    }
    ?>

    HTH

    David

  3. Digital Raindrops
    Member
    Posted 1 year ago #

    Missed the Star Rating Installed condition, should be!

    <?php
    if( in_category( array( '9','18','20' ) ) && defined("STARRATING_INSTALLED") ){

    HTH

    David

  4. nativebiz
    Member
    Posted 1 year ago #

    David,

    Thanks so much, I added this to template_functions.php and this works perfectly. I greatly appreciate your time and assistance.

    One question, should I also create a statement based on yours for the (comments.php) page where I simply copied the (if) statement a changed id's, or are they ok as is? (see top of original post)

    Thanks again.

    Chris

  5. Digital Raindrops
    Member
    Posted 1 year ago #

    Hi not sure on that one but if you have the condition in the template file then I would replicate it in the comments file if required.

    I am not familiar with the theme only looked at the code, I missed the two file part, so yes copy the function, change the div and php calls using the same logic in comments.php without the $thumb parts.

    UNTESTED:

    <?php
    if( in_category( array( '9','18','20' ) )){
    	if(in_category('9')) {
    		$rating=2;
    	} elseif(in_category('18')){
    		$rating=3;
    	} elseif(in_category('20')){
    		$rating=4;
    	} ?>
    	<label for="url"><small>Rating</small></label>
    	<p><?php wp_gdsr_comment_integrate_multi_rating($rating); ?></p>
    <?php
    }
    ?>

    Note: You have for category 20, rating 4 in the comments.php and 5 in the template_functions.php section, as per your first post!

    HTH

    David

  6. helplisa
    Member
    Posted 3 months ago #

    <?php if (is_category('lightbox')) { <br />
    };
    ?>

    I learned this much from this post.

    I have created posts for different lightboxs.

    For lightbox post i have catagory called 'lightbox' When i click on link.

    It opens whole post with header and sidebar and everything.

    I was wondering if we can hide header, sidebar, footer everything except that post data. That would be great...

    Please let me know if its possible and how can i achive this.

    Thanks a ton.

    * I am using twenty eleven theme.

  7. helplisa
    Member
    Posted 3 months ago #

    Anyone..??

Topic Closed

This topic has been closed to new replies.

About this Topic