Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need help with auto detecting the URL's title. The links I am going to use a link to the current website's blog posts.

The code I have right now:

<?php
  $custom_fields = get_post_custom($post_id); //Current post id
  $my_custom_field = $custom_fields['Featured-Blog']; //key name
  foreach ( $my_custom_field as $key => $url )
  echo $key."<a href='" .$url. "'>Title</a><br /><br /><br/>";
?>

Instead of "Title" I need the code auto detect the URL's title. Can someone help?

My functions.php code:

        $args = array(

    'labels' =>$labels,

    'public' =>true,

    'publicly_queryable' =>true,

    'show_ui' =>true,

    'query_var' =>true,

    'menu_icon' =>get_bloginfo('template_url').'/images/serviceicon.png',

    'rewrite' =>array('slug' => 'Featured-Blog'),

    'capability_type' =>'post',

    'hierarchical' =>true,

    'menu_position' =>'',

    'supports' =>array('title','editor','thumbnail','custom-fields','excerpt' ),

    'has_archive' =>true

  );
share|improve this question
    
Where does the URL's title come from? –  Chip Bennett Oct 14 '13 at 19:24
    
From the blog post. I am using this custom field for related articles. –  user26592 Oct 14 '13 at 19:24
    
And what data are you storing as the custom field? A post ID? A post permalink? Something else? –  Chip Bennett Oct 14 '13 at 19:26
    
in the value field I add a URL of the blog post I want to use –  user26592 Oct 14 '13 at 19:28
    
Your functions.php code appears to be incomplete, but based on what you've posted, you're registering a custom post type? If so, then until WordPress 3.7 is released, url_to_postid() won't work for you. –  Chip Bennett Oct 14 '13 at 19:45
show 2 more comments

1 Answer

You would probably make life much easier for yourself if you stored Post IDs as your custom meta value, rather than post URLs; that said, you can try back-tracing the Post ID using url_to_postid(), then get the post object from the ID, then get the post title:

// Your original code here
$custom_fields = get_post_custom($post_id); //Current post id
$my_custom_field = $custom_fields['Featured-Blog']; //key name

// Your original foreach loop
foreach ( $my_custom_field as $key => $url ) {

    // Fetch the Post ID from the url
    $postid = url_to_postid( $url );

    // Get the post object
    $post_obj = get_post( $postid );

    // Get the post title
    $post_title = $post_obj->post_title;

    // Then construct the link:
    $link = '<a href="' . $url . '">' . $post_title . '</a>';
}

Edit

As stated in the Codex entry for url_to_postid(), the function does not currently work properly for custom post types. This behavior is expected to be fixed in WordPress 3.7.

The easiest alternative in the meantime would be to store post IDs instead of post URLs:

// Your original code here
$custom_fields = get_post_custom($post_id); //Current post id
$my_custom_field = $custom_fields['Featured-Blog']; //key name

// Your original foreach loop
foreach ( $my_custom_field as $key => $id ) {

    // Get the post object
    $post_obj = get_post( $id );

    // Get the post title
    $post_title = $post_obj->post_title;

    // Get the post permalink
    $post_permalink = get_permalink( $id );

    // Then construct the link:
    $link = '<a href="' . $post_permalink . '">' . $post_title . '</a>';
}
share|improve this answer
    
I replaced the code you suggested and added a posts ID instead of the URL. Nothing is showing up. Do I need to change anything inside function.php ? –  user26592 Oct 14 '13 at 19:38
    
The answer I gave assumes that you're storing the post URL, as in your original question. Without seeing your relevant functions.php code, there's no way to know if you need to change anything or not. –  Chip Bennett Oct 14 '13 at 19:40
    
Please add all code to your original question. –  Chip Bennett Oct 14 '13 at 19:42
    
just did. I look forward to your answer! –  user26592 Oct 14 '13 at 19:44
    
See edited answer, that uses stored post ID rather than post URL. –  Chip Bennett Oct 14 '13 at 20:19
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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