Php WordPress get_post_meta在gallery meta中不工作
我的metabox有问题: 我在自定义帖子类型“startup”中添加了新的meta。代码如下:Php WordPress get_post_meta在gallery meta中不工作,php,jquery,wordpress,Php,Jquery,Wordpress,我的metabox有问题: 我在自定义帖子类型“startup”中添加了新的meta。代码如下: function product_thumbnail_url($pid){ $image_id = get_post_thumbnail_id($pid); $image_url = wp_get_attachment_image_src($image_id,'screen-shot'); return $image_url[0]; } function list_my_images
function product_thumbnail_url($pid){
$image_id = get_post_thumbnail_id($pid);
$image_url = wp_get_attachment_image_src($image_id,'screen-shot');
return $image_url[0];
}
function list_my_images_slots( $cpt = false ){
$list_images = apply_filters('list_images',array(
'image1' => '_image1',
'image2' => '_image2',
'image3' => '_image3',
'image4' => '_image۴',
'image5' => '_image5',
'image6' => '_image6',
'image7' => '_image7',
'image8' => '_image8',
'image9' => '_image9',
'image10'=> '_image10',
), $cpt );
return $list_images;
}
add_action("admin_init", "add_image_metabox");
function add_image_metabox(){
add_meta_box('elnazimage', __('گالری تصاویر'), "elnazimage", 'startup', 'normal', 'core');
//add_meta_box('elnazimage', __('گالری تصاویر'), "elnazimage", 'opportunities', 'normal', 'core');
}
add_action('save_post', 'save_image_metabox');
function save_image_metabox($post_ID){
// on retourne rien du tout s'il s'agit d'une sauvegarde automatique
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
$list_images = list_my_images_slots();
foreach($list_images as $k => $i){
if ( isset( $_POST[$k] ) ) {
check_admin_referer('image-liee-save_'.$_POST['post_ID'], 'image-liee-nonce');
update_post_meta($post_ID, $i, esc_html($_POST[$k]));
}
}
}
function elnazimage($post){
$list_images = list_my_images_slots();
wp_enqueue_script( 'media-upload' );
wp_enqueue_script( 'thickbox' );
wp_enqueue_script( 'quicktags' );
wp_enqueue_script( 'jquery-ui-resizable' );
wp_enqueue_script( 'jquery-ui-draggable' );
wp_enqueue_script( 'jquery-ui-button' );
wp_enqueue_script( 'jquery-ui-position' );
wp_enqueue_script( 'jquery-ui-dialog' );
wp_enqueue_script( 'wpdialogs' );
wp_enqueue_script( 'wplink' );
wp_enqueue_script( 'wpdialogs-popup' );
wp_enqueue_script( 'wp-fullscreen' );
wp_enqueue_script( 'editor' );
wp_enqueue_script( 'word-count' );
wp_enqueue_script( 'img-mb', get_template_directory_uri() . '/js/get-images.js', array( 'jquery','media-upload','thickbox','set-post-thumbnail' ) );
wp_enqueue_style( 'thickbox' );
wp_nonce_field( 'image-liee-save_'.$post->ID, 'image-liee-nonce');
echo '<div id="droppable">';
$z =1;
foreach($list_images as $k=>$i){
$meta = get_post_meta($post->ID,$i,true);
$img = (isset($meta)) ? '<img id="image-prev'.$z.'" src="'.$meta.'" width="100" height="100" alt="" draggable="false">' : '';
echo '<div class="image-entry" draggable="true">';
echo '<input type="text" name="'.$k.'" id="'.$k.'" class="id_img" value="'.$meta.'">';
echo '<div class="img-preview">'.$img.'</div>';
echo '<a href="javascript:void(0);" class="get-image button-secondary" data-num="'.$z.'">'._x('Add New','file').'</a><a href="javascript:void(0);" class="del-image button-secondary" data-num="'.$z.'">'.__('Delete').'</a>';
echo '</div>';
$z++;
}
echo '</div>';
?>
<div style="clear:left;"></div>
<script>jQuery(document).ready(function($){
function reorderImages(){
//reorder images
$('#droppable .image-entry').each(function(i){
//rewrite attr
var num = i+1;
$(this).find('.get-image').attr('data-num',num);
$(this).find('.del-image').attr('data-num',num);
$(this).find('div.img-preview').attr('data-num',num);
var $input = $(this).find('input');
$input.attr('name','image'+num).attr('id','image'+num).attr('data-num',num);
});
}
if('draggable' in document.createElement('span')) {
function handleDragStart(e) {
this.style.opacity = '0.۴'; // this / e.target is the source node.
}
function handleDragOver(e) {
if (e.preventDefault) {
e.preventDefault(); // Necessary. Allows us to drop.
}
e.dataTransfer.dropEffect = 'move'; // See the section on the DataTransfer object.
return false;
}
function handleDragEnter(e) {
// this / e.target is the current hover target.
this.classList.add('over');
}
function handleDragLeave(e) {
var rect = this.getBoundingClientRect();
// Check the mouseEvent coordinates are outside of the rectangle
if(e.x > rect.left + rect.width || e.x < rect.left
|| e.y > rect.top + rect.height || e.y < rect.top) {
this.classList.remove('over'); // this / e.target is previous target element.
}
}
function handleDrop(e) {
// this / e.target is current target element.
if (e.stopPropagation) {
e.stopPropagation(); // stops the browser from redirecting.
}
// Don't do anything if dropping the same column we're dragging.
if (dragSrcEl != this) {
// Set the source column's HTML to the HTML of the column we dropped on.
dragSrcEl.innerHTML = this.innerHTML;
this.innerHTML = e.dataTransfer.getData('text/html');
reorderImages();
}
// See the section on the DataTransfer object.
return false;
}
function handleDragEnd(e) {
// this/e.target is the source node.
this.style.opacity = '1';
[].forEach.call(cols, function (col) {
col.classList.remove('over');
});
}
var dragSrcEl = null;
function handleDragStart(e) {
// Target (this) element is the source node.
this.style.opacity = '0.4';
dragSrcEl = this;
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', this.innerHTML);
}
var cols = document.querySelectorAll('#droppable .image-entry');
[].forEach.call(cols, function(col) {
col.addEventListener('dragstart', handleDragStart, false);
col.addEventListener('dragenter', handleDragEnter, false);
col.addEventListener('dragover', handleDragOver, false);
col.addEventListener('dragleave', handleDragLeave, false);
col.addEventListener('drop', handleDrop, false);
col.addEventListener('dragend', handleDragEnd, false);
});
}else{
$( "#droppable" ).sortable({
opacity: 0.4,
cursor: 'move',
update: function(event, ui) {
reorderImages()
}
});
}
});</script>
<style type="text/css">
[draggable] {
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
user-select: none;
}
.img-preview{
position:relative;
display:block;
width:100px;
height:100px;
background:#efefef;
border:1px solid #FFF;
}
.img-preview img{
position:absolute;
top:0;
left:0;
}
.image-entry{
float:left;
margin:0 10px 10px 0;
border:2px solid #ccc;
padding:10px;
background:#FFF;
}
.image-entry:last-child{margin-right:0;}
.image-entry.over{
border: 2px dashed #000;
}
.get-image,.del-image{
margin-top:10px !important;
display:block !important;
}
</style>
<?php
}
function get_images_ids($thumbnail = false, $id = false){
global $post;
$the_id = ($id) ? $id : $post->ID;
$list_images = list_my_images_slots( get_post_type( $id ) );
$a = array();
foreach ($list_images as $key => $img) {
if($i = get_post_meta($the_id,$img,true))
$a[$key] = $i;
}
if($thumbnail){
$thumb_id = get_post_thumbnail_id($the_id);
if(!empty($thumb_id)) array_unshift($a, get_post_thumbnail_id($the_id));
}
return $a;
}
function get_images_src($size = 'medium',$thumbnail = false, $id = false){
if($id)
$images = $thumbnail ? get_images_ids(true,$id) : get_images_ids(false,$id);
else
$images = $thumbnail ? get_images_ids(true) : get_images_ids();
foreach($images as $k => $i)
$o= wp_get_attachment_image_src($i, $size);
return $o;
}
function get_multi_images_src($small = 'thumbnail',$large = 'full',$medium = 'medium',$thumbnail = false, $id = false){
if($id)
$images = $thumbnail ? get_images_ids(true,$id) : get_images_ids(false,$id);
else
$images = $thumbnail ? get_images_ids(true) : get_images_ids();
$o = array();
foreach($images as $k => $i) {
$pic = wp_get_attachment_image_src($i,$large);
echo '<div class="col-md-4 gallery"><div class="pgallery-item"><a rel="prettyPhoto[elnaz]" href="'.$pic[0].'">';
echo '<img src="'.$pic[0].'" width="'.$pic[1].'" height="'.$pic[2].'" />';
echo '<span>'.get_the_title($i).'</span>';
echo '</a></div></div>';
}
$list_images = list_my_images_slots();
foreach($list_images as $k => $i){
$pic = wp_get_attachment_image_src($i,$large);
echo '<div class="col-md-4 gallery"><div class="pgallery-item"><a rel="prettyPhoto[elnaz]" href="'.$pic[0].'">';
echo '<img src="'.$pic[0].'" width="'.$pic[1].'" height="'.$pic[2].'" />';
echo '<span>'.get_the_title($i).'</span>';
echo '</a></div></div>';
}
return '';
}
}
函数产品\u缩略图\u url($pid){
$image\u id=get\u post\u缩略图\u id($pid);
$image\u url=wp\u get\u attachment\u image\u src($image\u id,'screen-shot');
返回$image_url[0];
}
功能列表\u我的\u图像\u插槽($cpt=false){
$list_images=应用_过滤器('list_images',数组(
'image1'=>'u image1',
“image2'=>”\u image2',
“image3'=>”\u image3',
'image4'=>'u image۴',
“image5'=>”\u image5',
“image6'=>”\u image6',
“image7'=>”\u image7',
“image8'=>”\u image8',
“image9'=>”\u image9',
“image10'=>”\u image10',
)美元(cpt);
返回$list_图像;
}
添加操作(“管理初始化”、“添加图像”元数据库);
函数add_image_metabox(){
添加元框('elnazimage','elnazimage','startup','normal','core');
//添加元框(“elnazimage”、“opportunities”、“normal”、“core”);
}
添加操作(“保存帖子”、“保存图像”元框);
函数save_image_metabox($post_ID){
//关于索维嘉德自动化的再利用
if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE)
返回$post_id;
$list_images=列出我的图像_插槽();
foreach($k=>i列_图像){
如果(isset($_POST[$k])){
检查“管理”参考(“image-liee-save.$”\u POST['POST\u ID'],“image-liee-nonce”);
更新发布元数据($post\u ID,$i,esc\u html($\u post[$k]);
}
}
}
功能图像($post){
$list_images=列出我的图像_插槽();
wp_排队_脚本(“媒体上传”);
wp_排队_脚本('thickbox');
wp_排队_脚本('quicktags');
wp_排队_脚本('jqueryui可调整大小');
wp_enqueue_脚本('jqueryui draggable');
wp_排队_脚本(“jqueryui按钮”);
wp_排队_脚本('jqueryui位置');
wp_排队_脚本(“jqueryui对话框”);
wp_排队_脚本(“wpdialogs”);
wp_排队_脚本('wplink');
wp_enqueue_脚本('wpdialogs popup');
wp_排队_脚本(“wp全屏”);
wp_排队_脚本(“编辑器”);
wp_排队_脚本(“字数”);
wp_enqueue_脚本('img mb',get_template_directory_uri()。/js/get images.js',数组('jquery','media-upload','thickbox','set-post-缩略图');
wp_排队_样式('thickbox');
wp_nonce_字段('image-liee-save_'.$post->ID,'image-liee nonce');
回声';
$z=1;
foreach($k=>i列_图像){
$meta=get\u post\u meta($post->ID,$i,true);
$img=(isset($meta))?“”:“”;
回声';
回声';
回音“.$img.”;
回声';
回声';
$z++;
}
回声';
?>
jQuery(文档).ready(函数($){
函数reorderImages(){
//重新排列图像
$('#droppable.image entry')。每个(函数(i){
//重写属性
var num=i+1;
$(this.find('.get image').attr('data-num',num);
$(this.find('.del image').attr('data-num',num);
$(this.find('div.img-preview').attr('data-num',num);
var$input=$(this.find('input');
$input.attr('name','image'+num).attr('id','image'+num).attr('data-num',num);
});
}
if('draggable'在document.createElement('span')中){
函数handleDragStart(e){
this.style.opacity='0.۴';//this/e.target是源节点。
}
功能手柄(e){
如果(如默认){
e、 preventDefault();//必要。允许我们删除。
}
e、 dataTransfer.dropEffect='move';//请参阅有关dataTransfer对象的部分。
返回false;
}
功能手柄输入(e){
//此/e.target是当前悬停目标。
this.classList.add('over');
}
功能手柄(e){
var rect=this.getBoundingClientRect();
//检查mouseEvent坐标是否在矩形之外
如果(e.x>rect.left+rect.width | | e.xrect.top+rect.height | | e.y