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!
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
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.
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?
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?
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.
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/
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?
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.
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.