Display Divi Projects in a Table

News: Divi Code Snippet Module now supports Divi 5

Written by Dan Mossop

The Divi theme includes a Project custom post type, which can be displayed in a grid format using the portfolio module. If you instead want to display your projects in a table, here's an example of code for displaying projects in table format, with custom fields and links to the individual projects. I.e. this is what we're trying to achieve:

Step 1: Set up Custom Fields

To display custom fields for you projects, you can set them up using the Advanced Custom Fields plugin. Something like this:

An important thing to note is that the field group must be assigned to the Project post type (as shown in orange).

2. Create Projects and Fill Out Custom Fields

Create any projects to be displayed in the table. Enter the values of their custom fields (on the project edit screen). Like so:

3. Create a Shortcode to Return the Table HTML

To let us embed the table anywhere in the page, we'll create a shortcode "db_portfolio_table". Here's the PHP code for the example. You may well need to modify it to suit your needs.

if (!class_exists('DBPortfolioTable')) {
	
	class DBPortfolioTable {
		
		public function html() {
			return 
				'<table class="sortable">'.
					$this->thead().
					$this->tbody($this->projects()).
					$this->tfoot().
				'</table>';
		}
		
		private function projects() {
			$result = array();
			$query = new WP_Query('post_type=project');
			while ($query->have_posts()) {
				$query->the_post();
				$result[] = array(
					'title' => get_the_title(),
					'url' => get_permalink(),
					'year' => $this->customField('year'),
					'client' => $this->customField('client'),
					'project-type' => $this->customField('project_type'),
					'location' => $this->customField('location')
				);
			}
			wp_reset_postdata();
			return $result;
		}
		
		private function customField($name) {
			if (!function_exists('get_field')) { return ''; } // Advanced Custom Fields plugin not found
			$value = get_field($name);
			return $value?$value:'';
		}
		
		private function thead() {
			return '
				<thead>
				<tr>
					<th>Project Name</th>
					<th>Year</th>
					<th>Client</th>
					<th>Project Type</th>
					<th>Location</th>
				</tr>
				</thead>';
		}
		
		private function tbody($projects) {
			$result = '';
			foreach($projects as $project) { 
				$result .= $this->row($project);
			}
			return '<tbody id="tablecontent">'.$result.'</tbody>';
		}
		
		private function row($project) {
			$result = '';
			$fields = array('title', 'year', 'client', 'project-type', 'location');
			foreach($fields as $field) {
				$result .= $this->cell($project, $field);
			}
			return "<tr>{$result}</tr>";
		}
		
		private function cell($project, $field) {
			return '<td class="'.esc_attr($field).'">'.$this->link($project, $field).'</td>';
		}
		
		private function link($project, $field) {
			return '<a href="'.esc_attr($project['url']).'">'.esc_html($project[$field]).'</a>';
		}
		
		private function tfoot() {
			return '<tfoot></tfoot>';
		}
	}
	
	add_shortcode('db_porfolio_table', array(new DBPortfolioTable, 'html'));
}

4. Add Shortcode to the Page

To add the table to the page, we can place its shortcode into a code module like so:

Save and view the page and it should hopefully now display a table similar to that shown at the start of the post.

We may earn a commission when you visit links on our website.

Enhance Dynamic Content with Divi Dynamic Content Extended

Make your project displays more interactive. Leverage the power of Divi Dynamic Content Extended to integrate advanced custom field data directly into your Divi designs. Seamlessly unlock your site's potential with more robust data handling and presentation options.

Latest Posts

Add a Branded QR Code in Divi

To create a branded QR Code in Divi, you can use the Divi QR Code module and its "Center Image" feature. This lets you add an image / logo in the center of the QR code, perfect for promoting your brand or adding a unique twist to your QR Codes. The image is displayed...

Add Text Before the Number in the Divi Circle Counter

Adding a custom number prefix to your Circle Counter lets you clarify what the value represents—such as currency, units, or labels—without relying on the default percentage format. This helps your counters better match real-world metrics and keeps the design...

Add a Second Line of Text in a Divi Button Module

Adding a second line of text beneath a button’s main label lets you provide extra context, a brief benefit statement, or a supporting call-to-action without cluttering your design. This can improve clarity and increase clicks by making the button’s purpose more...

Top (or Bottom) Align Divi Icon List Module Icons

When Icon List items wrap onto multiple lines, the default icon alignment can make the icon appear vertically centered next to the text, which may look unbalanced or harder to scan. Setting the icons to align at the top creates a cleaner, more consistent...

Make Divi Button Modules Full Width with Centered Content

This configuration makes a Divi button stretch to the full width of its container while keeping its label neatly centered, creating a bold, edge-to-edge call-to-action. You might want this for hero sections, pricing tables, or any layout where a standard-sized button...

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

2 Comments

  1. Dan thank you, this is excellent. Absolutely perfectly executed and exactly what I needed for my project. I had tried cobbling together some code but when I compared it to yours it doesn't cut the mustard like yours does.

    Do you have a ko-fi page or something so I can, at the very least, buy you a coffee?

    Reply
    • Hey Surya, I'm glad it did what you needed! I don't have a ko-fi page, but I do drink coffee, so I've sent you an email with my PayPal address. But please don't feel at all obliged to send anything – I'm just happy to have been able to help. Cheers!

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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

Get this feature

This feature is available in the following plugins: