0

I am creating a new baseball blog. When submitting a post, I want Authors to cite their source for a news type of post.

I have created a custom meta box with three custom fields:

  • Source title, the title of the source. Field name - source_title
  • Source URL, the URL of the source. Field name - source_url
  • Source name, the name of the person or website of the information. Field name - source_name

I want to display every bit of information the Author gives, so credit goes where it is deserved.

The output of the code (with all fields present) will be something like:

<h3><a href="source_url">source_title</a> | source_name</h3>

When the source_url is entered, but not source_title, the output should be:

<h3><a href="source_url">source_url</a> | source_name</h3>

(it would be nice to trim the source_url to 25 or so characters and remove http:// if possible)

Through searching and help elsewhere, this is what I have so far:

<?php 
$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>Source: ';
  $output .= ($url != '' && $title != '') ? '<a href="'.$url.'">': '';
  if($title != '' && $name != '') {
  $seperator = ' | ';
}
  $output .=  $title;
  $output .= ($url != '' && $title != '') ? '</a>': '';
  $output .= $seperator.$name;
  echo $output . '</h3>';
}
?>

What works:

  • 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 but no title, 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 (no title or name), nothing shows.

In this case, I would like to display:

<h3><a href="source_url">source_url</a></h3>

Thanks in advance for helping out this novice.

1 Answer 1

0

I'm not sure if I'm meeting all of your requirements here, but I think this might work.

<div class="source">
<?php

$name = get_post_meta($post->ID, 'sourcename', true);
$url = get_post_meta($post->ID, 'sourceurl', true);
$title = get_post_meta($post->ID, 'sourcetitle', true);

$snippet = str_replace( "http://", "", $url );
$snippet = substr( $snippet, 0, 22 ) . "...";

if ( !empty( $title ) && !empty( $name ) && !empty( $url ) )
    $output = '<h3 class="cite"><a href="' . $url . '">' . $title . '</a> | ' . $name . '</h3>';
else if ( !empty( $name ) && !empty( $url ) )
    $output = '<h3 class="cite"><a href="' . $url . '">' . $name .  '</a></h3>';
else if ( !empty( $title ) && !empty( $url ) )
    $output = '<h3 class="cite"><a href="' . $url . '">' . $title . '</a></h3>';
else if ( !empty( $title ) && !empty( $name ) )
    $output = '<h3 class="cite">' . $title . ' | ' . $name . '</h3>';
else if ( !empty( $title ) )
    $output = '<h3 class="cite">' . $title . '</h3>';
else if ( !empty( $name ) )
    $output = '<h3 class="cite">' . $name . '</h3>';
else if ( !empty( $url ) )
    $output = '<h3 class="cite"><a href="' . $url . '">' . $snippet . '</a></h3>';
else
    $output = '';

echo $output;

?>
</div>

Let me know if it doesn't!

9
  • dunc, thanks for the reply. I replaced my entire code listed above with your code. Unfortunately, nothing displays at all with your code. Also, there is a missing semi-colon at the end of $output = '<h3><a href="' . $url . '">' . $title . '</a> | ' . $name . '</h3>' Mar 26, 2012 at 19:28
  • Code amended - try now :)
    – turbonerd
    Mar 26, 2012 at 19:30
  • Looks like it is also missing requirements for url, title & title, url & title only Mar 26, 2012 at 19:32
  • Can you list those again with semi-colons? Do you mean url; title & ?; title only?
    – turbonerd
    Mar 26, 2012 at 19:36
  • Sorry, I realized it was confusing after I submitted the comment. Still need requirements for: url title; title name; title; name. Also, the final else should just be an empty display, in case the Author does not enter any citation information. Mar 26, 2012 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.