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.