ThemeShaper Forums » Thematic

[closed]

If/Else Conditional Problem via Functions.php Child Theme

(8 posts)
  • Started 4 years ago by nimrod
  • Latest reply from nimrod
  • This topic is not resolved
  1. nimrod
    Member

    I've used this if/else code with More Fields plugin before within template files before with success, but am having problems with using in functions.php file in Thematic child theme.

    Would someone mind staring at this code with me for a moment and tell me where the problem is?

    It might be just some php syntax problem I'm unaware of (as opposed to More Fields oriented).
    ___________

    Here's an example that looks for data in the author-website field and, if available, wraps that data in an anchor tag with the author-name data as the link text. Otherwise, it just displays the author-name data:

    <?php $author-website = get_post_meta($post->ID, 'author-website', true);
    if ($author-website) {
    	print '<a href="http://';
    	echo get_post_meta(get_the_ID(), 'author-website', true);
    	print '" title="View author&rsquo;s website">';
    	echo get_post_meta(get_the_ID(), 'author-name', true);
    	print '</a>';
    } else {
    	echo get_post_meta(get_the_ID(), 'author-name', true);
    } ?>

    Problem is, I get a syntax error about an unexpected "=" in the first line.

    If I put in double equals "==" in the first line like so:

    <?php $author-website == get_post_meta($post->ID, 'author-website', true);

    ...the page shows but ignores the first "if" section and only shows the "else" part which is just the author's name with no link even when there IS website data.
    ____________

    Any ideas on what about using the above code in a child theme functions.php file would cause this...?

    Posted 4 years ago #
  2. how about?

    $author-website = get_post_meta($post->ID, 'author-website', true);
    
    if ($author-website != '') {
    	echo '<a href="http://'. $author-website . '" title="View author&rsquo;s website">' . get_post_meta(get_the_ID(), 'author-name', true) . '</a>';
            } else {
    	echo get_post_meta(get_the_ID(), 'author-name', true);
    }
    Posted 4 years ago #
  3. nimrod
    Member

    Thanks for your smart input (as usual Ms. Viking :-).

    It's a good question. There was some reason in the past project I took this from but honestly I can't remember now why.

    Anyhow, the concatenated code you gave works (with "==" added), but has the same result: the first part is ignored and only the name is returned.

    I tested this by placing "foo" after the else and that's exactly what gets shown on the front end. I'm stumped.

    Posted 4 years ago #
  4. what do you get if you echo $author-website outside of the if statement?

    Posted 4 years ago #
  5. Goran
    Member

    You cannot use '-' (dash) in the variable name.

    Posted 4 years ago #
  6. good to know. i have always used underscores b/c the people i was learning from always did... w/o explicitly knowing why.

    Posted 4 years ago #
  7. nimrod
    Member

    Well, I pulled the dashes from all variable names without any change. The result is the same: Name shows from the else segment only.

    @helgatheviking: if I add the following code outside the if statement, it simply does as expected and returns the text for the entered website address:

    <?php echo get_post_meta(get_the_ID(), 'author_website', true); ?>
    _________

    This new version has some success and actually shows the name with the proper link. But, it does NOT use the else segment when NO web site data is entered in the field (instead it links the author name with just "http:/" rather than just showing the name):

    <?php $author_website == get_post_meta($post->ID, 'author_website', true);
    if ($author_website !== '') {
    	echo '<a href="http://' . get_post_meta(get_the_ID(), 'author_website', true) . '" title="View author&rsquo;s website">' . get_post_meta(get_the_ID(), 'author_name', true) . '</a>';
    } else {
    	echo get_post_meta(get_the_ID(), 'author_name', true);
    } ?>

    Note that both lines 1 and 2 utilize double equals "==" (if not, then just the name shows up) and I used the longer ' . get_post_meta(get_the_ID(), 'author_website', true) . ' in line 3 instead of just ' . $author_website . '

    So, the question is: is there some syntax or other issue that stops it from using the else segment when no data is entered in the author website field?
    _________

    I finally have a prototype site that shows the above code in action (for now):

    http://themagpielist.com/page/2/

    The first name at the bottom of the list shows a properly linked name while the other two posts do not have website data entered in the author_website field and show the wrong link.

    Posted 4 years ago #
  8. nimrod
    Member

    Well, thanks for the comments. I've not been able to figure out what is wrong.

    I had to resort to using template files for More Fields if/else code instead of cleaner method of the functions.php file.

    Posted 4 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.