Modify Custom Taxonomy slug with CPT slug - url-rewriting

Hi please check my requirement bellow.
First of all, I have registered a CPT "directory" with a slug name "directory". Also, I have registered a custom taxonomy "business_category". I want to below permalink structure.
CPT Archive link: www.domain.com/directory/
Taxonomy link: www.domain.com/directory/category/TAXONOMY_NAME/
CPT Single Page: www.domain.com/directory/POST_NAME
So, I have used the Below code.
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->PT = 'cc-directory';
$this->name = 'Directory';
$this->singular_name = 'Directory';
$this->slug = 'directory';
}
public function register_post_type() {
// Get supported features for Directory post type
$supports = apply_filters('cc_directory_supports', array('editor', 'title','thumbnail'));
$labels = array(
'name' => $this->name,
'singular_name' => $this->singular_name,
'add_new' => 'Add New',
'add_new_item' => 'Add New ' . $this->singular_name,
'edit_item' => 'Edit ' . $this->singular_name,
'new_item' => 'New ' . $this->singular_name,
'all_items' => 'All ' . $this->name,
'view_item' => 'View ' . $this->name,
'search_items' => 'Search ' . $this->name,
'not_found' => 'No ' . strtolower($this->name) . ' found',
'not_found_in_trash' => 'No ' . strtolower($this->name) . ' found in Trash',
'parent_item_colon' => '',
'menu_name' => $this->name
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => $this->slug ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => 11,
'menu_icon' => 'dashicons-book',
'supports' => $supports,
'yarpp_support' => true
);
register_post_type( $this->PT, $args );
$plural_label = 'Categories';
$singular_label = 'Category';
register_taxonomy(
'business_category',
$this->PT,
array(
'label' => $plural_label,
'labels' => array(
'name' => $plural_label,
'singular_name' => $singular_label,
'all_items' => sprintf(__('All %s', 'claritycloud-directory'), $plural_label),
'edit_item' => sprintf(__('Edit %s', 'claritycloud-directory'), $singular_label),
'view_item' => sprintf(__('View %s', 'claritycloud-directory'), $singular_label),
'update_item' => sprintf(__('Update %s', 'claritycloud-directory'), $singular_label),
'add_new_item' => sprintf(__('Add New %s', 'claritycloud-directory'), $singular_label),
'new_item_name' => sprintf(__('New %s Name', 'claritycloud-directory'), $singular_label),
'popular_items' => sprintf(__('Popular %s', 'claritycloud-directory'), $plural_label),
'search_items' => sprintf(__('Search %s', 'claritycloud-directory'), $plural_label),
),
'public' => true,
'show_ui' => true,
'show_in_rest' => true,
'hierarchical' => true,
'rewrite' => array('slug' => 'business-category'),
)
);
}
Right now my URLs are :
http://localhost/demo-project/directory/
http://localhost/demo-project/business-category/antiques/
http://localhost/demo-project/directory/a-lil-bit-of-sas/
It will be like below:
http://localhost/demo-project/directory/
http://localhost/demo-project/directory/category/antiques/
http://localhost/demo-project/directory/a-lil-bit-of-sas/
Just need to modify the above 2nd URL.
Can anyone please advise me?
Thanks,
Subhankar

Finally, I got the solution here:
add_action('init', 'cpt_resources');
function cpt_resources() {
$labels = array(
'name' => _x('Resources', 'snt'),
'singular_name' => _x('Resource', 'snt'),
'add_new' => _x('Add Resource', 'snt'),
'add_new_item' => __('Add Resource'),
'edit_item' => __('Edit Resource'),
'new_item' => __('New Resource'),
'view_item' => __('View Resource'),
'search_items' => __('Search Resources'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'taxonomies' => array('resource_type'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'resources' ),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','thumbnail', 'editor' ),
);
register_post_type( 'resources_post_type' , $args );
}
function resource_type() {
$labels = array(
'name' => _x( 'Resource Types', 'Taxonomy General Name', 'snt' ),
'singular_name' => _x( 'Resource Type', 'Taxonomy Singular Name', 'snt' ),
'menu_name' => __( 'Resource Types', 'snt' ),
'all_items' => __( 'All Items', 'snt' ),
'parent_item' => __( 'Parent Item', 'snt' ),
'parent_item_colon' => __( 'Parent Item:', 'snt' ),
'new_item_name' => __( 'New Item Name', 'snt' ),
'add_new_item' => __( 'Add New Item', 'snt' ),
'edit_item' => __( 'Edit Item', 'snt' ),
'update_item' => __( 'Update Item', 'snt' ),
'view_item' => __( 'View Item', 'snt' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'snt' ),
'add_or_remove_items' => __( 'Add or remove items', 'snt' ),
'choose_from_most_used' => __( 'Choose from the most used', 'snt' ),
'popular_items' => __( 'Popular Items', 'snt' ),
'search_items' => __( 'Search Items', 'snt' ),
'not_found' => __( 'Not Found', 'snt' ),
'no_terms' => __( 'No items', 'snt' ),
'items_list' => __( 'Items list', 'snt' ),
'items_list_navigation' => __( 'Items list navigation', 'snt' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array('slug' => 'resources/category')
);
register_taxonomy( 'resource_type', array( 'resources_post_type' ), $args );
}
add_action( 'init', 'resource_type', 0 );
function resources_cpt_generating_rule($wp_rewrite) {
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'resource_type',
'hide_empty' => false,
) );
$post_type = 'resources_post_type';
foreach ($terms as $term) {
$rules['resources/category/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&resources_post_type=$matches[1]&name=$matches[1]';
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'resources_cpt_generating_rule');
Result :
http://localhost/demo-wp/resources/
http://localhost/demo-wp/resources/category/test-resources/
http://localhost/demo-wp/resources/final-check/

Related

Sub CPT taxonomy 404 page

I have a problem with my sub taxonomies.
my-cpt/chapitres archive is working. I made a redirection to a page
from the tax-template.
my-cpt/chapitres/sub-term --> 404
my-cpt/chapitres/sub-term/post --> working
This is my code :
cpt
function custom_cpt() {
$labels = array(
'name' => __( 'Mon cpt' ),
'singular_name' => __( 'Mon cpt'),
'menu_name' => __( 'Mon cpt'),
'parent_item_colon' => __( 'Parent Mon cpt'),
// Paramétrages
'all_items' => __( 'Mon cpt'),
'view_item' => __( 'Voir'),
'add_new_item' => __( 'Ajouter du contenu'),
'add_new' => __( 'Ajout contenu'),
'edit_item' => __( 'Modifier'),
'update_item' => __( 'Mettre à jour'),
'search_items' => __( 'Rechercher'),
'not_found' => __( 'Non trouvé'),
'not_found_in_trash' => __( 'Non trouvé dans la corbeille'),
);
$args = array(
'label' => __( 'my-cpt'), // label
'description' => __( 'Mon cpt'),
'labels' => $labels,
'menu_icon' => 'dashicons-book-alt',
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail'),
'public' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'my-cpt/%chapitres%', 'with_front' => false),
'show_in_rest' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'has_archive' => false,
'can_export' => true,
'query_var' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'menu_position' => 5,
);
register_post_type( 'my-cpt', $args );
}
add_action( 'init', 'custom_cpt', 0 );
Taxonomy:
add_action( 'init', 'custom_cpt_taxonomies', 0 );
function custom_cpt_taxonomies() {
$labels = array(
'name' => _x( 'Chapitres', 'taxonomy general name' ),
'singular_name' => _x( 'Chapitre', 'taxonomy singular name' ),
'search_items' => __( 'Rechercher un chapitre' ),
'all_items' => __( 'Tous les chapitres' ),
'parent_item' => __( 'Chapitres Parents' ),
'parent_item_colon' => __( 'Chapitre Parent:' ),
'edit_item' => __( 'Modifier' ),
'update_item' => __( 'Mettre à jour' ),
'add_new_item' => __( 'Ajouter un chapitre' ),
'new_item_name' => __( 'Nouveau chapitre' ),
'menu_name' => __( 'Chapitres' ),
'has_archive' => false,
);
register_taxonomy('chapitres',array('my-cpt'), array(
"labels" => $labels,
"hierarchical" => true,
"public" => true,
"show_ui" => true,
"show_admin_column" => true,
"show_in_nav_menus" => true,
"show_tagcloud" => true,
"show_in_rest" => true,
'rewrite' => array('hierarchical' => true, 'with_front' => false ),
));
}
I found this for the rewrite rules
function rewrite_permalink_cpt($link, $post){
if ($post->post_type != 'my-cpt')
return $link;
if ($cats = get_the_terms($post->ID, 'chapitres')){
$link = str_replace('%chapitres%', get_taxonomy_parents(array_pop($cats)->term_id, 'chapitres', false, '/', true), $link);
}
return $link;
}
add_filter('post_type_link', 'rewrite_permalink_cpt', 10, 2);
add_filter('rewrite_rules_array', 'rewrite_rules_permalink_cpt');
function rewrite_rules_permalink_cpt($rules){
$cpt = array();
$cpt['my-cpt/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?my-cpt=$matches[4]';
$newRules['my-cpt/(.+)/(.+)/?$'] = 'index.php?my-cpt=$matches[1]';
return array_merge($cpt, $rules);
}
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {
$chain = '';
$parentref = get_term($id, $taxonomy);
$parent = &$parentref;
if (is_wp_error($parent)) {
return $parent;
}
if ($nicename)
$name = $parent->slug;
else
$name = $parent->name;
if($parent!=0){
if ($parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited)) {
$visited[] = $parent->parent;
$chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $nicename, $visited);
$chain .= "/";
}
if ($link) {
// nothing, can't get this working :(
} else
$chain .= $name;
return $chain;
}
}
I think the problem is come from the rewrite rules, but I don't know what is wrong...
It would be appreciate if someone has an idea

Posts inserted with wp_insert_posts aren't showing up in admin

I have a particular issue with inserting custom posts with wp_insert_posts.
Circumstances:
WPML
Multisite enabled
Custom post type with name order
Post creation triggered through Ajax from frontend
The post is inserted in the original language, no translations
What have I tried so far
Checked the post type register code to see if it contains any incompatible settings, tried all combinations -> noting. This is what is the closest thing to my problem: wp_insert_post entries not showing in admin content list
The resolution there is not applicable to me, as I execute it through Ajax, and WP is already initialized at the Ajax hook
Went through WPML forums with all their posts related to similar issues, tried the steps provided there (although most of them talk about adding translations)
Tried various other solutions
Post insert code:
$post_id = wp_insert_post([
"post_type" => "order",
"post_content" => "",
"post_title" => $order->Name() . '<' . $order->Email() . '>',
"post_status" => "publish",
"post_author" => 1
]);
update_post_meta($post_id, 'order_details', '{"some":"json here"}');
The post gets inserted in the table, and the listing page in admin shows it in the total counts, but the post doesn't show up in the list itself.
Post type register code
if ( ! function_exists('ls_so_order') ) {
// Register Custom Post Type
function ls_so_order() {
$labels = array(
'name' => _x( 'Orders', 'Post Type General Name', 'ls-simple-order' ),
'singular_name' => _x( 'Order', 'Post Type Singular Name', 'ls-simple-order' ),
'menu_name' => __( 'Orders', 'ls-simple-order' ),
'name_admin_bar' => __( 'ProductOrder', 'ls-simple-order' ),
'archives' => __( 'Order Archives', 'ls-simple-order' ),
'attributes' => __( 'Order Attributes', 'ls-simple-order' ),
'parent_item_colon' => __( 'Parent Order:', 'ls-simple-order' ),
'all_items' => __( 'All Orders', 'ls-simple-order' ),
'add_new_item' => __( 'Add New Order', 'ls-simple-order' ),
'add_new' => __( 'Add New', 'ls-simple-order' ),
'new_item' => __( 'New Order', 'ls-simple-order' ),
'edit_item' => __( 'Edit Order', 'ls-simple-order' ),
'update_item' => __( 'Update Order', 'ls-simple-order' ),
'view_item' => __( 'View Order', 'ls-simple-order' ),
'view_items' => __( 'View Orders', 'ls-simple-order' ),
'search_items' => __( 'Search Order', 'ls-simple-order' ),
'not_found' => __( 'Not found', 'ls-simple-order' ),
'not_found_in_trash' => __( 'Not found in Trash', 'ls-simple-order' ),
'featured_image' => __( 'Featured Image', 'ls-simple-order' ),
'set_featured_image' => __( 'Set featured image', 'ls-simple-order' ),
'remove_featured_image' => __( 'Remove featured image', 'ls-simple-order' ),
'use_featured_image' => __( 'Use as featured image', 'ls-simple-order' ),
'insert_into_item' => __( 'Insert into order', 'ls-simple-order' ),
'uploaded_to_this_item' => __( 'Uploaded to this order', 'ls-simple-order' ),
'items_list' => __( 'Orders list', 'ls-simple-order' ),
'items_list_navigation' => __( 'Orders list navigation', 'ls-simple-order' ),
'filter_items_list' => __( 'Filter orders list', 'ls-simple-order' ),
);
$args = array(
'label' => __( 'Order', 'ls-simple-order' ),
'description' => __( 'Simple Order Orders', 'ls-simple-order' ),
'labels' => $labels,
'supports' => array( 'title', 'thumbnail', 'custom-fields' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-cart',
'show_in_admin_bar' => true,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'capability_type' => 'page',
);
register_post_type( 'order', $args );
}
add_action( 'init', 'ls_so_order', 0 );
}
Can you try doing this?
$post_data = array(
'post_title' => 'Test regex',
'post_content' => $order->Name() . '<' . $order->Email() . '>',
'post_type' => 'post',
'post_status' => 'publish',
'post_author' => 1,
);
$post_id = wp_insert_post($post_data);
update_post_meta($post_id, 'order_details', '{"some":"json here"}');
After going back and forth in every forum I could find, and tinkering with #miguelcalderons answer, I found the culprit, and it is a very simple, although quiet hidden thing.
WordPress Reserved Terms
WordPress has a quiet long list of reserved terms, and order being one of them.
After replacing the custom post type slug with a different string, it started working immediately.

Divi image gallery sorting

Hope someone could help me with this one! I've already asked for their support but "it goes beyond their support level" and they even adviced me to ask it here..
So I'm using the Divi image gallery for my wordpress catalog, which contains lots of images.
Now I'm looking for a way that the images in the gallery are standard sorted by filename.
Any guidelines how to do change this in their code?
Thanks a lot!!
File names for attachments are stored in the 'post_name' column of the posts table. Therefore, this simple change should do the trick.
The simple answer to your question is:
Locate the file divi/includes/builder/main-modules.php
Search for function get_galleryin the file
From there, scroll down slightly to find $attachments_args
Change 'orderby' => 'post__in' to 'orderby' => 'post_name'
The bad news...
Making a change to main-modules.php means either you can no longer do updates or when you do update you'll have to apply the hack again.
Michael is right BUT:
The good news
You you can insert it into your Child Theme and everything is save
The better answer to your question is:
Locate the file divi/includes/builder/main-modules.php
Copy the ET_Builder_Module_Gallery
Insert into your functions.php, wrap and modify like Michael said
Complete code for this
function divi_child_theme_setup() {
if ( class_exists('ET_Builder_Module')) {
class CHILD_ET_Builder_Module_Gallery extends ET_Builder_Module {
function init() {
$this->name = esc_html__( 'Gallery', 'et_builder' );
$this->slug = 'et_pb_gallery';
$this->fb_support = true;
$this->whitelisted_fields = array(
'src',
'gallery_ids',
'gallery_orderby',
'fullwidth',
'posts_number',
'show_title_and_caption',
'show_pagination',
'background_layout',
'auto',
'auto_speed',
'admin_label',
'module_id',
'module_class',
'zoom_icon_color',
'hover_overlay_color',
'hover_icon',
'orientation',
);
$this->fields_defaults = array(
'fullwidth' => array( 'off' ),
'posts_number' => array( 4, 'add_default_setting' ),
'show_title_and_caption' => array( 'on' ),
'show_pagination' => array( 'on' ),
'background_layout' => array( 'light' ),
'auto' => array( 'off' ),
'auto_speed' => array( '7000' ),
'orientation' => array( 'landscape' ),
);
$this->options_toggles = array(
'general' => array(
'toggles' => array(
'main_content' => esc_html__( 'Images', 'et_builder' ),
'elements' => esc_html__( 'Elements', 'et_builder' ),
),
),
'advanced' => array(
'toggles' => array(
'layout' => esc_html__( 'Layout', 'et_builder' ),
'overlay' => esc_html__( 'Overlay', 'et_builder' ),
'text' => array(
'title' => esc_html__( 'Text', 'et_builder' ),
'priority' => 49,
),
),
),
'custom_css' => array(
'toggles' => array(
'animation' => array(
'title' => esc_html__( 'Animation', 'et_builder' ),
'priority' => 90,
),
),
),
);
$this->main_css_element = '%%order_class%%.et_pb_gallery';
$this->advanced_options = array(
'fonts' => array(
'title' => array(
'label' => esc_html__( 'Title', 'et_builder' ),
'css' => array(
'main' => "{$this->main_css_element} .et_pb_gallery_title",
),
),
'caption' => array(
'label' => esc_html__( 'Caption', 'et_builder' ),
'use_all_caps' => true,
'css' => array(
'main' => "{$this->main_css_element} .mfp-title, {$this->main_css_element} .et_pb_gallery_caption",
),
'line_height' => array(
'range_settings' => array(
'min' => '1',
'max' => '100',
'step' => '1',
),
),
'depends_show_if' => 'off'
),
),
'border' => array(
'css' => array(
'main' => "{$this->main_css_element} .et_pb_gallery_item",
),
),
);
$this->custom_css_options = array(
'gallery_item' => array(
'label' => esc_html__( 'Gallery Item', 'et_builder' ),
'selector' => '.et_pb_gallery_item',
),
'overlay' => array(
'label' => esc_html__( 'Overlay', 'et_builder' ),
'selector' => '.et_overlay',
),
'overlay_icon' => array(
'label' => esc_html__( 'Overlay Icon', 'et_builder' ),
'selector' => '.et_overlay:before',
),
'gallery_item_title' => array(
'label' => esc_html__( 'Gallery Item Title', 'et_builder' ),
'selector' => '.et_pb_gallery_title',
),
'gallery_item_caption' => array(
'label' => esc_html__( 'Gallery Item Caption', 'et_builder' ),
'selector' => '.et_pb_gallery_caption',
),
'gallery_pagination' => array(
'label' => esc_html__( 'Gallery Pagination', 'et_builder' ),
'selector' => '.et_pb_gallery_pagination',
),
'gallery_pagination_active' => array(
'label' => esc_html__( 'Pagination Active Page', 'et_builder' ),
'selector' => '.et_pb_gallery_pagination a.active',
),
);
}
function get_fields() {
$fields = array(
'src' => array(
'label' => esc_html__( 'Gallery Images', 'et_builder' ),
'renderer' => 'et_builder_get_gallery_settings',
'option_category' => 'basic_option',
'overwrite' => array(
'ids' => 'gallery_ids',
'orderby' => 'gallery_orderby',
),
'toggle_slug' => 'main_content',
),
'gallery_ids' => array(
'type' => 'hidden',
'class' => array( 'et-pb-gallery-ids-field' ),
'computed_affects' => array(
'__gallery',
),
),
'gallery_orderby' => array(
'label' => esc_html__( 'Gallery Images', 'et_builder' ),
'type' => 'hidden',
'class' => array( 'et-pb-gallery-ids-field' ),
'computed_affects' => array(
'__gallery',
),
'toggle_slug' => 'main_content',
),
'fullwidth' => array(
'label' => esc_html__( 'Layout', 'et_builder' ),
'type' => 'select',
'option_category' => 'layout',
'options' => array(
'off' => esc_html__( 'Grid', 'et_builder' ),
'on' => esc_html__( 'Slider', 'et_builder' ),
),
'description' => esc_html__( 'Toggle between the various blog layout types.', 'et_builder' ),
'affects' => array(
'zoom_icon_color',
'caption_font',
'caption_text_color',
'caption_line_height',
'caption_font_size',
'caption_all_caps',
'caption_letter_spacing',
'hover_overlay_color',
'auto',
'posts_number',
'show_title_and_caption',
'orientation'
),
'computed_affects' => array(
'__gallery',
),
'tab_slug' => 'advanced',
'toggle_slug' => 'layout',
),
'posts_number' => array(
'label' => esc_html__( 'Images Number', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'description' => esc_html__( 'Define the number of images that should be displayed per page.', 'et_builder' ),
'depends_show_if' => 'off',
'toggle_slug' => 'main_content',
),
'orientation' => array(
'label' => esc_html__( 'Thumbnail Orientation', 'et_builder' ),
'type' => 'select',
'options_category' => 'configuration',
'options' => array(
'landscape' => esc_html__( 'Landscape', 'et_builder' ),
'portrait' => esc_html__( 'Portrait', 'et_builder' )
),
'description' => sprintf(
'%1$s<br><small><em><strong>%2$s:</strong> %3$s %4$s.</em></small>',
esc_html__( 'Choose the orientation of the gallery thumbnails.', 'et_builder' ),
esc_html__( 'Note', 'et_builder' ),
esc_html__( 'If this option appears to have no effect, you might need to', 'et_builder' ),
esc_html__( 'regenerate your thumbnails', 'et_builder')
),
'depends_show_if' => 'off',
'computed_affects' => array(
'__gallery',
),
'tab_slug' => 'advanced',
'toggle_slug' => 'layout',
),
'show_title_and_caption' => array(
'label' => esc_html__( 'Show Title and Caption', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'description' => esc_html__( 'Whether or not to show the title and caption for images (if available).', 'et_builder' ),
'depends_show_if' => 'off',
'toggle_slug' => 'elements',
),
'show_pagination' => array(
'label' => esc_html__( 'Show Pagination', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'toggle_slug' => 'elements',
'description' => esc_html__( 'Enable or disable pagination for this feed.', 'et_builder' ),
),
'background_layout' => array(
'label' => esc_html__( 'Text Color', 'et_builder' ),
'type' => 'select',
'option_category' => 'color_option',
'options' => array(
'light' => esc_html__( 'Dark', 'et_builder' ),
'dark' => esc_html__( 'Light', 'et_builder' ),
),
'tab_slug' => 'advanced',
'toggle_slug' => 'text',
'description' => esc_html__( 'Here you can choose whether your text should be light or dark. If you are working with a dark background, then your text should be light. If your background is light, then your text should be set to dark.', 'et_builder' ),
),
'auto' => array(
'label' => esc_html__( 'Automatic Animation', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'off' => esc_html__( 'Off', 'et_builder' ),
'on' => esc_html__( 'On', 'et_builder' ),
),
'affects' => array(
'auto_speed',
),
'depends_show_if' => 'on',
'depends_to' => array(
'fullwidth',
),
'tab_slug' => 'custom_css',
'toggle_slug' => 'animation',
'description' => esc_html__( 'If you would like the slider to slide automatically, without the visitor having to click the next button, enable this option and then adjust the rotation speed below if desired.', 'et_builder' ),
),
'auto_speed' => array(
'label' => esc_html__( 'Automatic Animation Speed (in ms)', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'depends_default' => true,
'tab_slug' => 'custom_css',
'toggle_slug' => 'animation',
'description' => esc_html__( "Here you can designate how fast the slider fades between each slide, if 'Automatic Animation' option is enabled above. The higher the number the longer the pause between each rotation.", 'et_builder' ),
),
'zoom_icon_color' => array(
'label' => esc_html__( 'Zoom Icon Color', 'et_builder' ),
'type' => 'color-alpha',
'custom_color' => true,
'depends_show_if' => 'off',
'tab_slug' => 'advanced',
'toggle_slug' => 'overlay',
),
'hover_overlay_color' => array(
'label' => esc_html__( 'Hover Overlay Color', 'et_builder' ),
'type' => 'color-alpha',
'custom_color' => true,
'depends_show_if' => 'off',
'tab_slug' => 'advanced',
'toggle_slug' => 'overlay',
),
'hover_icon' => array(
'label' => esc_html__( 'Hover Icon Picker', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'class' => array( 'et-pb-font-icon' ),
'renderer' => 'et_pb_get_font_icon_list',
'renderer_with_field' => true,
'tab_slug' => 'advanced',
'toggle_slug' => 'overlay',
),
'disabled_on' => array(
'label' => esc_html__( 'Disable on', 'et_builder' ),
'type' => 'multiple_checkboxes',
'options' => array(
'phone' => esc_html__( 'Phone', 'et_builder' ),
'tablet' => esc_html__( 'Tablet', 'et_builder' ),
'desktop' => esc_html__( 'Desktop', 'et_builder' ),
),
'additional_att' => 'disable_on',
'option_category' => 'configuration',
'description' => esc_html__( 'This will disable the module on selected devices', 'et_builder' ),
'tab_slug' => 'custom_css',
'toggle_slug' => 'visibility',
),
'admin_label' => array(
'label' => esc_html__( 'Admin Label', 'et_builder' ),
'type' => 'text',
'description' => esc_html__( 'This will change the label of the module in the builder for easy identification.', 'et_builder' ),
'toggle_slug' => 'admin_label',
),
'module_id' => array(
'label' => esc_html__( 'CSS ID', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'tab_slug' => 'custom_css',
'toggle_slug' => 'classes',
'option_class' => 'et_pb_custom_css_regular',
),
'module_class' => array(
'label' => esc_html__( 'CSS Class', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'tab_slug' => 'custom_css',
'toggle_slug' => 'classes',
'option_class' => 'et_pb_custom_css_regular',
),
'__gallery' => array(
'type' => 'computed',
'computed_callback' => array( 'ET_Builder_Module_Gallery', 'get_gallery' ),
'computed_depends_on' => array(
'gallery_ids',
'gallery_orderby',
'fullwidth',
'orientation',
),
),
);
return $fields;
}
/**
* Get attachment data for gallery module
*
* #param array $args {
* Gallery Options
*
* #type array $gallery_ids Attachment Ids of images to be included in gallery.
* #type string $gallery_orderby `orderby` arg for query. Optional.
* #type string $fullwidth on|off to determine grid / slider layout
* #type string $orientation Orientation of thumbnails (landscape|portrait).
* }
* #param array $conditional_tags
* #param array $current_page
*
* #return array Attachments data
*/
static function get_gallery( $args = array(), $conditional_tags = array(), $current_page = array() ) {
$attachments = array();
$defaults = array(
'gallery_ids' => array(),
'gallery_orderby' => '',
'fullwidth' => 'off',
'orientation' => 'landscape',
);
$args = wp_parse_args( $args, $defaults );
$attachments_args = array(
'include' => $args['gallery_ids'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'post_name',
);
if ( 'rand' === $args['gallery_orderby'] ) {
$attachments_args['orderby'] = 'rand';
}
if ( 'on' === $args['fullwidth'] ) {
$width = 1080;
$height = 9999;
} else {
$width = 400;
$height = ( 'landscape' === $args['orientation'] ) ? 284 : 516;
}
$width = (int) apply_filters( 'et_pb_gallery_image_width', $width );
$height = (int) apply_filters( 'et_pb_gallery_image_height', $height );
$_attachments = get_posts( $attachments_args );
foreach ( $_attachments as $key => $val ) {
$attachments[$key] = $_attachments[$key];
$attachments[$key]->image_src_full = wp_get_attachment_image_src( $val->ID, 'full' );
$attachments[$key]->image_src_thumb = wp_get_attachment_image_src( $val->ID, array( $width, $height ) );
}
return $attachments;
}
function shortcode_callback( $atts, $content = null, $function_name ) {
$module_id = $this->shortcode_atts['module_id'];
$module_class = $this->shortcode_atts['module_class'];
$gallery_ids = $this->shortcode_atts['gallery_ids'];
$fullwidth = $this->shortcode_atts['fullwidth'];
$show_title_and_caption = $this->shortcode_atts['show_title_and_caption'];
$background_layout = $this->shortcode_atts['background_layout'];
$posts_number = $this->shortcode_atts['posts_number'];
$show_pagination = $this->shortcode_atts['show_pagination'];
$gallery_orderby = $this->shortcode_atts['gallery_orderby'];
$zoom_icon_color = $this->shortcode_atts['zoom_icon_color'];
$hover_overlay_color = $this->shortcode_atts['hover_overlay_color'];
$hover_icon = $this->shortcode_atts['hover_icon'];
$auto = $this->shortcode_atts['auto'];
$auto_speed = $this->shortcode_atts['auto_speed'];
$orientation = $this->shortcode_atts['orientation'];
$module_class = ET_Builder_Element::add_module_order_class( $module_class, $function_name );
if ( '' !== $zoom_icon_color ) {
ET_Builder_Element::set_style( $function_name, array(
'selector' => '%%order_class%% .et_overlay:before',
'declaration' => sprintf(
'color: %1$s !important;',
esc_html( $zoom_icon_color )
),
) );
}
if ( '' !== $hover_overlay_color ) {
ET_Builder_Element::set_style( $function_name, array(
'selector' => '%%order_class%% .et_overlay',
'declaration' => sprintf(
'background-color: %1$s;
border-color: %1$s;',
esc_html( $hover_overlay_color )
),
) );
}
// Get gallery item data
$attachments = self::get_gallery( array(
'gallery_ids' => $gallery_ids,
'gallery_orderby' => $gallery_orderby,
'fullwidth' => $fullwidth,
'orientation' => $orientation,
) );
if ( empty( $attachments ) ) {
return '';
}
wp_enqueue_script( 'hashchange' );
$fullwidth_class = 'on' === $fullwidth ? ' et_pb_slider et_pb_gallery_fullwidth' : ' et_pb_gallery_grid';
$background_class = " et_pb_bg_layout_{$background_layout}";
$module_class .= 'on' === $auto && 'on' === $fullwidth ? ' et_slider_auto et_slider_speed_' . esc_attr( $auto_speed ) : '';
$posts_number = 0 === intval( $posts_number ) ? 4 : intval( $posts_number );
$output = sprintf(
'<div%1$s class="et_pb_module et_pb_gallery%2$s%3$s%4$s clearfix">
<div class="et_pb_gallery_items et_post_gallery" data-per_page="%5$d">',
( '' !== $module_id ? sprintf( ' id="%1$s"', esc_attr( $module_id ) ) : '' ),
( '' !== $module_class ? sprintf( ' %1$s', esc_attr( ltrim( $module_class ) ) ) : '' ),
esc_attr( $fullwidth_class ),
esc_attr( $background_class ),
esc_attr( $posts_number )
);
foreach ( $attachments as $id => $attachment ) {
$data_icon = '' !== $hover_icon
? sprintf(
' data-icon="%1$s"',
esc_attr( et_pb_process_font_icon( $hover_icon ) )
)
: '';
$image_output = sprintf(
'<a href="%1$s" title="%2$s">
<img src="%3$s" alt="%2$s" />
<span class="et_overlay%4$s"%5$s></span>
</a>',
esc_url( $attachment->image_src_full[0] ),
esc_attr( $attachment->post_title ),
esc_url( $attachment->image_src_thumb[0] ),
( '' !== $hover_icon ? ' et_pb_inline_icon' : '' ),
$data_icon
);
$output .= sprintf(
'<div class="et_pb_gallery_item%2$s%1$s">',
esc_attr( $background_class ),
( 'on' !== $fullwidth ? ' et_pb_grid_item' : '' )
);
$output .= "
<div class='et_pb_gallery_image {$orientation}'>
$image_output
</div>";
if ( 'on' !== $fullwidth && 'on' === $show_title_and_caption ) {
if ( trim($attachment->post_title) ) {
$output .= "
<h3 class='et_pb_gallery_title'>
" . wptexturize($attachment->post_title) . "
</h3>";
}
if ( trim($attachment->post_excerpt) ) {
$output .= "
<p class='et_pb_gallery_caption'>
" . wptexturize($attachment->post_excerpt) . "
</p>";
}
}
$output .= "</div>";
}
$output .= "</div><!-- .et_pb_gallery_items -->";
if ( 'on' !== $fullwidth && 'on' === $show_pagination ) {
$output .= "<div class='et_pb_gallery_pagination'></div>";
}
$output .= "</div><!-- .et_pb_gallery -->";
return $output;
}
}
$cgm = new CHILD_ET_Builder_Module_Gallery();
remove_shortcode( 'et_pb_gallery' );
add_shortcode( 'et_pb_gallery', array($cgm, '_shortcode_callback') );
}
}
add_action('wp', 'divi_child_theme_setup', 9999);
I used code from Divi 3.0.51. have fun

ZF2 How to validate fields inside fieldset?

I have a field in the form like this:
$this -> add(array(
'type' => 'field-set',
'name' => 'meta_properties',
'options' => array(
'label' => "Meta Properties",
),
'elements' => array(
)
));
$meta_fieldSet = $this -> get('meta_properties');
$meta_fieldSet->add(array(
'name' => 'meta_title',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Title',
),
));
$meta_fieldSet->add(array(
'name' => 'meta_description',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Description',
),
));
$meta_fieldSet->add(array(
'name' => 'meta_keywords',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Keywords',
)
));
And in my input filter class I have
$inputFilter -> add($factory -> createInput(array(
'name' => 'meta_title',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' => 150,
),
),
),
)));
$inputFilter -> add($factory -> createInput(array(
'name' => 'meta_description',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' => 150,
),
),
),
)));
$inputFilter -> add($factory -> createInput(array(
'name' => 'meta_keywords',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' => 150,
),
),
),
)));
It is not validating. How to validate InputFilter?
When creating Fieldsets you need to extend \Zend\Form\Fieldset. Try creating a fieldset like this:
<?php
/**
* Fieldset Example
*/
namespace Module\Form;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class NewFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct()
{
// we want to ignore the name passed and give it our own
parent::__construct('appointment');
// Use the model you are associating with your fieldset here
$this->setHydrator(new ClassMethodsHydrator(false))
->setObject(new MyModel)
->setLabel('My Model');
$this->add(array(
'name' => 'fieldset_item_1',
'type' => 'Select',
'options' => array(
'label' => 'FieldSet Item 1',
)
));
$this->add(array(
'name' => 'fieldset_item_2',
'type' => 'Select',
'options' => array(
'label' => 'FieldSet Item 2',
)
));
}
/**
* Sets up the input filter specification and returns it
*/
public function getInputFilterSpecification()
{
return array(
'fieldset_item_1' => array(
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
),
'fieldset_item_2' => array(
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
),
);
}
}
Then you can use it in your form like this:
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'fieldset',
'options' => array(
'label' => '',
'count' => 1,
'should_create_template' => true,
'allow_add' => true,
'use_as_base_fieldset' => false,
'create_new_objects' => true,
'target_element' => array('type' => 'Module\Form\NewFieldset')
),
));
Now when you perform $form->isValid() in a controller it will validate the fieldset. In addition, you can now make some fancy front-end code to dynamically add and remove fieldsets from the form and they will all get validated.

Codeigniter: Display multiple images from database for

I'm running a real estate website. I'm trying to list all images in the database for a particular listing on their respective page. I have the following code but it just repeats the same default image instead of showing all images for that listing.
Model:
public function getDetails($listing_id)
{
$this->db->select(
'listings.listing_id AS id,
listings.account_id AS account_id,
listings.nbrhood_id AS nbrhood_id,
listings.price AS price,
listings.convertible AS convertible,
listings.available AS available,
images.file_name AS file_name,
listings.sqft AS sqft,
listings.db_tag AS db_tag,
listings.web_id AS web_id,
listings.description AS description,
bedrooms.beds_num AS beds_val,
bedrooms.beds_name AS beds_text,
bathrooms.baths_num AS baths_val,
bathrooms.baths_name AS baths_text,
nbrhoods.nbrhood_name AS nbrhood,
nbrhoods.image AS nbrhood_image,
types_listings.type_name AS type,
pets.pet_name AS pets'
);
$this->db->join('images', 'images.image_id = listings.image_id', 'left');
$this->db->join('bedrooms', 'bedrooms.beds_id = listings.beds_id');
$this->db->join('bathrooms', 'bathrooms.baths_id = listings.baths_id');
$this->db->join('nbrhoods', 'nbrhoods.nbrhood_id = listings.nbrhood_id');
$this->db->join('types_listings', 'types_listings.type_id = listings.type_id');
$this->db->join('pets', 'pets.pet_id = listings.pet_id');
$this->db->where('listings.listing_id', $listing_id);
$this->db->limit(1);
return $this->db->get($this->table_name)->row();
}
PHP:
<?php foreach ($listing AS $listing_id): ?>
<li data-image-id="<?=$image -> image_id ?>" class="uploaded-image" id="image_<?=$image -> image_id ?>">
<div class="sortableimagewrapper">
<!-- <span class="label label-inverse image-name"> </span> -->
<img src="<?=images_url('110x68/' . $listing -> file_name) ?>" alt="" width="110px" height="68px" class="img-polaroid" />
</div>
</li>
<?php endforeach; ?>
class Listing_Model extends CW_Model {
protected $table_name = 'listings';
protected $order_by = 'listing_id';
protected $primary_key = 'listing_id';
protected $display_val = '';
public $defaults = array(
'status_id' => 1
);
public $qualifiers = array('listing_id', 'account_id', 'status_id', 'image_id', 'file_name', 'featured', 'feature_id', 'deal_id', 'nbrhood_id', 'beds_id', 'baths_id', 'pet_id', 'type_id', 'owner_id', 'web_id', 'price', 'convertible', 'available', 'sqft', 'address', 'unit', 'zip', 'description', 'notes');
public $rules = array(
'new' => array(
'listing_id' => array(
'field' => 'listing_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'account_id' => array(
'field' => 'account_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'status_id' => array(
'field' => 'status_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'image_id' => array(
'field' => 'image_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'file_name' => array(
'field' => 'file_name',
'label' => '',
'rules' => 'trim|xss_clean'
),
'featured' => array(
'field' => 'featured',
'label' => '',
'rules' => 'trim|xss_clean'
),
'feature_id' => array(
'field' => 'feature_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'deal_id' => array(
'field' => 'deal_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'nbrhood_id' => array(
'field' => 'nbrhood_id',
'label' => 'Neighborhood',
'rules' => 'trim|required|xss_clean'
),
'beds_id' => array(
'field' => 'beds_id',
'label' => 'Bedrooms',
'rules' => 'trim|required|xss_clean'
),
'baths_id' => array(
'field' => 'baths_id',
'label' => 'Bathrooms',
'rules' => 'trim|required|xss_clean'
),
'pet_id' => array(
'field' => 'pet_id',
'label' => 'Pets',
'rules' => 'trim|required|xss_clean'
),
'type_id' => array(
'field' => 'type_id',
'label' => 'Listing Type',
'rules' => 'trim|required|xss_clean'
),
'owner_id' => array(
'field' => 'owner_id',
'label' => 'Owner/Landlord',
'rules' => 'trim|required|xss_clean'
),
'web_id' => array(
'field' => 'web_id',
'label' => 'Web ID',
'rules' => 'trim|alpha_dash|xss_clean'
),
'price' => array(
'field' => 'price',
'label' => 'Price',
'rules' => 'trim|required|min_length[3]|integer|xss_clean'
),
'convertible' => array(
'field' => 'convertible',
'label' => 'Convertible',
'rules' => 'trim|is_natural|xss_clean'
),
'dateAvailable' => array(
'field' => 'dateAvailable',
'label' => '',
'rules' => 'trim|xss_clean'
),
'available' => array(
'field' => 'available',
'label' => 'Date Available',
'rules' => 'trim|required|xss_clean'
),
'sqft' => array(
'field' => 'sqft',
'label' => 'Square Feet',
'rules' => 'trim|min_length[3]|integer|xss_clean'
),
'address' => array(
'field' => 'address',
'label' => 'Street Address',
'rules' => 'trim|required|min_length[12]|xss_clean'
),
'unit' => array(
'field' => 'unit',
'label' => 'Unit Number',
'rules' => 'trim|required|max_length[5]|alpha_dash|xss_clean'
),
'zip' => array(
'field' => 'zip',
'label' => 'Zip Code',
'rules' => 'trim|required|exact_length[5]|integer|xss_clean'
),
'description' => array(
'field' => 'description',
'label' => 'Description',
'rules' => 'trim|required|min_length[175]|xss_clean'
),
'notes' => array(
'field' => 'notes',
'label' => 'Notes',
'rules' => 'trim|xss_clean'
),
'amenity_ids[unit][]' => array(
'field' => 'amenity_ids[unit][]',
'label' => 'Unit Amenities',
'rules' => 'trim|xss_clean'
),
'amenity_ids[property][]' => array(
'field' => 'amenity_ids[property][]',
'label' => 'Property Amenities',
'rules' => 'trim|xss_clean'
),
'image_ids[]' => array(
'field' => 'image_ids[]',
'label' => '',
'rules' => 'trim|xss_clean'
)
),
'edit' => array(
'listing_id' => array(
'field' => 'listing_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'account_id' => array(
'field' => 'account_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'status_id' => array(
'field' => 'status_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'image_id' => array(
'field' => 'image_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'file_name' => array(
'field' => 'file_name',
'label' => '',
'rules' => 'trim|xss_clean'
),
'featured' => array(
'field' => 'featured',
'label' => '',
'rules' => 'trim|xss_clean'
),
'feature_id' => array(
'field' => 'feature_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'deal_id' => array(
'field' => 'deal_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'nbrhood_id' => array(
'field' => 'nbrhood_id',
'label' => 'Neighborhood',
'rules' => 'trim|required|xss_clean'
),
'price' => array(
'field' => 'price',
'label' => 'Price',
'rules' => 'trim|required|min_length[3]|integer|xss_clean'
),
'beds_id' => array(
'field' => 'beds_id',
'label' => 'Bedrooms',
'rules' => 'trim|required|xss_clean'
),
'convertible' => array(
'field' => 'convertible',
'label' => 'Convertible',
'rules' => 'trim|is_natural|xss_clean'
),
'baths_id' => array(
'field' => 'baths_id',
'label' => 'Bathrooms',
'rules' => 'trim|required|xss_clean'
),
'dateAvailable' => array(
'field' => 'dateAvailable',
'label' => '',
'rules' => 'trim|xss_clean'
),
'available' => array(
'field' => 'available',
'label' => 'Date Available',
'rules' => 'trim|required|xss_clean'
),
'pet_id' => array(
'field' => 'pet_id',
'label' => 'Pets',
'rules' => 'trim|required|xss_clean'
),
'sqft' => array(
'field' => 'sqft',
'label' => 'Square Feet',
'rules' => 'trim|min_length[3]|integer|xss_clean'
),
'type_id' => array(
'field' => 'type_id',
'label' => 'Listing Type',
'rules' => 'trim|required|xss_clean'
),
'description' => array(
'field' => 'description',
'label' => 'Description',
'rules' => 'trim|required|min_length[175]|xss_clean'
),
'amenity_ids[unit][]' => array(
'field' => 'amenity_ids[unit][]',
'label' => 'Unit Amenities',
'rules' => 'trim|xss_clean'
),
'amenity_ids[property][]' => array(
'field' => 'amenity_ids[property][]',
'label' => 'Property Amenities',
'rules' => 'trim|xss_clean'
),
'owner_id' => array(
'field' => 'owner_id',
'label' => 'Owner/Landlord',
'rules' => 'trim|required|xss_clean'
),
'address' => array(
'field' => 'address',
'label' => 'Street Address',
'rules' => 'trim|required|min_length[12]|xss_clean'
),
'unit' => array(
'field' => 'unit',
'label' => 'Unit Number',
'rules' => 'trim|required|max_length[5]|alpha_dash|xss_clean'
),
'zip' => array(
'field' => 'zip',
'label' => 'Zip Code',
'rules' => 'trim|required|exact_length[5]|integer|xss_clean'
),
'notes' => array(
'field' => 'notes',
'label' => 'Notes',
'rules' => 'trim|xss_clean'
),
'image_ids[]' => array(
'field' => 'image_ids[]',
'label' => '',
'rules' => 'trim|xss_clean'
),
'web_id' => array(
'field' => 'web_id',
'label' => 'Web ID',
'rules' => 'trim|alpha_dash|xss_clean'
)
)
);
public function __construct() {
parent::__construct();
}
public function getNew() {
// Remember everytime you add a field you have to add it to form validation...
$listing = new stdClass();
$listing->listing_id = $this->_get_identifier();
$listing->account_id = $this->session->userdata('account_id');
$listing->status_id = 0;
$listing->feature_id = '';
$listing->image_id = 0;
$listing->nbrhood_id = ''; // Dropdown
$listing->beds_id = ''; // Dropdown
$listing->baths_id = ''; // Dropdown
$listing->pet_id = ''; // Dropdown
$listing->type_id = ''; // Dropdown
$listing->owner_id = ''; // Dropdown
$listing->web_id = 'CWA' . random_string('numeric', 7); // varchar
$listing->featured = ''; // bool
$listing->deal_id = '0'; // bool
$listing->price = ''; // int
$listing->convertible = ''; // bool
$listing->dateAvailable = ''; // Only to populate visible jQuery datapicker
$listing->available = ''; // date
$listing->sqft = ''; // int
$listing->address = ''; // varchar
$listing->unit = ''; // varchar
$listing->description = ''; // text
$listing->zip = ''; // int
$listing->notes = ''; // text
$listing->amenity_ids['unit'] = array(); // Dropdown
$listing->amenity_ids['property'] = array(); // Dropdown
$listing->image_ids = array();
return $listing;
}
I don't know about your data model. You should post it here. If your data model has been done correctly, then this is the line that would change:
$this->db->join('images', 'images.image_id = listings.image_id', 'left');
to
$this->db->join('images', 'images.listing_id = listings.id');
You should store the listing ID in the Images table rather than the other way round.
EDIT:
From data model, I mean database structure. If there are n images for every single 'listing', then you need to store a reference to the 'listing ID' in the 'images' table.
Took over this project from someone else. Where would I add fields to the image table. Right now in CMS it shows multiple images for a listing. The code is:
<?php foreach ($images as $image): ?>
<li data-image-id="<?=$image -> image_id ?>" class="uploaded-image" id="image_<?=$image -> image_id ?>">
<div class="sortableimagewrapper">
<!-- <span class="label label-inverse image-name"> </span> -->
<img src="<?=images_url('110x68/' . $image -> file_name) ?>" alt="" width="110px" height="68px" class="img-polaroid" />
<div class="btn-group">
<button class="btn <?php
if ($image -> image_id == $listing -> image_id)
echo 'btn-warning';
?> image-default"><span class="icon-star <?php
if ($image -> image_id == $listing -> image_id)
echo 'icon-white';
?>"></span></button>
<button class="btn image-remove"><span class="icon-remove"></span></button>
</div>
</div>
</li>
<?php endforeach; ?>
</ol>

Resources