1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to hide a section when having no content?

Discussion in 'PHP' started by Divvy, Jan 20, 2023.

  1. #1
    Hey, can someone help me with this?

    I use this code in the single.php file of WordPress:
    https://paste2.org/weBNh8NI

    The most important code line is this:
    <?php if ( function_exists( 'echo_ald_crp' ) ) echo_ald_crp(); ?>
    Code (markup):
    This code will show related posts in a specific section of my site.
    I want to hide the section when there is no content to show:
    https://prnt.sc/opECWw4rK2zF

    Is it possible?

    Thank you in advance!
     
    Divvy, Jan 20, 2023 IP
  2. Divvy

    Divvy Well-Known Member

    Messages:
    781
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    Btw, I need to hide this entire code when the content is empty: https://paste2.org/weBNh8NI
    Because I'm using a get_template_part in single.php to load that code :)
     
    Divvy, Jan 21, 2023 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #3
    I'd just check "echo_ald_crp()" to see if it wasn't empty:

    <?php
    
    $var = echo_ald_crp();
    if(!empty($var)) {
    
    $grid = ot_get_option( 'grid_option', 'col-sm-' );
    $numrel=0;
    
    ?>
    
    <div class="container">
        <div class="row">
    
            <div class="<?php echo $grid;?>12">
                <div class="maddos-link-container">
    
                    <div class="row">
                    <div class="<?php echo esc_html( $grid );?>12">
                    <div class="maddos-link-header nocenter maddos-related-sites">
                        <h4 class="maddos-post-header">Videos from <?php the_title();?>:</h4>
                        <div class="maddos-link-header-back">
                            <a href="#top">
                                Back To Top
                                <span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
                            </a>
                        </div>
                    </div>
                    </div>
                    </div>
                    <div class="row">
                    <div class="<?php echo esc_html( $grid );?>12">
                    <div class="maddos-link-content">
                        <div class="maddos-url-links-wrapper clearfix">
                          
                           
    <?php if ( function_exists( 'echo_ald_crp' ) ) echo_ald_crp(); ?>
                                </div>
                        </div>
                    </div>
                    </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php }  ?>
    
    
    Code (markup):
    PS don't forget the closing tag. I am sure there are better ways of doing this.
     
    Last edited: Jan 22, 2023
    qwikad.com, Jan 22, 2023 IP
  4. Divvy

    Divvy Well-Known Member

    Messages:
    781
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Thank you, my friend!
    The idea is excellent but it didn't work as I expected, take a look to these screenshots:

    Before the code change: https://prnt.sc/KsoW5PiBQ8tq
    After the code change with content: https://prnt.sc/BQ4MGEFQ_8zD
    After the code change without content: https://prnt.sc/wqMdDPUsv4sz

    When having content, the code shouldn't be hidden, only when is empty like in the last screenshot.
    Any idea how to fix your code to not hide when having content? :)
     
    Divvy, Jan 23, 2023 IP
  5. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #5
    You're saying it hides the "VIDEOS FROM PLAYSTATION" title even if there's content, but the rest is being shown like it should, correct?
     
    qwikad.com, Jan 23, 2023 IP
  6. Divvy

    Divvy Well-Known Member

    Messages:
    781
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #6
    Correct, I just need to show the "VIDEOS FROM PLAYSTATION" when having content (videos).
    With your code, is removing "VIDEOS FROM PLAYSTATION" even when has content.
     
    Divvy, Jan 23, 2023 IP
  7. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #7
    I don't know why it's doing that.
     
    qwikad.com, Jan 23, 2023 IP
  8. Divvy

    Divvy Well-Known Member

    Messages:
    781
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #8
    Maybe because the content is added by the plugin that is using the code below?
    <?php if ( function_exists( 'echo_ald_crp' ) ) echo_ald_crp(); ?>
    Code (markup):
    I also have no more ideas...

    Btw, that code belongs to this plugin:
    https://wordpress.org/plugins/contextual-related-posts/
     
    Divvy, Jan 23, 2023 IP
  9. Divvy

    Divvy Well-Known Member

    Messages:
    781
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #9
    I think I already have a solution, which is replacing this code that I have in my single.php:
    get_template_part( 'inc/related-videos' );
    Code (markup):
    I need to add the code like that:
    if ( ! empty( get_crp_posts() ) ) { get_template_part( ‘inc/related-videos’ ); }
    Code (markup):
    But I already have an if in my code:
    
    if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
    get_template_part( 'content', get_post_format() ); 
    get_template_part( 'inc/related-videos' ); 
    get_template_part( 'inc/related-posts' ); 
    endwhile; 
    endif;
    PHP:
    And my PHP knowledge is very poor, how can I do it? :)
     
    Divvy, Jan 25, 2023 IP
  10. UpgradeIT.ro

    UpgradeIT.ro Member

    Messages:
    81
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    45
    #10
    Yes, it's possible to hide the section when there is no content to show. One way to accomplish this is to check if the function echo_ald_crp returns any content before outputting the section.

    You can use the following code snippet in your single.php file to achieve this:

    <?php
    $related_posts = echo_ald_crp();
    if ( ! empty( $related_posts ) ) {
    echo '<section class="related-posts">';
    echo $related_posts;
    echo '</section>';
    }
    ?>
    Code (markup):
    This code first calls the echo_ald_crp function and stores the output in the variable $related_posts. Then it checks if the variable is not empty and if true, it outputs the section with the related posts.

    Alternatively, you can also use the PHP function ob_start(), ob_get_clean() and empty() to check if the function echo_ald_crp() returns any content

    <?php
    ob_start();
    echo_ald_crp();
    $related_posts = ob_get_clean();
    if(!empty($related_posts)){
    echo '<section class="related-posts">';
    echo $related_posts;
    echo '</section>';
    }
    ?>
    Code (markup):
    This code uses the output buffering functions to capture the output of echo_ald_crp() and stores it in the variable $related_posts. Then it checks if the variable is not empty and if true, it outputs the section with the related posts.
     
    UpgradeIT.ro, Jan 25, 2023 IP
  11. Divvy

    Divvy Well-Known Member

    Messages:
    781
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #11
    Thank you for your response.

    I just need to edit this code:
    https://forums.digitalpoint.com/thr...when-having-no-content.2876530/#post-19738603

    I want to check the inc/related-videos only.
    The full code:

    if ( have_posts() ) :
    while ( have_posts() ) : the_post();
    get_template_part( 'content', get_post_format() );
    get_template_part( 'inc/related-videos' );
    get_template_part( 'inc/related-posts' );
    endwhile;
    endif; 
    PHP:
    I need to add in this code:
    if ( ! empty( get_crp_posts() ) ) { get_template_part( ‘inc/related-videos’ ); }
    PHP:
    But I don't know how to do it.
     
    Divvy, Jan 26, 2023 IP
  12. Divvy

    Divvy Well-Known Member

    Messages:
    781
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #12
    Solved! Thank you anyway guys!
     
    Divvy, Jan 27, 2023 IP