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)
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?
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)
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?
Share Improve this question asked May 16, 2020 at 22:00 810311810311 417 bronze badges1 Answer
Reset to default 0You 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/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/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.