WordPress.org

Ready to get started?Download WordPress

Forums

Link and link text as custom field, and hide the lot of it when it's not in use (3 posts)

  1. SchuminWeb
    Member
    Posted 2 years ago #

    I'm trying to create a link on my blog where custom fields dictate the URL and the linked text, and I also want to hide the whole thing when it is not used in a post.

    This is how it currently works on my non-WordPress site:

    http://www.schuminweb.com/schumin-web/journal/permalink.php?id=1358

    Note the "Web site" part. The link text is one database field, and the URL in the link tag is a separate database field. Additionally, some PHP exists to hide it when the link text field is blank.

    In building this feature in WordPress, I'm having trouble reproducing this feature. I've defined the link text using the custom field "website", and the URL itself as "website_url". I can get the link text to display on its own unlinked using this:

    <?php
    $var = get_post_meta($post->ID, 'website', true);
    if ($var == '')
    {
    echo "";
    } else {
    echo "<p><b>Web site:</b> " . $var . "</p>";
    } ?>

    But then when I modify it to add the URL in there, I get an error:

    <?php
    $var = get_post_meta($post->ID, 'website', true);
    if ($var == '')
    {
    echo "";
    } else {
    echo "<p><b>Web site:</b> <a href="<?php echo get_post_meta($post->ID, 'website_url', true); ?>">" . $var . "</A></p>";
    } ?>

    That nets me this error:

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in C:\xampp\htdocs\wp-content\themes\schumin-web-blue-squares\index.php on line 19

    Any ideas?

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    the error is caused by the "" in href="" which is breaking the string;

    try:

    echo '<p><b>Web site:</b> <a href="">' . $var . '</a></p>';
  3. SchuminWeb
    Member
    Posted 2 years ago #

    So far, so good. That doesn't cause a PHP error as it did before, however, there's no link in there. If I put the URL's custom field in between those quote marks in the a tag, then I get myself in trouble again. Is there a way to put the custom field for the URL in there without causing the site to break?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.