• Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

Easy Web Design Tutorials

WordPress Tutorials and more

  • Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

15 August - 2018 By Paal Joachim 2 Comments

I will be showing how to add and remove columns that are listed in the post, page or custom post screen in the backend.
I began by creating a new php file I named columns.php which I placed into a lib folder inside the root of the child theme.

Adjusting the post list screen columns in the backend

// manage the columns of the post post type
function manage_columns_for_post($columns){
    //remove columns
    unset($columns['title']);
    unset($columns['categories']);
    unset($columns['tags']);
    unset($columns['date']);
    unset($columns['comments']);
    unset($columns['author']);

   //add new columns
    $columns['title'] = 'Title';
    $columns['post_featured_image'] = 'Featured Image';
    $columns['post_content']    = 'Content';
    $columns['categories']    = 'Categories';
    $columns['tags']    = 'Tags';
    $columns['date']    = 'Date';   
    $columns['author']    = 'Author';
    return $columns;
}

add_action('manage_post_posts_columns','manage_columns_for_post');

 

The code: manage_post_posts_columns affects the post list screen.

Post columns = manage_post_posts_columns
Page columns = manage_page_posts_columns
Custom Post columns = manage_movie_posts_columns

 

I removed (unset) all the columns and then added them back again in the order I wanted them in.
I found the names by right clicking the columns and selecting Inspect in the browser. The col id shows the names used above.

Adjusting-backend-post-list-columns

 

 

 

Adding content to the columns

//Populate custom columns for post type
function populate_post_columns($column,$post_id){

   // featured image column
   if($column == 'post_featured_image'){
      //if this post has a featured image
      if(has_post_thumbnail($post_id)){
        // Original: $post_featured_image = get_the_post_thumbnail($post_id,'thumbnail');
         echo the_post_thumbnail( array(100,100) );
     }else{
        echo 'This post has no featured image'; 
     }
}


    //post content column
    if($column == 'post_content'){

       //get the page based on its post_id
       $post = get_post($post_id);
       if($post){
          //get the main content area
          $post_content = apply_filters('the_content', $post->post_content); 
          // Original: echo $post_content; 
          echo wp_trim_words(get_the_content(), 10);
       }
   }
}
add_action('manage_post_posts_custom_column','populate_post_columns',10,2);

 

The finished result.

Adding-content-post-list-columns

 

Adjusting a movie custom post.

I have already made a movie custom post in this tutorial.
In the same columns.php file I added the following to affect the custom post type movie.

 

// manage the columns of the movie custom post type
function manage_columns_for_movie($columns){
  //remove columns
       unset($columns['title']);
       unset($columns['taxonomy-movie-years']);
       unset($columns['gadwp_stats']);
       unset($columns['tags']);
       unset($columns['taxonomy-movie-type']);
       unset($columns['date']);
       unset($columns['comments']);
       unset($columns['author']);

  //add new columns - Placing them in the order I want them in.
      $columns['title'] = 'Movie Title';  
      $columns['page_featured_image'] = 'Featured Image';
      $columns['page_content'] = 'Movie Content';
      $columns['taxonomy-movie-years'] = 'Year Screened '; 
      $columns['taxonomy-movie-type'] = 'Movie Category';
      $columns['date'] = 'Date';
   // $columns['Author'] = 'Author';
   // $columns['gadwp_stats'] = 'Stats';  - a plugin that adds a column. I removed it above and did not add it back in.
   // $columns['page_template'] = 'Page Template'; 
      return $columns;
  }
  add_action('manage_movie_posts_columns','manage_columns_for_movie'); 


//Populate custom columns for the movie custom post type
function populate_movie_columns($column,$post_id){

    //featured image column
    if($column == 'page_featured_image'){
        //if this page has a featured image
        if(has_post_thumbnail($post_id)){
            $post_featured_image = get_the_post_thumbnail($post_id,'thumbnail');
            // echo $post_featured_image;
            echo the_post_thumbnail( array(140,100) );
       }else{
            echo 'This custom post has no featured image'; 
       }
     }

     //page content column
    if($column == 'page_content'){
        //get the page based on its post_id
        $page = get_post($post_id);
        if($page){
           //get the main content area
           $page_content = apply_filters('the_content', $page->post_content); 
           echo wp_trim_words( get_the_content(), 10);
        }
     }
}

add_action('manage_movie_posts_custom_column','populate_movie_columns',10,2); 

 

The finished result.

Custom-post-movie-columns

Share this:

  • Email

Categories: Developer, Featured, How to, WordPress

Paal Joachim Romdahl

I enjoy teaching and creating tutorials. As well creating web sites.
I help people gain WordPress knowledge through my easy to follow tutorials and specialized training. Contact me for more information on how I can improve your WordPress skills and to help get your web site quickly up and running.

Comments

  1. magicamir says

    2 September - 2019

    Thanks for the good content

    Reply
  2. طراحان نواندیش پویا says

    3 October - 2018

    Thanks for submitting this entry

    Reply

Leave a Comment Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2021 · By Easy Web Design Tutorials · Built on the Genesis Framework · WordPress · Log in · ⇪

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.