0

在我的 WordPress 网站上,我有一个自定义字段,我想将我的帖子摘录添加到自定义字段值中。

我的代码:

function mk_set_default_custom_fields($post_id)
{
if ( $_GET['post_type'] != 'page' ) {
    add_post_meta($post_id, 'key', 'value');

}

return true;
}

点击发布按钮后,如何将帖子的摘录放入add_post_meta值中?

4

4 回答 4

0

像这样将数组放在您想要的自定义帖子中functions.php

$args = array(
      'supports' => array('title','editor','author','excerpt') // by writing these lines an custom field  has been added to CMS
  );

用于在前端检索

echo $post->post_excerpt; // this will return you the excerpt of the current post
于 2013-06-26T06:40:37.313 回答
0

您可以使用$postObject 并在它的帮助下您可以设置value

$post->post_excerpt

仅用于其他可用选项的信息

$post->post_author
$post->post_date
$post->post_date_gmt
$post->post_content
$post->post_content_filtered
$post->post_title
$post->post_excerpt
$post->post_status
$post->post_type
$post->comment_status
$post->ping_status
$post->post_password
$post->post_name
$post->to_ping
$post->pinged
$post->post_modified
$post->post_modified_gmt
$post->post_parent
$post->menu_order
$post->guid
于 2013-06-26T06:29:22.017 回答
0

我不确定您为什么要这样做,因为这会导致您的内容重复。但是,看起来您的函数已连接到save_post. 如果是这种情况,您可以从$_POST['post_except']变量中获取异常。只是不要假设该变量将始终设置,因为save_post在几种不同的情况下会调用该变量。

于 2013-06-26T06:51:17.237 回答
0

将此添加到您的functions.php

add_action( 'save_post', 'my_custom_field_save' );
function my_custom_field_save( $post_id )
{
    if ( $_POST['post_type'] == 'post' ) {
        add_post_meta($post_id, 'custom_excerpt_field', get_the_excerpt($post_id), true);
    }
}

每次添加/更新帖子时,这将save/update是自定义字段 ( )。custom_excerpt_field在前端,要获取自定义字段,请使用(在循环内时)

$custom_excerpt_field_data = get_post_meta(get_the_ID(), 'custom_excerpt_field', true);

使用这个(在循环之外时)

global $post;
$custom_excerpt_field_data = get_post_meta($post->ID, 'custom_excerpt_field', true);
于 2013-06-26T06:56:20.853 回答