WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] id of new custom post (3 posts)

  1. passimal
    Member
    Posted 3 years ago #

    I want the ID of new custom post.
    Im trying with several code ( between echo "zaza";)
    Can someone help to print the id of this NEW custom post ?

    Thanks

    /************** à la fin du formulaire, ajout des champs définis dans $mon_offre_type_Options ***********/
    
    add_action('edit_form_advanced', 'mon_offre_posttype_form');
    add_action('save_post', 'mon_offre_posttype_save');
    function mon_offre_posttype_form(){
    
    	global $mon_offre_type;
    	global $mon_offre_type_Options;
    
    	if((isset($_GET['post_type'])) and ($_GET['post_type']==$mon_offre_type)) /* formulaire d'ajout */
    	{
    
    		echo "zaza- ";
    		echo $post->ID;
    		echo " -zaza";
    		/* ---------------------------------------------------------------------- */
    					/* Permet d'afficher le numero de L'id */
    					/* Affiche Numero de l'offre lors d'un ajout d une offre      */
    					echo '<div class="meta-box-sortables ui-sortable">';
    						echo '<div class="postbox">';
    							echo '
    							<h3 class="hndle"><span>Num&eacute;ro de l\'offre :</span></h3>
    							<div class="inside">
    								<label class="screen-reader-text"></label>
    							</div>' ;
    							echo '<p>&nbsp;&nbsp;Offre N&deg; '.$id;
    							echo '</p>';
    						echo '</div>';
    					echo '</div>';
    		/* ------------------------------------------------------- */
    
    		echo '<div class="meta-box-sortables ui-sortable">';
    		/* formulaire vide (ou avec les valeurs par défaut) */
    
    		foreach ($mon_offre_type_Options as $o)
    		{
    			echo '<div class="postbox">';
    			echo '<h3 class="hndle"><span>'.$o['name'].'</span></h3><div class="inside"><label class="screen-reader-text" for="'.$o['id'].'">'.$o['name'].'</label>';
    
    			//fonction pour afficher le bon html en fonction du type, définie ci-après
    			echo get_champ($o,$o['std']);			
    
    			if($o['desc']!='')
    				echo '<p>'.$o['desc'].'</p>';
    
    			echo '</div></div>';
    		}
    		echo '</div>';
    
    	}
    	else{
    		if(!isset($_GET['post_type']))
    		{
    			if(isset($_GET['post']))
    			{
    				if(get_post_type($_GET['post'])==$mon_offre_type) /* formulaire de modification */
    				{
    					$id=$_GET['post'];
    
    					/* ------------------------------------------------------- */
    					/* Permet d'afficher le numero de L'id */
    					/* Affiche Numero de l'offre lors d'une modification      */
    					echo '<div class="meta-box-sortables ui-sortable">';
    						echo '<div class="postbox">';
    							echo '
    							<h3 class="hndle"><span>Num&eacute;ro de l\'offre :</span></h3>
    							<div class="inside">
    								<label class="screen-reader-text"></label>
    							</div>' ;
    							echo '<p>&nbsp;&nbsp;Offre N&deg; '.$id;
    							echo '</p>';
    						echo '</div>';
    					echo '</div>';
    					/* ------------------------------------------------------- */
    
    					/* formulaire prérempli avec les valeurs pré-existantes */
    					echo '<div class="meta-box-sortables ui-sortable">';
    					foreach ($mon_offre_type_Options as $o)
    					{
    						echo '<div class="postbox">';
    						echo '<h3 class="hndle"><span>'.$o['name'].'</span></h3><div class="inside"><label class="screen-reader-text" for="'.$o['id'].'">'.$o['name'].'</label>';
    
    						echo get_champ($o,get_post_meta($id, $o['id'], true));			
    
    						if($o['desc']!='')
    							echo '<p>'.$o['desc'].'</p>';
    
    						echo '</div></div>';
    					}
    					echo '</div>';
    				}
    			}
    		}
    	}
    }
    
    function get_champ($o,$val)
    {
    	switch ($o['type'])
    	{
    		case 'textarea':
    			echo '	<textarea rows="5" cols="101" name="'.$o['id'].'" id="'.$o['id'].'">'.$val.'</textarea>';
    			break;
    		case 'text':
    			echo '	<input type="text" style="width:100%;" name="'.$o['id'].'" id="'.$o['id'].'" value="'.$val.'"/>';
    			break;
    		case 'checkbox':
    			echo '<input type="checkbox" name="'.$o['id'].'" id="'.$o['id'].'" value="1" ';
    			if($val==1)
    				echo 'checked="checked';
    			echo '/><label for="'.$o['id'].'">'.$o['name'].'</label>';
    			break;
    		case 'select':
    			echo '	<select name="'.$o['id'].'" style="width:100%;" id="'.$o['id'].'"><option value="">-</option>';
    			foreach($o['options'] as $opt)
    			{
    				echo '<option value="'.$opt.'"';
    				if($opt==$val)
    					echo 'selected="selected"';
    				echo '>'.$opt.'</option>';
    			}
    			echo'</select>';
    			break;
    	}
    
    }
    
    /* ************************************************************************************************* */
  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    global $post ?

    You can't access vars inside a function if they have no scope inside that function, globalise the variable..

    You may also be able to access the post type by globalising the appropriate variable ..

    global $post_type;

    NOTE: Globals can be grouped, so they're one statement, like so..

    global $mon_offre_type, $mon_offre_type_Options, $post, $post_type;

    Hope that helps..

  3. passimal
    Member
    Posted 3 years ago #

    Yes thanks a lot that s right now !!!

    global $post that's all ...

    you can close it ...

Topic Closed

This topic has been closed to new replies.

About this Topic