PHP 自定义帖子类型和分类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 自定义帖子类型和分类相关的知识,希望对你有一定的参考价值。

// REALISATIONS
add_action('init', 'ajoute_une_realisation');
function ajoute_une_realisation(){
	$labels = array(
		'name' => _x('Ajouter une réalisation', 'taxonomy general name'),
		'singular_name' => _x('Ajouter une réalisation', 'taxonomy singular name'),
		'add_new' => _x('Ajouter une réalisation', 'realisation'),
		'add_new_item' => __('Ajouter une réalisation'),
		'edit_item' => __('Modifier une réalisation'),
		'new_item' => __('Ajouter une réalisation'),
		'view_item' => __('Voir une réalisation'),
		'search_items' => __('Rechercher une réalisation'),
		'not_found' =>  __('Aucune réalisation trouvée'),
		'not_found_in_trash' => __('Aucune réalisation trouvée'), 
		'parent_item_colon' => '',
		'menu_name' => 'Réalisations'
	);
	$args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true, 
		'show_in_menu' => true, 
		'menu_position' => 9,
		'query_var' => true,
		//'rewrite' => true,
		'rewrite' => array( 'slug' => 'realisations-et-clients' ),
		'capability_type' => 'page',
		'has_archive' => false, 
		'hierarchical' => false,
		//'taxonomies' => array('category'),
		'supports' => array('title','editor')
	); 
	register_post_type('realisations',$args);
	
}

add_filter("manage_edit-realisations_columns", "realisations_edit_columns");
add_action("manage_posts_custom_column",  "realisations_custom_columns");
add_filter("manage_edit-realisations_sortable_columns", "realisations_register_sortable" );

function realisations_edit_columns($columns){
		$columns = array(
			"cb" => "<input type=\"checkbox\" />",
			"activer_projet_sur_site" => "Actif sur le site",
			"est_un_projet_vedette" => "Projet vedette",
			"est_un_projet_branding" => "Projets Branding",
			"title" => "Titre du projet",
			"icl_translations" => $columns['icl_translations'],
			"date" => "Date"
		);

		return $columns;
}

function realisations_custom_columns($column){
		global $post;
		switch ($column)
		{
			case "activer_projet_sur_site":
				$custom = get_post_custom();
				if(get_field('acf_activer_le_projet_sur_le_site'))
				{
					echo 'Oui';
				}
				else
				{
					echo 'Non';
				}
				break;		
				
			case "est_un_projet_vedette":
				$custom = get_post_custom();
				if(get_field('acf_real_projet_en_vedette'))
				{
					echo 'Oui';
				}
				else
				{
					echo 'Non';
				}
				break;		
			case "est_un_projet_branding":
				$custom = get_post_custom();
				if(get_field('acf_est_un_projet_branding'))
				{
					echo 'Oui';
				}
				else
				{
					echo 'Non';
				}
				break;										
		}
}



function realisations_register_sortable($columns)
{
	$columns['activer_projet_sur_site'] = 'activer_projet_sur_site';
	$columns['est_un_projet_vedette'] = 'est_un_projet_vedette';
	$columns['est_un_projet_branding'] = 'est_un_projet_branding';
	return $columns;
}



//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_realisations_taxonomies', 0 );

//create two taxonomies, genres and writers for the post type "book"
function create_realisations_taxonomies() 
{
  // Add new taxonomy
  $labels = array(
    'name' => _x( 'Type de réalisations', 'taxonomy general name' ),
    'singular_name' => _x( 'Type de réalisation', 'taxonomy singular name' ),
    'search_items' =>  __( 'Rechercher un type' ),
    'all_items' => __( 'Tous les types' ),
    'parent_item' => __( 'Parent Types' ),
    'parent_item_colon' => __( 'Parent Types:' ),
    'edit_item' => __( 'Modifier un type' ), 
    'update_item' => __( 'Mise à jour du type' ),
    'add_new_item' => __( 'Ajouter un type' ),
    'new_item_name' => __( 'Nouveau nom du type' ),
    'menu_name' => __( 'Type de réalisations' ),
  ); 	

  register_taxonomy('type_realisation',array('realisations'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true
  ));

}

/*
 * Example code showing how to hook WordPress to add fields to the taxonomny term edit screen.
 * 
 * This example is meant to show how, not to be a drop in example.
 *
 * This example was written in response to this question:
 *
 *    http://lists.automattic.com/pipermail/wp-hackers/2010-August/033671.html
 *
 * By:
 *
 *    Mike Schinkel (http://mikeschinkel.com/custom-wordpress-plugins/)
 *
 * NOTE:
 *
 *    This could easily become a plugin if it were fleshed out.
 *    A class with static methods was used to minimize the variables & functions added to the global namespace.
 *    wp_options was uses with one option be tax/term instead of via a serialize array because it aids in retrival
 *    if there get to be a large number of tax/terms types. A taxonomy/term meta would be the prefered but WordPress
 *    does not have one.
 *
 * This example is licensed GPLv2.
 *
 */
/*

// These are helper functions you can use elsewhere to access this info
function get_taxonomy_term_type($taxonomy,$term_id) {
  return get_option("_term_type_{$taxonomy}_{$term->term_id}");
}
function update_taxonomy_term_type($taxonomy,$term_id,$value) {
  update_option("_term_type_{$taxonomy}_{$term_id}",$value);
}

//This initializes the class.
TaxonomyTermTypes::on_load();

//This should be called in your own code. This example uses two taxonomies: 'region' & 'opportunity'
TaxonomyTermTypes::register_taxonomy(array('type_realisation'));

class TaxonomyTermTypes {
  //This initializes the hooks to allow saving of the
  static function on_load() {
    add_action('created_term',array(__CLASS__,'term_type_update'),10,3);
    add_action('edit_term',array(__CLASS__,'term_type_update'),10,3);
  }
  //This initializes the hooks to allow adding the dropdown to the form fields
  static function register_taxonomy($taxonomy) {
    if (!is_array($taxonomy))
      $taxonomy = array($taxonomy);
    foreach($taxonomy as $tax_name) {
      add_action("{$tax_name}_add_form_fields",array(__CLASS__,"add_form_fields"));
      add_action("{$tax_name}_edit_form_fields",array(__CLASS__,"edit_form_fields"),10,2);
    }
  }
  // This displays the selections. Edit it to retrieve
  static function add_form_fields($taxonomy) {
    echo "Services associés " . self::get_select_html('strategie');
  }
  // This displays the selections. Edit it to retrieve your own terms however you retrieve them.
  static function get_select_html($selected) {
    $selected_attr = array('strategie'=>'','developpement-web'=>'');
    $selected_attr[$selected] = ' selected="selected"';
    $html =<<<HTML
<select id="tag-type" name="tag-type">
  <option value="strategie"{$selected_attr['strategie']}>Stratégie</option>
  <option value="developpement-web"{$selected_attr['developpement-web']}>Développement Web</option>
</select>
HTML;
    return $html;
  }
    // This a table row with the drop down for an edit screen
    static function edit_form_fields($term, $taxonomy) {
    $selected = get_option("_term_type_{$taxonomy}_{$term->term_id}");
    $select = self::get_select_html($selected);
    $html =<<<HTML
<tr class="form-field form-required">
  <th scope="row" valign="top"><label for="tag-type">Services associés </label></th>
  <td>$select</td>
</tr>
HTML;
    echo $html;
  }
  // These hooks are called after adding and editing to save $_POST['tag-term']
  static function term_type_update($term_id, $tt_id, $taxonomy) {
	$varService = $_POST['tag-type'];
	if (isset($varService)) {
      update_taxonomy_term_type($taxonomy,$term_id,$varService);
	  
    }
  }
}
*/
add_filter("manage_edit-type_realisation_columns", 'type_realisation_columns');   
//add_action("manage_posts_custom_column",  "type_realisation_custom_columns");

   function type_realisation_columns($type_realisation_columns) {
    $new_columns = array(
        'cb' => '<input type="checkbox" />',
        'name' => __('Name'), 
        'description' => "Services associés", /* Solution broche � foin en attendant de trouver autre chose */ 
        'slug' => __('Slug'),
        'posts' => __('Posts')
        );
    return $new_columns;
   }

/*
function type_realisation_custom_columns($new_columns){
		global $post;
		switch ($new_columns) 
		{
			  case "services_associes":
   			  //$new_columns = get_post_custom();
			  echo("test");
			  break;
		  }
  
}
*/
/*
add_filter("manage_edit-type_realisation_columns", "type_realisation_edit_columns");
add_action("manage_posts_custom_column",  "type_realisation_custom_columns");

function type_realisation_edit_columns($columns){
		$columns = array(
			"cb" => "<input type=\"checkbox\" />",
			'name' => __('Name'),
			'slug' => __('Slug'),
        	'posts' => __('Posts'),
			"services_associes" => "Services associés"
		);

		return $columns;
}

function type_realisation_custom_columns($column){
		global $Post;
		
		switch ($column) {
			case "services_associes":
   			  $custom = get_post_custom();
			  echo "test";
			  break;
		  }
  
}

*/

以上是关于PHP 自定义帖子类型和分类的主要内容,如果未能解决你的问题,请参考以下文章

PHP 自定义帖子类型和分类

PHP 带有自定义帖子类型和自定义分类的WordPress图库页面

php 在自定义帖子类型中显示所有自定义分类中的所有帖子。

php 具有自定义分类类别的自定义帖子类型

php 创建具有自定义分类的自定义帖子类型

php 在自定义帖子类型存档和一些自定义分类存档上强制命令到`menu_order`