用于自动将自定义字段转换为标签的插件

时间:2012-04-24 作者:user1068544

我有3个自定义字段,我想随时在帖子中自动转换为标签。

自定义字段名称:tag\\u name1、tag\\u name2、tag\\u name3

是否有一个插件或函数,我可以添加来做到这一点?我将其添加到我的函数中,但出现以下错误:

警告:在/home/content/46/8529846/html/wp-content/themes/super-light/functions中为foreach()提供的参数无效。php在线94

警告:无法修改标题信息-标题已由/home/content/46/8529846/html/wp-content/themes/super-light/functions.php:94)在/home/content/46/8529846/html/wp-includes/pluggable中发送。php在线866

<?php
if ( ! isset( $content_width ) )
$content_width = 650;

add_action( \'widgets_init\', \'super_light_sidebars\' );

function super_light_sidebars() {
register_sidebar(array(
    \'name\' => __( \'Sidebar Widget Area\', \'super_light\'),
    \'id\' => \'sidebar-widget-area\',
    \'description\' => __( \'The sidebar widget area\', \'super_light\'),
    \'before_widget\' => \'<div class="widget">\',
    \'after_widget\' => \'</div>\',
    \'before_title\' => \'<h3>\',
    \'after_title\' => \'</h3>\',
));
}

register_nav_menus(
    array(
      \'primary\' => __(\'Header Menu\', \'super_light\'),
      \'secondary\' => __(\'Footer Menu\', \'super_light\')
    )
);

//Multi-level pages menu
function super_light_page_menu() {

if (is_page()) { $highlight = "page_item"; } else {$highlight = "menu-item current-menu-item"; }
echo \'<ul class="menu">\';
wp_list_pages(\'sort_column=menu_order&title_li=&link_before=&link_after=&depth=3\');
echo \'</ul>\';
}

//Single-level pages menu
function super_light_menu_flat() {
    if (is_page()) { $highlight = "page_item"; } else {$highlight = "menu-item current-menu-item"; }
    echo \'<ul class="menu">\';
    wp_list_pages(\'sort_column=menu_order&title_li=&link_before=&link_after=&depth=1\');
    echo \'</ul>\';
}




add_editor_style();
add_theme_support(\'automatic-feed-links\');
add_theme_support(\'post-thumbnails\');

set_post_thumbnail_size( 120, 120, true ); // Default size

// Make theme available for translation
// Translations can be filed in the /languages/ directory
load_theme_textdomain(\'super_light\', get_template_directory() . \'/languages\');

function catch_that_image() {
  global $post, $posts;
  $first_img = \'\';
  ob_start();
  ob_end_clean();
  $output = preg_match_all(\'/<img.+src=[\\\'"]([^\\\'"]+)[\\\'"].*>/i\', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}

add_action(\'save_post\',\'custom_field_add_tags\');

function custom_field_add_tags($post_id) {

 $post = get_post($post_id);

 //get values of custom fields and put into array

 $tag1 = get_post_meta($post_id, \'tag_name1\', true);
 $tag2 = get_post_meta($post_id, \'tag_name2\', true);
 $tag3 = get_post_meta($post_id, \'tag_name3\', true);
 $tag4 = get_post_meta($post_id, \'tag_name4\', true);
 $tag5 = get_post_meta($post_id, \'tag_name5\', true);
 $tag6 = get_post_meta($post_id, \'tag_name6\', true);
 $tag7 = get_post_meta($post_id, \'tag_name7\', true);
 $tag8 = get_post_meta($post_id, \'tag_name8\', true);
 $tag9 = get_post_meta($post_id, \'tag_name9\', true);

 $tags_to_add = array($tag1, $tag2, $tag3, $tag4, $tag5, $tag6, $tag7, $tag8, $tag9);

 //now check if tag does not already exist (if no - add tag from custom field)

 $add_tags = array();

 foreach(get_the_terms($post_id, \'post_tag\') as $term)

    if(!in_array($term->slug, $tags_to_add))
        $add_tags[] = $term->slug;

 if(!empty($add_tags))
    wp_add_post_tags($post_id, implode(\',\', $add_tags));
}

?>

1 个回复
SO网友:Adam

在函数中放置类似的内容。php应该可以工作。我还没有机会测试这段代码,它有点凌乱(可以改进),但应该给你一个很好的起点。。

add_action(\'save_post\',\'custom_field_add_tags\');

function custom_field_add_tags($post_id) {

 $post = get_post($post_id);

 //get values of custom fields and put into array

 $tag1 = get_post_meta($post_id, \'key_1\', true);
 $tag2 = get_post_meta($post_id, \'key_2\', true);
 $tag3 = get_post_meta($post_id, \'key_3\', true);

 $tags_to_add = array($tag1, $tag2, $tag3);

 //now check if tag does not already exist (if no - add tag from custom field)

 $add_tags = array();

 foreach(get_the_terms($post_id, \'post_tag\') as $term) {

    if(!in_array($term->slug, $tags_to_add))
        $add_tags[] = $term->slug;
 }

 if(!empty($add_tags))
    wp_add_post_tags($post_id, implode(\',\', $add_tags));
}

结束

相关推荐

Sorting tags by name

当标签有描述时,我会显示一系列标签。我想按名字而不是按邮寄数量来分类。有没有办法做到这一点?<?php $tags = get_tags(); if ($tags) { foreach ($tags as $tag) { if ($tag->description) { echo \'<dt><a href=\"\' . get_tag_link( $tag->te