My go to guy is recuperating from heart surgery so I jumped into something today on my own. So far I haven't broken anything but I got stuck.

I am using Wordpress. I have several custom fields for each post. What I am working on doing is placing a php widget in the sidebar that grabs some of the data from the custom fields and displays the data in the sidebar.

Here's what my code looks like:

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '411_bus_name', true);
echo "<br />< br/>";
echo get_post_meta($postid, '411_bus_descrip', true);
echo "<br /><br />";
echo "<h5>Contact</h5>";
echo get_post_meta($postid, '411_bus_address', true);
echo "<br />";
echo get_post_meta($postid, '411_bus_phone1', true);
echo "<br />";
echo get_post_meta($postid, '411_bus_phone2', true);
echo "<br />";
echo get_post_meta($postid, '411_bus_email', true);
echo "<br />";
echo get_post_meta($postid, '411_bus_website', true);
wp_reset_query();
?>

The above works just fine, but it's not quite what I want. For the email data, I want to set up a link, where say the word 'Email' is linked to the data grabbed by get_post_meta($postid, '411_bus_email', true).

I've played for hours and got close but not close enough. The closest I've got is:

echo "<a href=\"mailto:\" ' .get_post_meta($postid, '411_bus_email', true). ' \">Email </a>";

The above works, sort of. It sets up the link, but the link only shows: mailto:

I just can't seem to figure out how to put it all together.

Any help provided will be greatly appreciated.

    writerdave;10997323 wrote:

    The above works, sort of. It sets up the link, but the link only shows: mailto:

    Of course it does; that's exactly what you set the 'href' attribute to. 🙂 (Take a look at the raw HTML output that code produces, and I think you'll find that you've got an extra double quote in there.)

      Hmmm. Well I removed one set of double quotes, thus:

      echo "<a href=\"mailto:\" ' .get_post_meta($postid, '411_bus_email', true). ' \">Email </a>";

      And now the above outputs a link of:

      mailto: ' .get_post_meta($postid, '411_bus_email', true). '

      That doesn't help me either.

        When posting PHP code, please use the board's [noparse]

        ..

        [/noparse] bbcode tags as they make your code much easier to read and analyze.

        Now that I've added those tags, it makes your problem more obvious: the first string piece is started by a double quote, yet you try to close it with a single quote. Likewise, the second string piece begins with a single quote, yet you try to close it with a double quote. A string must start and end with the same delimiter.

        Further more, you've still got the extra double quote in there, so even if you only fix the syntax errors you'll still end up with output that looks like:

        <a href="mailto:"email@example.com">Email </a>

          And it's often easier to use [man]printf[/man]/[man]sprintf[/man] to build strings containing lots of mixed single/duoble quotes along with variables that need to be placed in the string.

          # direct output
          printf('<a href="mailto:"&#37;s">Email </a>',
          	get_post_meta($postid, '411_bus_email', true)
          );
          # returned as string
          $string = printf('<a href="mailto:"%s">Email </a>',
          	get_post_meta($postid, '411_bus_email', true)
          );
          

          Pretty much the only thing you need to learn initially is that %s is a placeholder for a string, %d for signed decimal integer and %f for signed decimal float. There are more placeholders and things to do with them, but that'll get you started.
          See [man]sprintf[/man] for more info.

            Note that Johanafm's examples still have the extraneous double quote. They should instead look like:

            # direct output
            printf('<a href="mailto:&#37;s">Email </a>',
                get_post_meta($postid, '411_bus_email', true)
            );
            # returned as string
            $string = printf('<a href="mailto:%s">Email </a>',
                get_post_meta($postid, '411_bus_email', true)
            ); 

              A million thank you's, guys!

              Very much appreciated.

                Write a Reply...