WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Executing a function only if a post contains text (12 posts)

  1. venkmanuk
    Member
    Posted 1 year ago #

    Hi there,

    How can i check if a post or page has got text. That is, it might have other stuff like custom fields, a thumbnail etc.. but how can i tell that text was entered into the post's timyMce editor?

  2. mbrsolution
    Member
    Posted 1 year ago #

    I am not sure what your questions is?

    If you just want to know if a page or post has text when you are logged into your website admin panel. If you open a page or post select HTML and you will see the raw code or text.

    Kind regards

  3. WebTechGlobal
    Member
    Posted 1 year ago #

    The PHP for this is something like...

    if( isset( $post_content ) && !is_null( $post_content ) && !empty($post_content ) ){
    
        call_your_function();
    
    }

    You need to set $post_content using whatever method you are getting the post. I've answered this without assuming. Also for educational purposes the if is over doing it a little. Best to search each of the used functions and check php website.

    However if ever unsure it does not do harm to add each PHP function that checks for the existence of a variable and that content is in the variable. Just as I have done.

    Let us know how you are querying posts for further help.

  4. mbrsolution
    Member
    Posted 1 year ago #

    Sorry for my reply.......Your question was based on your title "Executing a function only if a post contains text"

  5. venkmanuk
    Member
    Posted 1 year ago #

    Thanks for the replies :)

    Yes, i'm looking for a php script that checks if the post has text. Unfortunately i don't think 'the_content' will work because it SEEMS to contain all the post's information - thumbnails and all that.

    i'm using the_content like this, but need to see if there's text ( or image ) content in the post.

    <?php while ( have_posts() ) : the_post(); ?>
                <div class="four columns module">				
    
                    <!-- Begin Module -->
                    <div>
                        <?php the_content();  ?>	
    
                    </div>
                    <!-- End Module -->
    
                </div>
    <?php endwhile; ?>

    so ideally it'd be:

    <?php while ( have_posts() ) : the_post(); ?>
          if (there is text or anything in there ) {
          // show the stuff
          } else {
          // do nothing }
     endwhile; ?>

    for the other elements i get an array of the child pages and show them. but that's besides the point. i'm sorry, it's a bit hard to explain.

  6. venkmanuk
    Member
    Posted 1 year ago #

    is it possible to identify if there is text by getting an array of get_the_content() ? hmm...

    this is close but doesn't work:
    if(post_content != "") {}

  7. WebTechGlobal
    Member
    Posted 1 year ago #

    Your on track...

    <?php while ( have_posts() ) : the_post(); ?>
    
      <?php
    $post_content = get_the_content(');
    
      if( !is_null( $post_content ) && !empty($post_content ) ){?>
    
                <div class="four columns module">				
    
                    <!-- Begin Module -->
                    <div>
                        <?php the_content();  ?>	
    
                    </div>
                    <!-- End Module -->
    
                </div>
      <?php } ?>
    
    <?php endwhile; ?>
  8. WebTechGlobal
    Member
    Posted 1 year ago #

    The if statement is probably best being if( $post_content != '' ) when your doing this in the loop.

    You could also do...

    if( get_the_content() != '' )

    However if you use $post_content you can also replace the_content() function and $post_content is available to do other things with. Just a little bit of extra information for anyone else browsing this.

  9. mbrsolution
    Member
    Posted 1 year ago #

    Hi I am not a php coder but this might provide some help with your request....override content

    I am doing my best to learn ;)

    Kind regards

  10. venkmanuk
    Member
    Posted 1 year ago #

    AAArgh @webtechglobal you are a genius! thank you.

    i had to remove a ' from your code, i assume that was just a typo. works perfectly. just for my own learning can i check what this means?

    $post_content = get_the_content();
    --> set a (new?) variable called $post_content equal to get_the_content()

    if( !is_null( $post_content ) && !empty($post_content ) ){
    --> if it doesn't-NOT exist and is also NOT empty, do whatever

    thanks again for all the replies folks.

  11. WebTechGlobal
    Member
    Posted 1 year ago #

    Thanks.

    I did notice the typo but figured you would realize quickly.

    You have it right. the ! means not, opposite to what the function states, at least in a well named function like these where it reads like a phrase "if is null" but we change it to "if is not null" by adding ! before is_null(). You understand it fully from what you wrote.

    Also...

    What I done was mentioned a couple of different approaches. One is to put the result from get_the_content() into variable. This uses memory and should really only be used if you need to do more with the content other than just output it.

    The other approach is to put the get_the_content() function into the if statement, avoid creating a variable, then use the_content() to output the content. I think that is better than creating a variable.

    Not a huge issue either way but if you want a quick loading script, do not do both. Do not create a variable that holds content prior to the if statement then also use the_content().

    We use get_the_content() because it returns the content where as the_content() prints it, no return possible with that function.

  12. venkmanuk
    Member
    Posted 1 year ago #

    oh right, thanks. I'll keep that in mind - always in search of perfection! lol

    seems like a good idea to conditionally create ( & load ) variables whenever possible.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.