自定义帖子类型简码无法在WordPress.com安装上使用

问题描述 投票:-1回答:1

我是一名网站设计师/开发人员,正在为客户的网站工作。我最初在WordPress.org安装上使用子主题和名为“询问专家”的自定义帖子类型开发了该网站。一切都很好,但是现在我的客户决定改为使用WordPress.com安装。我创建的要在主页上显示CPT的简码遇到麻烦。仅供参考,您可以在https://aboutcaltopo.wpcomstaging.com/ask-an-expert/看到CPT。

要查看简码当前显示的内容,请参见https://aboutcaltopo.wpcomstaging.com/上橙色的“询问专家”部分。 CPT具有3个自定义“功能图像”。简码旨在获取3张精选图片和最新文章的摘录。摘录效果很好,但是图像没有显示出来,并且在检查器面板中返回了很多额外的信息(请参阅img类“ askandexpert-thumbnails-image”以查看我正在谈论的所有其他信息)。

您可以访问我的开发网站(位于WordPress.org网站上)以查看我要实现的目标:http://steadyradiancedesign.com/dev-caltopo/

我添加到CPT的Child Theme的functions.php文件中的代码,简码如下。它是从Stackoverflow上的this thread等几个来源中整理而来的。我了解PHP的基础知识,但是我并不擅长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网站上有什么不同之处正在破坏代码。

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

php wordpress custom-post-type wordpress.com
1个回答
0
投票

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

为了使短代码在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上无效,我很乐意听到!

© www.soinside.com 2019 - 2024. All rights reserved.