如何回调自定义字段文本

时间:2013-05-31 作者:user32447

我有一个插件,用户可以点击一个按钮来发布帖子。

我添加了一个文本字段,但我希望该文本字段回调其中发布的任何内容

下面是它现在的样子

http://ratedrnb.com/wp-content/uploads/2013/05/ffm2.jpg

现在插件可以抓取帖子的缩略图,但我也希望它可以抓取“艺术家/歌曲”字段中的任何内容。

以下是“特色音乐选项”的代码

<?php
/*
Plugin Name: Featured Music
Plugin URI: LittleLadyEnterprises.com
Description: This Plugin is used to show your featured Posts/Pages with thumbnails in a nice slider.
Version: 2.6
Author: Little Lady Enterprises
*/

  $m_slider_options_page = get_option(\'siteurl\') . \'/wp-admin/admin.php?page=rated-featured-music/options.php\';

 function m_slider_options_page() {

add_options_page(\'Featured Music Options\', \'Featured Music\', 10, \'rated-featured-music/options.php\');

 }

 add_action(\'admin_menu\', \'m_slider_options_page\');

 function m_slider_add_scripts() {

    if ( !is_admin() ) {

            wp_register_script(\'jquery.cycle\', get_bloginfo(\'url\') . \'/wp-content/plugins/rated-featured-music/scripts/jquery.cycle.all.2.72.js\', array(\'jquery\'), \'1.3\' );
            wp_enqueue_script(\'jquery.cycle\');

     }

 }

 add_action(\'wp_enqueue_scripts\', \'m_slider_add_scripts\');


 function m_slider_cut_text($text, $chars, $points = "...") {

$content = $text;

$content = preg_replace(\'/\\[.+\\]/\',\'\', $content);
$content = apply_filters(\'the_content\', $content); 
$content = str_replace(\']]>\', \']]&gt;\', $content);
$content = strip_tags($content);

$length = strlen($content);

if($length <= $chars) {

    return $content;

 } else {

    return mb_substr($content, 0, $chars)." ".$points;

}
}

add_action("admin_init", "m_slider_init");
add_action(\'save_post\', \'m_slider_save\');

function m_slider_init() {

    add_meta_box("feat_music", "Featured Music Options", "m_slider_meta", "post", "normal", "high");
    add_meta_box("feat_music", "Featured Music Options", "m_slider_meta", "page", "normal", "high");

}

function m_slider_meta() {

    global $post;
    $custom = get_post_custom($post->ID);
    $feat_music = $custom["feat_music"][0];

?>

        <div class="inside">
    <table class="form-table">
        <tr>
            <th><label for="feat_music">Feature in Featured Music?    </label></th>
            <td><input type="checkbox" name="feat_music" value="1" <?php if($feat_music == 1) { echo "checked=\'checked\'";} ?></td>

<th><label for="points">Artist/Song</label></th>
                <td><input type="text" name="artist_title" value="Artist - Song"></td>
        </tr>
    </table>
</div>

<?php

}

 function m_slider_save() {

    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) 
    return $post_id;
    global $post;

    if($post->post_type == "post" || $post->post_type == "page") {

            update_post_meta($post->ID, "feat_music", $_POST["feat_music"]);

    }

}

function m_slider_insert($atts, $content = null) {

    include (ABSPATH . \'/wp-content/plugins/rated-featured-music/content-slider.php\');

}

add_shortcode("featslider", "m_slider_insert");

$m_slider_img_width = get_option(\'img_width\');

if(empty($m_slider_img_width)) {

    $m_slider_img_width = 320;

}

$m_slider_img_height = get_option(\'img_height\');

if(empty($m_slider_img_height)) {

    $m_slider_img_height = 200;

}

if (function_exists(\'add_image_size\')) {

add_image_size( \'feat_music\', $m_slider_img_width, $m_slider_img_height, true );

}

function m_slider_get_thumb($position) {

$thumb = get_the_post_thumbnail($post_id, $position);
$thumb = explode("\\"", $thumb);
return $thumb[5];

}

//Check for Post Thumbnail Support

add_theme_support( \'post-thumbnails\' );

function m_slider_get_dynamic_class() {

    $class = explode("http://", get_bloginfo("url"));
$class = explode(".", $class[1]);
    $class = $class[0];
    return $class . "_slider";

}

?>

What is the callback code that I need to add to the Value Area?

The code below is the styling of the plugin and callback codes, what is the callback code to call back whatever is placed in the textarea.

<?php

$m_slider_direct_path =  get_bloginfo(\'wpurl\')."/wp-content/plugins/rated-featured-music";

$m_slider_class = m_slider_get_dynamic_class();

?>

<div id="featuredmusicbox">
<?php

    $m_slider_sort = get_option(\'sort\'); if(empty($c_slider_sort))    {$c_slider_sort = "post_date";}
    $m_slider_order = get_option(\'order\'); if(empty($c_slider_order)){$c_slider_order = "DESC";}
    $m_slider_post_limit = get_option(\'limit_posts\');     if(empty($m_slider_limit_posts)){$m_slider_limit_posts = "-1";}

    global $wpdb;

    global $post;

    $args = array( \'meta_key\' => \'feat_music\', \'meta_value\'=> \'1\', \'suppress_filters\' => 0, \'post_type\' => array(\'post\', \'page\'), \'orderby\' => $m_slider_sort, \'order\' => $m_slider_order, \'numberposts\'=> $m_slider_post_limit);

    $myposts = get_posts( $args );

    foreach( $myposts as $post ) :  setup_postdata($post);

        $m_slider_custom = get_post_custom($post->ID);

        $m_slider_thumb = m_slider_get_thumb("featmusic");

    ?>


            <div class="image"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(\'featmusic\'); ?></a></div>
 <div id="titlelisten">
 <div class="titlefm"><center><a href="<?php the_permalink();?>">TEXT AREA TEXT HERE</a></center></div> <div class="listenfm"><a href="<?php the_permalink();?>"><img src="http://ratedrnb.com/wp-content/uploads/2013/05/listen.jpg"></a></div>
</div>
</div>
<?php endforeach; ?>    

1 个回复
SO网友:Milo

您的文本已在数组中$m_slider_custom, 从此行开始:

$m_slider_custom = get_post_custom($post->ID);
如果您只需要特定的密钥feat_music 您可以使用get_post_meta():

echo get_post_meta( $post->ID, \'feat_music\', true );
另一件与此无关的事情——我建议花点时间阅读Codex,清理你的URL和路径。例如,这:

get_option(\'siteurl\') . \'/wp-admin/admin.php?page=rated-featured-music/options.php\';
或者这个:

get_bloginfo(\'url\') . \'/wp-content/plugins/rated-featured-music/scripts/jquery.cycle.all.2.72.js\'
或者这个:

include (ABSPATH . \'/wp-content/plugins/rated-featured-music/content-slider.php\');
。。在某些情况下会严重失败。WordPress内置了获取所需路径或URL的功能,请参阅:

http://codex.wordpress.org/Function_Reference/admin_urlhttp://codex.wordpress.org/Function_Reference/plugins_urlhttp://codex.wordpress.org/Function_Reference/plugin_dir_path

结束