Forums

[resolved] alternative excerpt if doesn't have excerpt (23 posts)

  1. Begin
    Member
    Posted 7 months ago #

    I have this post template that starts with the regular "if have posts"
    then displays different parts of the post (get title, get excerpt, get content...)
    and in the place where it takes the excerpt I want to tell him:

    If there is a manual (inserted) excerpt, show it, and if not show the code that i will provide that uses some free text and some "get custom fields" in between the words.

    How can I code that?

    Thank's!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 7 months ago #

    Under what conditions would a post have no excerpt?

  3. Begin
    Member
    Posted 7 months ago #

    I don't really get the question?! if I don't insert excerpt to a post,
    then it has no excerpt (I mean it's blank, of course you can have it
    add the automatic excerpt, but wp knows that field is empty in that post).

  4. esmi
    Theme Diva & Forum Moderator
    Posted 7 months ago #

    So you're talking about the optional manual excerpt - not the automatic excerpt? Your original post wasn't clear and the devil is always in the details.

  5. Begin
    Member
    Posted 7 months ago #

    oh sorry! I edited that now!

  6. keesiemeijer
    moderator
    Posted 7 months ago #

    try:

    <?php
    if($post->post_excerpt == ''){
    // no excerpt
    } else {
    // excerpt
    }
    ?>
  7. esmi
    Theme Diva & Forum Moderator
    Posted 7 months ago #

    Try something like:

    if( $post->post_excerpt ) : the_excerpt();
    else:
    [your code]
    endif;
  8. Begin
    Member
    Posted 7 months ago #

    but how do I put the other php's in that (get excerpt, get custom field)?

    Lets say I wanted to be if there is one: show it,
    if there isn't show:
    " this post is <custom field content here> and has the <custom field content here>
    in it. "

  9. keesiemeijer
    moderator
    Posted 7 months ago #

    try it with something like this:

    <?php
      if( $post->post_excerpt) :
        the_excerpt();
      else : ?>
    <?php
      $first = get_post_meta( $post->ID , 'first', true);
      $second = get_post_meta( $post->ID , 'second', true);
      if($first && $second) : ?>
        <p>this post is <?php echo $first; ?> and has the <?php echo $second; ?>
    in it.</p>
    <?php endif; ?>
    <?php endif; ?>
  10. Begin
    Member
    Posted 7 months ago #

    I got it working in the end with a similar code:

    <?php if ( ! has_excerpt() ) {
                $name = get_post_meta($post->ID, 'name', true);
                $experience = get_post_meta($post->ID, 'experience', true);
                echo "owns a company named $name with a  $experience years of experience";
                } else {
                the_excerpt();
                }?>

    Thank's!!!!

  11. Begin
    Member
    Posted 7 months ago #

    The real complicated (to me :-) thing I wanna do now is that:

    in that alternativ text i want to insert the age of a person,
    weich will be automatically calculated by getting is year, month and day
    of birth from three custom fields i have with those details.

    Is that a crazy thinking or is it possible ?

  12. keesiemeijer
    moderator
    Posted 7 months ago #

    Try it with this:

    <?php
    $birthDate =array();
    $birthDate[] = get_post_meta($post->ID, 'month', true);
    $birthDate[] = get_post_meta($post->ID, 'day', true);
    $birthDate[] = get_post_meta($post->ID, 'year', true);
    $birthDate = array_filter($birthDate);
    //get age from date or birthdate
    if(!empty($birthDate)) {
    $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y")-$birthDate[2])-1):(date("Y")-$birthDate[2]));
    echo "Age is:".$age;
    }
    ?>
  13. Begin
    Member
    Posted 7 months ago #

    Wow amazing!!! works great! and it will be always up to date right?!

    Thank's again and again!!!

  14. keesiemeijer
    moderator
    Posted 7 months ago #

    Yes, but make this alteration:

    Change this:

    if(!empty($birthDate)) {

    to this:

    if(!empty($birthDate) && (count($birthDate) == 3)) {

    This will ensure that if the birthdate is not complete it will not show it

  15. Begin
    Member
    Posted 7 months ago #

    if I change it it makes an error:

    Parse error: syntax error, unexpected ';' in /home/bentalgad/MySites/guitara.co.il/wp-content/themes/mguitara/single_teacher.php on line 26

  16. keesiemeijer
    moderator
    Posted 7 months ago #

    This is my working code:

    <?php
    $birthDate =array();
    $birthDate[] = get_post_meta($post->ID, 'month', true);
    $birthDate[] = get_post_meta($post->ID, 'day', true);
    $birthDate[] = get_post_meta($post->ID, 'year', true);
    $birthDate = array_filter($birthDate);
    //get age from date or birthdate
    if(!empty($birthDate) && (count($birthDate) == 3)) {
    $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y")-$birthDate[2])-1):(date("Y")-$birthDate[2]));
    echo "Age is:".$age;
    }
    ?>
  17. Begin
    Member
    Posted 7 months ago #

    I don't know why, but the previous code you made is working great,
    and when I try to replace to the last one it brings the error...

  18. keesiemeijer
    moderator
    Posted 7 months ago #

    Can you show your (broken) code here?

  19. Begin
    Member
    Posted 7 months ago #

    <?php if ( ! has_excerpt() ) {
                $experience = get_post_meta($post->ID, 'experience', true);
                $name = get_post_meta($post->ID, 'name', true);
                $birthDate =array();
    $birthDate[] = get_post_meta($post->ID, 'birth_month', true);
    $birthDate[] = get_post_meta($post->ID, 'birth_day', true);
    $birthDate[] = get_post_meta($post->ID, 'birth_year', true);
    $birthDate = array_filter($birthDate);
    //get age from date or birthdate
    if(!empty($birthDate)) {
    $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y")-$birthDate[2])-1):(date("Y")-$birthDate[2]));}
    
                echo "he is a $age years old. owns $name for $experience years";
                } else {
                the_excerpt();
                }?>

    (That's the working one!)

  20. keesiemeijer
    moderator
    Posted 7 months ago #

    I cannot recreate the error. Try it with this: http://pastebin.com/zDc9SLAE

  21. Begin
    Member
    Posted 7 months ago #

    I think i will just stay with that code... anyway I don't think
    there will be such a case wich the date won't be complete
    because they fill it in a form with drop menus for the date
    and it won't let them send the form without choosing a birth date...

  22. keesiemeijer
    moderator
    Posted 7 months ago #

    That's fine by me.

  23. Begin
    Member
    Posted 7 months ago #

    Thank's!!!

Reply

You must log in to post.

About this Topic