自定义 Post 类型简码在 WordPress.com 安装中不起作用

Custom Post Type Shortcode Not Working on WordPress.com Installation

我是一个网站 designer/developer 为客户开发网站。我最初使用子主题和名为 "Ask an Expert" 的自定义 Post 类型在 WordPress.org 安装上开发该站点。一切正常,但现在我的客户决定改用 WordPress.com 安装。我创建的用于在主页上显示 CPT 的简码 运行 遇到了麻烦。仅供参考,您可以在 https://aboutcaltopo.wpcomstaging.com/ask-an-expert/.

查看 CPT

要查看当前显示的短代码,请参阅 https://aboutcaltopo.wpcomstaging.com/ 上的橙色 "Ask an Expert" 部分。 CPT有3个自定义"featured images"。简码旨在获取 3 张特色图片和最新 post 的摘录。摘录工作正常,但图像没有显示并在检查器面板中返回大量额外信息(请参阅 img class "askandexpert-thumbnails-image" 以查看我正在谈论的所有额外信息).

您可以访问我的开发站点(在 WordPress.org 站点上)以了解我的目标:http://steadyradiancedesign.com/dev-caltopo/

我为 CPT 添加到儿童主题的 functions.php 文件中的代码和简码如下。它是从几个来源拼凑而成的,包括 Whosebug 上的 。我了解 PHP 的基础知识,但我并不擅长,所以非常感谢您的帮助!

/*Custom Post Type for Ask an Expert*/
function create_posttype() {

    register_post_type( 'askanexpert',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Ask an Expert' ),
                'singular_name' => __( 'Ask an Expert' )
            ),
            'public' => true,
            'has_archive' => true,
            'show_ui' => true,
            'show_in_nav_menus' => true,
            'hierarchical' => true,
            'supports' => array(
            'title',
            'editor',
            //'thumbnail',
            'excerpt',
            'revisions'),
            'rewrite' => array('slug' => 'ask-an-expert'),
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

//Multiple featured images for custom post type
//init the meta box
add_action( 'after_setup_theme', 'custom_postimage_setup' );
function custom_postimage_setup(){
    add_action( 'add_meta_boxes', 'custom_postimage_meta_box' );
    add_action( 'save_post', 'custom_postimage_meta_box_save' );
}

function custom_postimage_meta_box(){

    //on which post types should the box appear?
    $post_types = array('askanexpert');
    foreach($post_types as $pt){
        add_meta_box('custom_postimage_meta_box',__( 'Featured Images: Hike, Headshot, Logo', 'yourdomain'),'custom_postimage_meta_box_func',$pt,'side','low');
    }
}

function custom_postimage_meta_box_func($post){

    //an array with all the images (ba meta key). The same array has to be in custom_postimage_meta_box_save($post_id) as well.
    $meta_keys = array('hike_featured_image','headshot_featured_image','logo_featured_image');

    foreach($meta_keys as $meta_key){
        $image_meta_val=get_post_meta( $post->ID, $meta_key, true);
        ?>
        <div class="custom_postimage_wrapper" id="<?php echo $meta_key; ?>_wrapper" style="margin-bottom:20px;">
            <img src="<?php echo ($image_meta_val!=''?wp_get_attachment_image_src( $image_meta_val)[0]:''); ?>" style="width:100%;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" alt="">
            <a class="addimage button" onclick="custom_postimage_add_image('<?php echo $meta_key; ?>');"><?php _e('add image','yourdomain'); ?></a><br>
            <a class="removeimage" style="color:#a00;cursor:pointer;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" onclick="custom_postimage_remove_image('<?php echo $meta_key; ?>');"><?php _e('remove image','yourdomain'); ?></a>
            <input type="hidden" name="<?php echo $meta_key; ?>" id="<?php echo $meta_key; ?>" value="<?php echo $image_meta_val; ?>" />
        </div>
    <?php } ?>
    <script>
    function custom_postimage_add_image(key){

        var $wrapper = jQuery('#'+key+'_wrapper');

        custom_postimage_uploader = wp.media.frames.file_frame = wp.media({
            title: '<?php _e('select image','yourdomain'); ?>',
            button: {
                text: '<?php _e('select image','yourdomain'); ?>'
            },
            multiple: false
        });
        custom_postimage_uploader.on('select', function() {

            var attachment = custom_postimage_uploader.state().get('selection').first().toJSON();
            var img_url = attachment['url'];
            var img_id = attachment['id'];
            $wrapper.find('input#'+key).val(img_id);
            $wrapper.find('img').attr('src',img_url);
            $wrapper.find('img').show();
            $wrapper.find('a.removeimage').show();
        });
        custom_postimage_uploader.on('open', function(){
            var selection = custom_postimage_uploader.state().get('selection');
            var selected = $wrapper.find('input#'+key).val();
            if(selected){
                selection.add(wp.media.attachment(selected));
            }
        });
        custom_postimage_uploader.open();
        return false;
    }

    function custom_postimage_remove_image(key){
        var $wrapper = jQuery('#'+key+'_wrapper');
        $wrapper.find('input#'+key).val('');
        $wrapper.find('img').hide();
        $wrapper.find('a.removeimage').hide();
        return false;
    }
    </script>
    <?php
    wp_nonce_field( 'custom_postimage_meta_box', 'custom_postimage_meta_box_nonce' );
}

function custom_postimage_meta_box_save($post_id){

    if ( ! current_user_can( 'edit_posts', $post_id ) ){ return 'not permitted'; }

    if (isset( $_POST['custom_postimage_meta_box_nonce'] ) && wp_verify_nonce($_POST['custom_postimage_meta_box_nonce'],'custom_postimage_meta_box' )){

        //same array as in custom_postimage_meta_box_func($post)
        $meta_keys = array('hike_featured_image','headshot_featured_image','logo_featured_image');
        foreach($meta_keys as $meta_key){
            if(isset($_POST[$meta_key]) && intval($_POST[$meta_key])!=''){
                update_post_meta( $post_id, $meta_key, intval($_POST[$meta_key]));
            }else{
                update_post_meta( $post_id, $meta_key, '');
            }
        }
    }
}

/*Generate shortcode to display Ask an Expert most recent post*/
add_shortcode( 'askanexpert-recent-post', 'askanexpert_recent_post_shortcode1' );
function askanexpert_recent_post_shortcode1( $atts ) {
    ob_start();
    $query = new WP_Query( array(
        'post_type' => 'askanexpert',
        'posts_per_page' => 1,
        'order' => 'DSC',
        'orderby' => 'date',
    ) );
    if ( $query->have_posts() ) { ?>



        <!--Hike, Headshot, and Logo images-->
        <div class="askanexpert-thumbnails-container">
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <div class="askanexpert-thumbnails-circle">
                <div class="askanexpert-thumbnails-circle-inner">
                    <div class="askandexpert-thumbnails1"> <a href="<?php the_permalink(); ?>"><img class="askandexpert-thumbnails-image" src="<?php echo wp_get_attachment_image(get_post_meta(get_the_ID(), 'headshot_featured_image', true),'thumbnail'); ?>"></a> </div>
                    <div class="askandexpert-thumbnails2"> <a href="<?php the_permalink(); ?>"><img class="askandexpert-thumbnails-image" src=" <?php echo wp_get_attachment_image(get_post_meta(get_the_ID(), 'logo_featured_image', true),'thumbnail'); ?>"></a> </div>
                </div>
            </div>
            <div class="askandexpert-thumbnails3">
                <a href="<?php the_permalink(); ?>"><img class="askandexpert-thumbnails3-inside" src="<?php echo wp_get_attachment_image(get_post_meta(get_the_ID(), 'hike_featured_image', true),'medium-large'); ?></a> 
            </div>
            <?php endwhile;
            wp_reset_postdata(); ?>
        </div>

        <div class="askanexpert-post-listing">
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <h2 id="post-<?php the_ID(); ?>" style="color: #000;" <?php post_class(); ?>>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </h2>
            <p><?php the_excerpt(); ?></p>
            <?php endwhile;
            wp_reset_postdata(); ?>
        </div>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}

我不知道 WordPress.com 网站上破坏代码的不同之处。

提前感谢您提供的任何帮助!

我知道问题出在哪里了,尽管我仍然不明白为什么。万一其他人遇到这个问题...

为了让简码在 WordPress.com 上工作,我需要将 "wp_get_attachment_image(..." 更改为 "wp_get_attachment_url(..." 因此,例如,简码中返回的第一个缩略图更改为:

<div class="askandexpert-thumbnails1"> <a href="<?php the_permalink(); ?>"><img class="askandexpert-thumbnails-image" src="<?php echo wp_get_attachment_url(get_post_meta(get_the_ID(), 'headshot_featured_image', true),'thumbnail'); ?>"></a> </div>

如果有人知道为什么 "wp_get_attachment_image" 适用于 WordPress.org 而不是 WordPress.com,我很想听听!