• By no means am I an expert in php or coding really, so a complete dumbing down would be great.

    I use Easy Custom Content Types (https://pippinsplugins.com/easy-content-types/) for my custom post types and custom meta boxes/custom fields.

    My website is a baseball blog. I have created 3 fields to allow Authors to cite their main source in a news type of blog post:

    Source title – The title of the article, blog post, etc
    Source URL – The URL of the source
    Source Name – Either the website name or person name

    I want to display the source at the end of the article as:

    Sample Source Title | Sample Source Name

    The issue: All 3 fields are not always going to be present. For example, if the blog post refers to a radio or TV show, there will be no link, just the show name and the person name, or maybe even simple the person’s name.

    I guess what I need is a conditional statement for all the combination possibilities?

    echo get_post_meta($post->ID, 'ecpt_fieldname', true);

    is used to call the custom field in the template.

    Thanks for your help

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    try:

    $name = get_post_meta($post->ID, 'ecpt_fieldname', true);
    if($name != '') {
    echo $name
    }

    Thread Starter Travis Pflanz

    (@tpflanz)

    Thanks for the reply, but that only deals with one custom field, not all three, correct?

    I need a condition that will display each possible combination of the three fields.

    If the Author enters all 3 fields, it would produce something like:

    <h3><a href="https://espn.com/peyton-manning-signs-with-broncos">Peyton Manning signs with the Denver Broncos</a> | Adam Schefter</h3>

    If only the source title (from TV show, for example) and source name are entered it would produce:

    <h3>Inside the NFL</a> | Adam Schefter</h3>

    If only the source name is entered it would produce:
    <h3>Adam Schefter</h3>

    if only the source title is entered it would produce:
    <h3>Inside the NFL</h3>

    etc, etc… I hope that is more clear

    Thread Starter Travis Pflanz

    (@tpflanz)

    My field names:

    Source title = source_title
    Source URL = source_url
    Source name = source_name

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this:

    $seperator = '';
    
    $name = get_post_meta($post->ID, 'source_name', true);
    $url = get_post_meta($post->ID, 'source_url', true);
    $title = get_post_meta($post->ID, 'source_title', true);
    
    if($title != '' || $name != '' || ($url != '' && $title != '')){
      $output = '<h3>';
      $output .= ($url != '' && $title != '') ? '<a href="'.$url.'">': '';
      if($title != '' && $name != '') {
        $seperator = ' | ';
      }
      $output .=  $title;
      $output .= ($url != '' && $title != '') ? '</a>': '';
      $output .= $seperator.$name;
      echo $output . '</h3>';
    }

    `

    Thread Starter Travis Pflanz

    (@tpflanz)

    keesiemeijer, thanks for your help. There are still a few conditions this code does not account for.

    I want to make sure that any citation information entered by an Author is displayed… I mean, I want to make sure credit goes to where it is deserved.

    When all three fields are entered everything is perfect.

    When title and name are entered, but not url everything is perfect.

    When title and url are entered, but not name everything is perfect.

    When only name is entered everything is perfect

    When only title is entered everything is perfect

    The problems:

    When url and name are entered, only the name shows. In this case, I would like to display:
    <h3><a href="source_url">source_url</a> | source_name</h3>

    Similar is true when only the url is entered, nothing shows. In this case, I would like to display:
    <h3><a href="source_url">source_url</a></h3>

    Thanks for your help, and sorry if I am over-explaining… I am accused of that a lot, but better than creating confusion.

    Thread Starter Travis Pflanz

    (@tpflanz)

    Not to be a pain… More than I already am… But I just realized some of these urls may be very long, a method to shorten them would be appreciated. I don’t really care about the method. I know I have seen some sites that do https://example.com/th…al.htm, for example

    Thread Starter Travis Pflanz

    (@tpflanz)

    keesiemeijer, thanks again for the help. I got it all figured out with help at Stack Exchange.

    Here is the final code, with URL shortening. Also, I decide that if the Author only enters name url, but no title, I would link the name to the source url, rather than displaying a snippet. The snippet is only displayed if the Author enters only a source url.

    <?php
        $name = get_post_meta($post->ID, 'source_name', true);
        $url = get_post_meta($post->ID, 'source_url', true);
        $title = get_post_meta($post->ID, 'source_title', true);
    
        $snippet = str_replace( "https://", "", $url );
        $snippet = substr( $snippet, 0, 22 ) . "...";
    
        if ( !empty( $title ) && !empty( $name ) && !empty( $url ) )
            $output = '<h3><a href="' . $url . '">' . $title . '</a> | ' . $name . '</h3>';
        else if ( !empty( $name ) && !empty( $url ) )
            $output = '<h3><a href="' . $url . '">' . $name .  '</a></h3>';
    	else if ( !empty( $title ) && !empty( $url ) )
            $output = '<h3><a href="' . $url . '">' . $title . '</a></h3>';
    	else if ( !empty( $title ) && !empty( $name ) )
            $output = '<h3>' . $title . ' | ' . $name . '</h3>';
    	else if ( !empty( $title ) )
            $output = '<h3>' . $title . '</h3>';
        else if ( !empty( $name ) )
            $output = '<h3>' . $name . '</h3>';
        else if ( !empty( $url ) )
            $output = '<h3><a href="' . $url . '">' . $snippet . '</a></h3>';
    	else
    		$output = '';
    
        echo $output;
    
    ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Create a link to cite source using 3 custom fields’ is closed to new replies.