我是一名网页设计师/开发人员,为客户开发网站。我最初在WordPress.org上安装了一个名为"询问专家"的儿童主题和自定义帖子类型来开发这个网站。一切都很好,但现在我的客户决定安装WordPress.com。我遇到了一个问题,我创建了一个在主页上显示CPT的快捷代码。仅供参考,您可以在https://aboutcaltopo.wpcomstaging.com/ask-an-expert/.
要查看快捷代码当前显示的内容,请参阅上的橙色"询问专家"部分https://aboutcaltopo.wpcomstaging.com/.CPT有3个自定义的"特色图像"。短代码是为了获取3张特色图片和最近帖子的摘录。摘录很好,但图像没有显示,并且在检查器面板中返回了很多额外的信息(请参阅img类"askandexpert缩略图图像",查看我所说的所有额外信息(。
你可以访问我的开发网站(在WordPress.org网站上(,看看我的目标是什么:http://steadyradiancedesign.com/dev-caltopo/.
下面是我添加到儿童主题的functions.php文件中的CPT和快捷代码。它是从一些来源拼凑而成的,包括stackoverflow上的这条线索。我了解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 _attachmant_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上无效,我很想听!