Custom field value link title

时间:2013-10-14 作者:user26592

我需要帮助自动检测URL的标题。我将使用当前网站博客帖子的链接。

我现在拥有的代码:

<?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/>";
?>
我需要代码自动检测URL的标题,而不是“标题”。有人能帮忙吗?

我的职能。php代码:

        $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

  );

1 个回复
SO网友:Chip Bennett

如果你储存邮件,你的生活可能会更轻松IDs 作为您的自定义元值,而不是发布URLs; 也就是说,你可以试试back-tracing the Post ID using url_to_postid(), 然后从ID获取帖子对象,然后获取帖子标题:

// 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>\';
}
编辑法典条目中规定的url_to_postid(), 对于自定义帖子类型,该函数当前无法正常工作。此行为预计将在WordPress 3.7中修复。

在此期间,最简单的选择是储存邮件IDs 而不是postURLs:

// 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>\';
}

结束

相关推荐