2

I'm trying to add a column to the manage screen that displays the Title and url of the post. I got the Title to work but it's not pulling the url. Basically I just want to show the url that's not been cut of like what sometimes happens in the edit screen. Here's my code:

function change_columns( $cols ) {
$cols = array(
'cb'       => '<input type="checkbox" />',
'title'       => __( 'Title'),
'url'      => __( 'URL',      'trans' ),
 );
return $cols;    
}
add_filter( "manage_cf-link_posts_columns", "change_columns" );

function custom_columns( $column, $post_id ) {
global $post;
switch ( $column ) {
case "url":
  $url = get_post_meta( $post_id, 'url', true);
  echo '<a href="' . $url . '">' . $url. '</a>';
  break;
}
}

add_action( "manage_cf-link_posts_custom_column", "custom_columns", 10, 2 );

Any help in the right direction is much appreciated.

Thanks, Phil

4
  • are you sure that 'url' is the name of the field and that it has any content?
    – Bainternet
    Mar 6, 2012 at 11:08
  • 1
    Why not get_permalink( $post_id );?
    – fuxia
    Mar 6, 2012 at 11:19
  • I swore I tried that.. but it was 3:30 am. I just replaced the get_post_meta with the get_permalink and it worked. I think when I tried that I had forgotten to call the global $post variable. Thanks!
    – krazymatty
    Mar 6, 2012 at 20:56
  • Okay, then I move my comment to an answer. :)
    – fuxia
    Mar 6, 2012 at 23:17

1 Answer 1

1

Always use get_permalink( $post_id ) to get the URI of a post. It takes care of changed permalink structures and SSL.

Be aware that the similar named function the_permalink() does not accept a parameter. To get the permalink of a custom post type (or a regular post outside of the loop) you have to use get_permalink().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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