WordPress admin post list modifizieren: Unterschied zwischen den Versionen

Aus wiki.sehanka.de
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „ == Spalte der Listenansicht des Custom Post Type filterbar machen == <pre> </pre> = Allgemein = * https://www.smashingmagazine.com/2013/12/modifying-admin…“)
 
 
(8 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
== Spalte der Listenansicht des Custom Post Type filterbar machen ==
<pre>
</pre>


= Allgemein =
= Allgemein =
Zeile 18: Zeile 12:
# Custom Column(s) anzeigen
# Custom Column(s) anzeigen


'''Beispielcode um Custom Column(s) zu registrieren'''
=== Beispielcode um Custom Column(s) zu registrieren ===


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
Zeile 44: Zeile 38:
</syntaxhighlight>
</syntaxhighlight>


'''Beispielcode um Custom Column(s) anzuzeigen'''
=== Beispielcode um Custom Column(s) anzuzeigen ===


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
Zeile 54: Zeile 48:
   switch ( $column ) {
   switch ( $column ) {


     // display a thumbnail photo
     // Display a thumbnail photo
     case 'photo' :
     case 'photo' :
       echo get_the_post_thumbnail( $post_id, 'thumbnail' );
       echo get_the_post_thumbnail( $post_id, 'thumbnail' );
       break;
       break;


     // display a list of the custom taxonomy terms assigned to the post  
     // Display a list of the custom taxonomy terms assigned to the post  
     case 'custom_taxonomy' :
     case 'custom_taxonomy' :
       $terms = get_the_term_list( $post_id , 'my_custom_taxonomy' , '' , ', ' , '' );
       $terms = get_the_term_list( $post_id , 'my_custom_taxonomy' , '' , ', ' , '' );
Zeile 65: Zeile 59:
       break;
       break;


     // display the value of an ACF (Advanced Custom Fields) field
     // Display the value of an ACF (Advanced Custom Fields) field
     case 'acf_field' :
     case 'acf_field' :
       echo get_field( 'my_acf_field', $post_id );   
       echo get_field( 'my_acf_field', $post_id );   
       break;
       break;


     // display the value of an ACF (Advanced Custom Fields) array field
     // Display the value of an ACF (Advanced Custom Fields) array field
     case 'acf_field' :
     case 'acf_field' :
       $var = get_field( 'my_acf_field', $post_id );
       $var = get_field( 'my_acf_field', $post_id );
       echo implode(", ", $var);
       echo implode(", ", $var);
      break;
    // Display the value of an related ACF (Advanced Custom Fields) field of an related Post Object
    case 'location-title' :
      echo get_the_title(get_field( 'contact-location', $post_id));
      break;
    // Display the value of an related ACF (Advanced Custom Fields) field of an ACF field
    case 'location-country' :
      echo get_field('location-country',get_field( 'contact-location', $post_id));
       break;
       break;
   }
   }
Zeile 79: Zeile 83:


</syntaxhighlight>
</syntaxhighlight>
siehe auch: https://developer.wordpress.org/reference/hooks/manage_posts_custom_column/


== Spalte der Listenansicht des Custom Post Type sortierbar machen ==
== Spalte der Listenansicht des Custom Post Type sortierbar machen ==
Zeile 84: Zeile 90:
'''Beispielcode um Custom Column(s) sortierbar zu machen'''
'''Beispielcode um Custom Column(s) sortierbar zu machen'''


<pre>
<syntaxhighlight lang="php">
 
add_filter( 'manage_edit-mycpt_sortable_columns', 'set_custom_mycpt_sortable_columns' );
add_filter( 'manage_edit-mycpt_sortable_columns', 'set_custom_mycpt_sortable_columns' );


Zeile 93: Zeile 100:
   return $columns;
   return $columns;
}
}
</pre>
 
</syntaxhighlight>


https://www.ractoon.com/wordpress-sortable-admin-columns-for-custom-posts/
https://www.ractoon.com/wordpress-sortable-admin-columns-for-custom-posts/
== Spalte der Listenansicht des Custom Post Type alphabetisch sortieren ==
<syntaxhighlight lang="php">
// Order custom columns in WordPress Backend CPT Listing alphabetically
add_filter( 'manage_edit-reference_sortable_columns', 'reference_sortable_columns' );
function reference_sortable_columns( $columns ) {
    $columns['reference_customer_firma'] = 'reference_customer_firma';
    return $columns;
}
add_action( 'pre_get_posts', 'reference_firma_orderby' );
function reference_firma_orderby( $query ) {
    if( ! is_admin() )
        return;
    $orderby = $query->get( 'orderby');
    if( 'reference_customer_firma' == $orderby ) {
        $query->set('meta_key','reference_customer_firma');
        $query->set('orderby','meta_value');
        // "meta_value_num" is used for numeric sorting
        // "meta_value"    is used for Alphabetically sort.
    }
}
</syntaxhighlight>
https://gist.github.com/vishalkakadiya/8c997a5e8789147e012b12cc4af90389


== Spalte der Listenansicht des Custom Post Type filterbar machen ==
== Spalte der Listenansicht des Custom Post Type filterbar machen ==

Aktuelle Version vom 2. Mai 2022, 17:17 Uhr

Allgemein[Bearbeiten]

Spalte der Listenansicht des Custom Post Type hinzufügen[Bearbeiten]

https://www.ractoon.com/wordpress-sortable-admin-columns-for-custom-posts/

Um die Custom Fields eines Custom Post Type anzuzeigen sind zwei Schritte notwendig:

  1. Custom Column(s) registrieren
  2. Custom Column(s) anzeigen

Beispielcode um Custom Column(s) zu registrieren[Bearbeiten]

// Register custom columns to WordPress Backend CPT Listing
add_filter( 'manage_mycpt_posts_columns', 'set_custom_edit_mycpt_columns' );

function set_custom_edit_mycpt_columns( $columns ) {

  // unset to order it last
  unset($columns['date']);

  // register custom columns
  $columns['photo'] = __( 'Photo', 'my-text-domain' );
  $columns['custom_taxonomy'] = __( 'Custom Taxonomy', 'my-text-domain' );
  $columns['acf_field'] = __( 'ACF Field', 'my-text-domain' );
  // end register custom columns

  // ready to order it last
  $columns['date'] = $date;

  return $columns;
}

Beispielcode um Custom Column(s) anzuzeigen[Bearbeiten]

// Display custom columns in WordPress Backend CPT Listing
add_action( 'manage_mycpt_posts_custom_column' , 'custom_mycpt_column', 10, 2 );

function custom_mycpt_column( $column, $post_id ) {
  switch ( $column ) {

    // Display a thumbnail photo
    case 'photo' :
      echo get_the_post_thumbnail( $post_id, 'thumbnail' );
      break;

    // Display a list of the custom taxonomy terms assigned to the post 
    case 'custom_taxonomy' :
      $terms = get_the_term_list( $post_id , 'my_custom_taxonomy' , '' , ', ' , '' );
      echo is_string( $terms ) ? $terms : '—';
      break;

    // Display the value of an ACF (Advanced Custom Fields) field
    case 'acf_field' :
      echo get_field( 'my_acf_field', $post_id );  
      break;

    // Display the value of an ACF (Advanced Custom Fields) array field
    case 'acf_field' :
      $var = get_field( 'my_acf_field', $post_id );
      echo implode(", ", $var);
      break;

    // Display the value of an related ACF (Advanced Custom Fields) field of an related Post Object
    case 'location-title' :
      echo get_the_title(get_field( 'contact-location', $post_id));
      break;

    // Display the value of an related ACF (Advanced Custom Fields) field of an ACF field
    case 'location-country' :
      echo get_field('location-country',get_field( 'contact-location', $post_id));
      break;
  }
}

siehe auch: https://developer.wordpress.org/reference/hooks/manage_posts_custom_column/

Spalte der Listenansicht des Custom Post Type sortierbar machen[Bearbeiten]

Beispielcode um Custom Column(s) sortierbar zu machen

add_filter( 'manage_edit-mycpt_sortable_columns', 'set_custom_mycpt_sortable_columns' );

function set_custom_mycpt_sortable_columns( $columns ) {
  $columns['custom_taxonomy'] = 'custom_taxonomy';
  $columns['acf_field'] = 'acf_field';

  return $columns;
}

https://www.ractoon.com/wordpress-sortable-admin-columns-for-custom-posts/

Spalte der Listenansicht des Custom Post Type alphabetisch sortieren[Bearbeiten]

// Order custom columns in WordPress Backend CPT Listing alphabetically
add_filter( 'manage_edit-reference_sortable_columns', 'reference_sortable_columns' );
function reference_sortable_columns( $columns ) {
    $columns['reference_customer_firma'] = 'reference_customer_firma';

    return $columns;
}

add_action( 'pre_get_posts', 'reference_firma_orderby' );
function reference_firma_orderby( $query ) {
    if( ! is_admin() )
        return;

    $orderby = $query->get( 'orderby');

    if( 'reference_customer_firma' == $orderby ) {
        $query->set('meta_key','reference_customer_firma');
        $query->set('orderby','meta_value');
        // "meta_value_num" is used for numeric sorting
        // "meta_value"     is used for Alphabetically sort.
    }
}

https://gist.github.com/vishalkakadiya/8c997a5e8789147e012b12cc4af90389

Spalte der Listenansicht des Custom Post Type filterbar machen[Bearbeiten]

Quellen[Bearbeiten]