0

I have the following code registering metabox with form input used to input live project link

    function portfolio_meta_box() {

  add_meta_box ('project-meta', 'Add Project Link', 'portfolio_meta_options',  'portfolio', 'side', 'low');

  }

  add_action("admin_init", "portfolio_meta_box");

  function portfolio_meta_options(){

        global $post;

        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
        $custom = get_post_custom($post->ID);
        $link = $custom ['project-link'][0];
  ?>

  <input name="project-link" value="<?php echo $link; ?>" />
  <?php
   }


//save custom meta boxes when the post is saved


function save_project_link (){

  global $post;

  if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )  {
     return $post_id;
  }

  else  {

    update_post_meta ($post->ID, 'project-link', $_POST['project-link']);
  }
}

add_action ('save_post', 'save_project_link');

In my single-portfolio.php I am using this code to output a project URL

<?php

get_header(); ?>

<main class="view"> 

   <?php $link= get_post_custom_values('project-link'); 
      if($link[0] != "") ://!= not equal empty string
    ?>                     
       <a href="<?=$link[0]?>" target="_blank">view site</a>

      <?php  else: ?>

      <em>live link unavailable</em>

      <?php endif; ?>

      <?php     while ( have_posts() ) :the_post(); the_content(); endwhile;?>

</main><!-- .site-main -->


<?php get_footer(); ?>

which gives me the following output (see left column on the screenshot) but I was wondering how can I change the code to give me the output shown in right column (its a clickable link which should appear under the first project screenshot only)

enter image description here

My second question is I've noticed in $link = $custom ['project-link'][0]; and

'<?php $link= get_post_custom_values('project-link'); 
      if($link[0] != "") ://!= not equal empty string
    ?>                     
       <a href="<?=$link[0]?>" target="_blank">view site</a>'

we use ['project-link'][0]; and $link[0]. Is the first one ['project-link'][0];an indexed array with zero index which makes it pick the 1st value in the array? as to the second $link[0] why do we need zero index in that case?

0

You can always use Wordpress Code Reference to understand what is going on:

get_post_custom() is a function that retrieves post meta fields and needs a post id as an parameter. It returns an array with post meta for the given post: https://developer.wordpress.org/reference/functions/get_post_custom/

get_post_custom_values() is a function that retrieves values for a custom post field and accepts the key and the post id as parameters. It returns an array of meta field values. https://developer.wordpress.org/reference/functions/get_post_custom_values/

So you need the indexes to access the first position in the arrays. If you want to take a look at the the indexes of the array, you can output them like:

$key_values = get_post_custom_values( 'project-link' );

echo $key_values[0];

This will show you what the array contains on index position 0. Try it with 1 or 2, if you want to see whats inside of these index positions.


The other question was, how you get the desired output. First of all you want to show the url, so you have to put it between the a tags:

<a href="<?php echo $link[0]; ?>" target="_blank"><?php echo $link[0]; ?></a>

And if you want to put it below your content, you need to put your the_content() call above the a tag. Also I guess you should use it inside your post loop and use the function get_post_custom_values with the post id:

<main class="view">
    <?php
        while ( have_posts() ) :the_post();
            the_content();
            $current_post = get_the_ID();
            $link= get_post_custom_values('project-link', $current_post); 
            if($link[0] != "") { ?>
                <a href="<?php echo $link[0]; ?>" target="_blank"><?php echo $link[0]; ?></a>
            <?php } else { ?>
                <em>live link unavailable</em>
            <?php }
        endwhile;
    ?>
</main><!-- .site-main -->

Hope this helps and I understood your question correctly.

| improve this answer | |
  • foreach loop to output the whole array throws an error Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ';' – 810311 May 17 at 1:37
  • I edited my answer. You do not actualy need to do a for each, because you just want to check whats in [0]. But you can also output the other index positions. Is there the value you want to recieve? – rank May 17 at 1:47
  • I want to understand why we are using zero value and not 1, 2 or other value – 810311 May 17 at 2:00
  • And this is the reason, why you should look whats inside of it. This way you will find out, why you use the 0 index and if you like to, what the other values are. If it is not an array, you do not need a index number. – rank May 17 at 2:10
  • ok in other words because this array contains only one value which is website URL zero outputs this value, if I put 1 instead of 0 nothing happens. – 810311 May 17 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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