• Resolved Begin

    (@bentalgad)


    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!

Viewing 15 replies - 1 through 15 (of 22 total)
  • Under what conditions would a post have no excerpt?

    Thread Starter Begin

    (@bentalgad)

    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).

    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.

    Thread Starter Begin

    (@bentalgad)

    oh sorry! I edited that now!

    Moderator keesiemeijer

    (@keesiemeijer)

    try:

    <?php
    if($post->post_excerpt == ''){
    // no excerpt
    } else {
    // excerpt
    }
    ?>

    Try something like:

    if( $post->post_excerpt ) : the_excerpt();
    else:
    [your code]
    endif;
    Thread Starter Begin

    (@bentalgad)

    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. ”

    Moderator keesiemeijer

    (@keesiemeijer)

    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; ?>

    Thread Starter Begin

    (@bentalgad)

    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!!!!

    Thread Starter Begin

    (@bentalgad)

    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 ?

    Moderator keesiemeijer

    (@keesiemeijer)

    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;
    }
    ?>

    Thread Starter Begin

    (@bentalgad)

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

    Thank’s again and again!!!

    Moderator keesiemeijer

    (@keesiemeijer)

    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

    Thread Starter Begin

    (@bentalgad)

    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

    Moderator keesiemeijer

    (@keesiemeijer)

    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;
    }
    ?>

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘alternative excerpt if doesn't have excerpt’ is closed to new replies.