Skip to main content

Post Header Integration

Post Header Diagram

This guide details how to programmatically populate the visual "Header" section of pix-article posts (and standard posts), which corresponds to the visual layer commonly referred to as the "Layer 2" or "Hero Section".

Field Mapping​

The visual elements map to standard WordPress core fields:

Visual ElementWordPress FieldDatabase ColumnFunction
1. CategoryTaxonomy Termwp_term_relationshipswp_set_object_terms()
2. Post TitlePost Titlepost_titlewp_insert_post() / wp_update_post()
3. ExcerptPost Excerptpost_excerptwp_insert_post() / wp_update_post()
4. Published DatePost Datepost_datewp_insert_post() / wp_update_post()
5. Featured ImageFeatured Image_thumbnail_id (meta)set_post_thumbnail()

Programmatic Implementation​

1. Creating/Updating the Post Object​

The Title, Excerpt, and Date are set directly on the post object.

$post_data = array(
'ID' => $existing_post_id, // include only if updating
'post_title' => 'Layer 2: Noumenal Notes', // [2] Post Title
'post_excerpt' => 'Lorem ipsum dolor sit amet...', // [3] Excerpt
'post_date' => '2022-04-12 12:00:00', // [4] Published Date (Y-m-d H:i:s)
'post_status' => 'publish',
'post_type' => 'pix-article', // Custom Post Type
);

// Insert or Update
$post_id = wp_insert_post($post_data);

2. Setting the Category​

The "pill" at the top (e.g., "GRPS protection") is a Taxonomy Term.

  • Taxonomy: Usually category (Standard) or pix-portfolio-cat depending on theme config.
  • Function: wp_set_object_terms
// Define the Category Name or ID
$category_name = 'GRPS protection'; // [1] Category
$taxonomy_slug = 'category'; // Check if 'pix-article' uses a custom taxonomy

// Resolve Term ID (create if doesn't exist)
$term_info = term_exists($category_name, $taxonomy_slug);
if (!$term_info) {
$term_info = wp_insert_term($category_name, $taxonomy_slug);
}
$term_id = is_array($term_info) ? $term_info['term_id'] : $term_info;

// Assign to Post
wp_set_object_terms($post_id, array($term_id), $taxonomy_slug);

The background image (Hero Image) is the standard Featured Image.

$image_url = 'https://example.com/path/to/image.jpg'; // [5] Featured Image Source
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);

if(wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}

file_put_contents($file, $image_data);

$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);

$attach_id = wp_insert_attachment($attachment, $file, $post_id);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);

// Set as Featured Image
set_post_thumbnail($post_id, $attach_id);

Troubleshooting​

  • Category Not Showing?: If the "GRPS protection" pill doesn't appear, verify the Taxonomy Slug. Some themes treat pix-article as part of a portfolio logic and use pix-portfolio-cat or pix-service-cat. Check the URL of the category page in the admin panel (e.g., /wp-admin/edit-tags.php?taxonomy=pix-portfolio-cat).
  • Excerpt Not Showing?: Ensure the theme's header.php or bg_image.php template hasn't been modified to exclude the excerpt for this post type.