• Resolved Divvy

    (@divvy)


    Hello, maybe someone can help me.

    I’m using this code in my single.php:
    get_template_part( ‘inc/related-videos’ );

    Is it possible to hide this code when no content is available?
    This is my inc/related-videos code:
    https://paste2.org/weBNh8NI

    I tried check “echo_ald_crp()” to see if it wasn’t empty:
    $var = echo_ald_crp();
    if(!empty($var)) {

    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 the code to not hide when having content? 🙂

    Thank you in advance!

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author Ajay

    (@ajay)

    I’m a bit confused with the various screenshots. But, I think I get the gist. The issue is that echo_crp() will not return empty because it hold the div in there. Since you’re editing code, I’d suggest using CRP_Query as you would use WP_Query.

    https://webberzone.com/support/knowledgebase/crp-query/

    Alternatively, you can check if get_crp_posts returns anything. Would suggest enabling the caching in the settings page as you’ll end up running two queries.

    • This reply was modified 1 year, 5 months ago by Ajay.
    Thread Starter Divvy

    (@divvy)

    Thank you for your response, @ajay

    Unfortunately, my PHP knowledge is very poor. Can you please help me change the code for me? https://paste2.org/weBNh8NI

    I hope you can help me with this. Thanks! 🙂

    Plugin Author Ajay

    (@ajay)

    Can you please paste the code you have in the template probably somewhere like pastebin or gist.github.com.

    Alternatively, here but please wrap that into a code block.

    Thread Starter Divvy

    (@divvy)

    Thank you, @ajay

    But I already pasted the code here:
    https://paste2.org/weBNh8NI

    That is the code that I have in the file:
    get_template_part( ‘inc/related-videos’ );

    That is loaded in single.php

    I just need to hide this content when the content is empty.

    Thank you in advance 😉

    Plugin Author Ajay

    (@ajay)

    Sorry. Thought it was a screenshot!

    Easier way to do this would be:

    if ( ! empty( get_crp_posts() ) ) { get_template_part( ‘inc/related-videos’ ); }

    Thread Starter Divvy

    (@divvy)

    Thank you for the code, @ajay

    This is my code in single.php:

    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;

    I already have an if here, how can I add your code?
    Sorry for the newbie question 🙂

    Plugin Author Ajay

    (@ajay)

    Replace this line with what I posted above.

    get_template_part( 'inc/related-videos' );

    Thread Starter Divvy

    (@divvy)

    @ajay it was what I did first.

    The code works fine in posts that don’t have related videos.
    But for the posts that have videos I get the following error:
    There has been a critical error on this website.

    Full single.php code:

    <?php
    
    get_header();
    
    if ( have_posts() ) :
    while ( have_posts() ) : the_post();
    get_template_part( 'content', get_post_format() );
    if ( ! empty( get_crp_posts() ) ) { get_template_part( ‘inc/related-videos’ ); }
    get_template_part( 'inc/related-posts' );
    endwhile;
    endif;
    
    get_footer(); 
    
    ?>
    Plugin Author Ajay

    (@ajay)

    Did you get the error before making the above change?
    if so, what happens when you delete this line in the related-videos file

    <?php if ( function_exists( 'echo_ald_crp' ) ) echo_ald_crp(); ?>
    Thread Starter Divvy

    (@divvy)

    @ajay I only get the error after I add your code, the one that I posted above. After I revert the changes, the error disappear.

    Plugin Author Ajay

    (@ajay)

    I think it might be the single quotes around inc/related-videos

    Can you please check they are a clean and simple single quote and not the formatted ones.

    Thread Starter Divvy

    (@divvy)

    Thank you very much, @ajay !
    Worked perfectly 😀

    The problem was in the single quotes like you said.
    I didn’t notice they were formatted.

    Thank you once again for the excellent support!! 😉

    Plugin Author Ajay

    (@ajay)

    Thank you for confirming. Please do consider a review of the plugin if it is working well for you.

    Thread Starter Divvy

    (@divvy)

    @ajay done 😉

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘How to hide a section when having no content?’ is closed to new replies.