尝试使用GET_POST_META编写短码,但不起作用

时间:2014-09-30 作者:tjoenz

我正在尝试编写一个带有CPT的插件,并有一个显示数据的短代码。我可以让“the\\u title()”正常工作并显示帖子类型的标题,但使用“get\\u post\\u meta()”似乎无法正常工作。这是我的代码,非常感谢您的帮助。谢谢

function ClientDirectoriesCPT() {
    add_action( \'init\', array( $this, \'register_custom_post_type\' ) );
    add_action( \'add_meta_boxes\', array( $this, \'register_meta_boxes\' ) );
    add_action( \'save_post\', array( $this, \'save_meta_boxes\' ) );
    add_shortcode( \'client-directory-page\', array( $this, \'build_client_directory_page\' ) );
}


function build_client_directory_page( $atts ) {
    ob_start();
    $query = new WP_Query( array(
        \'post_type\' => \'client\',
        \'posts_per_page\' => 10,
        \'orderby\' => \'meta_value\',
        \'order\' => \'ASC\'
    ) );
    extract(shortcode_atts(array(
        \'id\' => null,
    ), $atts));

    if ( $query->have_posts() ) { ?>
        <?php while ( $query->have_posts() ) : $query->the_post();

        $client_name = get_post_meta($post->ID, \'client_name\', true); ?>

            <div class="row">
                <div class="col-sm-6"><?= the_title(); ?></div>
                <div class="col-sm-6"><?= $client_name ?></div>
            </div>
        <?php endwhile;
        wp_reset_postdata(); ?>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}

/**
* Registers a Meta Box on our Client Directory Custom Post Type, called \'Client Details\'
*/
function register_meta_boxes() {
    add_meta_box( \'client-details\', \'Client Details\', array( $this, \'output_meta_box\' ), \'client\', \'normal\', \'high\' );
}

/**
* Output a Client Details meta box
*
* @param WP_Post $post WordPress Post object
*/
function output_meta_box( $post ) {
    // 2nd with Name, address, website and description

    $client_name = get_post_meta( $post->ID, \'_client_name\', true );
    $client_address = get_post_meta( $post->ID, \'_client_address\', true );
    $client_url = get_post_meta( $post->ID, \'_client_url\', true );
    $client_description = get_post_meta( $post->ID, \'_client_description\', true );

    // Add a nonce field so we can check for it later.
    wp_nonce_field(\'save_client\', \'clients_nonce\' );

    // Output label and field
    echo (\'<p><label for="client_name">\' . __( \'Name\', \'cd-crm\' ) . \'</label>\' );
    echo (\'<input type="text" name="client_name" id="client_name" value="\'.esc_attr( $client_name ).\'" /></p>\' );

    echo (\'<p><label for="client_address">\' . __( \'Address\', \'cd-crm\' ) . \'</label>\' );
    echo (\'<input type="text" name="client_address" id="client_address" value="\'.esc_attr( $client_address ).\'" /></p>\' );

    echo (\'<p><label for="client_url">\' . __( \'URL\', \'cd-crm\' ) . \'</label>\' );
    echo (\'<input type="text" name="client_url" id="client_url" value="\'.esc_attr( $client_url ).\'" /></p>\' );

    echo (\'<p><label for="client_description">\' . __( \'Description\', \'cd-crm\' ) . \'</label>\' );
    echo (\'<textarea rows="5" cols="30" id="client_description" name="client_description">\' . esc_html( $client_description ) . \'</textarea>\' );
}

/**
* Saves the meta box field data
*
* @param int $post_id Post ID
*/
function save_meta_boxes( $post_id ) {

    // Check if our nonce is set.
    if ( ! isset( $_POST[\'clients_nonce\'] ) ) {
        return $post_id;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[\'clients_nonce\'], \'save_client\' ) ) {
        return $post_id;
    }

    // Check this is the Contact Custom Post Type
    if ( \'client\' != $_POST[\'post_type\'] ) {
        return $post_id;
    }

    // Check the logged in user has permission to edit this post
    if ( ! current_user_can( \'edit_post\', $post_id ) ) {
        return $post_id;
    }

    // OK to save meta data
    $client_name = sanitize_text_field( $_POST[\'client_name\'] );
    update_post_meta( $post_id, \'_client_name\', $client_name );

    $client_address = sanitize_text_field( $_POST[\'client_address\'] );
    update_post_meta( $post_id, \'_client_address\', $client_address );

    $client_url = sanitize_text_field( $_POST[\'client_url\'] );
    update_post_meta( $post_id, \'_client_url\', $client_url );

    // $client_description = sanitize_text_field( $_POST[\'client_description\'] );
    // update_post_meta( $post_id, \'_client_description\', $client_description );

    // Save the textarea
    if ( isset( $_POST[\'client_description\'] ) ) {

        // WP\'s default allowed tags
        global $allowedtags;

        // allow iframe only in this instance
        $iframe = array( \'iframe\' => array(
                            \'src\' => array (),
                            \'width\' => array (),
                            \'height\' => array (),
                            \'frameborder\' => array(),
                            \'allowFullScreen\' => array() // add any other attributes you wish to allow
                             ) );

        $allowed_html = array_merge( $allowedtags, $iframe );

        // Sanitize user input.
        $my_data = wp_kses( $_POST[\'client_description\'], $allowed_html );

        //$my_data = $_POST[\'client_description\'];

        // Update the meta field in the database.
        update_post_meta( $post_id, \'_client_description\', $my_data );
    }

}\'

1 个回复
最合适的回答,由SO网友:Nicolai Grossherr 整理而成

您的代码有几个小问题:

首先,您使用的是速记PHP标记,如<?= /*some stuff*/ ?> 他们不会工作的-看WordPress Coding Standards - No Shorthand PHP Tags.

其次,你不是echo\'正在初始化变量$client_name.

最后但并非最不重要的一点是,您正在使用名称保存您的帖子元_client_name, 前面有一个额外的下划线,但在尝试获取时输入了错误,下划线丢失。

结束

相关推荐

Return vs Echo Shortcode

好的,我创建了一个简短的代码,其中echo是一些输出;我发现的问题是,必须返回值而不是echo\'d。我在转换快捷码以返回日期时遇到问题。。我试过了,但它打破了我的短码。。如果此短代码正确返回代码而不是回显代码,那么它会是什么样子?// [service slug=\"foobar\"] function services_shortcode( $atts ) { // Attributes extract( shortcode_atts(