Search

Alves Singh

Month

August 2015

Template for custom taxonomy with pagination

Template for custom taxonomy with pagination:

file name: taxonomy-plants-category.php

<?php

/**

* Archive template for plants category taxonomy

*/

get_header();

get_sidebar();

$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;

$posts_per_page = get_option(‘posts_per_page ‘);

$category = single_cat_title(”, false);

if(isset($_REQUEST[‘filter’]) && !empty($_REQUEST[‘filter’])){

$args = array(

‘post_title_like’ => $_REQUEST[‘filter’],

‘paged’ => $paged,

‘posts_per_page’ => $posts_per_page,

‘post_type’ => ‘plants’,

‘plants-category’ => $category,

‘orderby’ => ‘title’,

‘order’ => ‘ASC’

);

}else{

$args = array(

‘paged’ => $paged,

‘posts_per_page’ => $posts_per_page,

‘post_type’ => ‘plants’,

‘plants-category’ => $category,

‘orderby’ => ‘title’,

‘order’ => ‘ASC’

);

}

query_posts( $args );

$totalRecords = $wp_query->found_posts;

$totalPages = ceil($totalRecords / $posts_per_page);

$totalColList = ceil($posts_per_page / 2);

?>

foreach (range(‘A’, ‘Z’) as $char) {

echo ‘‘.$char.’‘;

}

?>

$glossaryWord = ”;

$counter = 0;

$i = 1;

while ( have_posts() ) : the_post();

$title = get_the_title();

$desc = get_the_content();

$firestWord = substr($title, 0, 1);

if($firestWord != $glossaryWord){

$glossaryWord = $firestWord;

$counter = 0;

}

//echo $i.’ % ‘.$totalColList .’=’. $i % $totalColList.”
“;

echo ( ($i % $totalColList == 1) ? ‘

    ‘ : ”);

    echo ($counter == 0 ? ‘‘.$glossaryWord.’‘ : ”);

    echo (‘

  • ‘’.$title.’’
  • ‘);

    echo ( ($i % $totalColList == 0) ? ‘

‘ : ”);

$counter++;

$i++;

endwhile;

?>

<?php else: ?>

<p>No record found.</p>

<?php endif; ?>

</div>

</div>

</div>

<?php

get_footer();

Code Download here: custome-taxonomy-page-with-pagination

Custom post type and taxonomy

Register post type and taxonomy

// Custom post for plants

function plants_custom_init() {

$labels = array(

‘name’ => _x(‘Plants’, ‘post type general name’),

‘singular_name’ => _x(‘plants’, ‘post type singular name’),

‘add_new’ => _x(‘Add New’, ‘book’),

‘add_new_item’ => __(‘Add New Plants’),

‘edit_item’ => __(‘Edit Plants’),

‘new_item’ => __(‘New Plants’),

‘all_items’ => __(‘All Plants’),

‘view_item’ => __(‘View Plants’),

‘search_items’ => __(‘Search Plants’),

‘not_found’ => __(‘No plants items found’),

‘not_found_in_trash’ => __(‘No plants items found in Trash’),

‘parent_item_colon’ => ”,

‘menu_name’ => __(‘Plants’)

);

$args = array(

‘labels’ => $labels,

‘public’ => true,

‘publicly_queryable’ => true,

‘show_ui’ => true,

‘show_in_menu’ => true,

‘query_var’ => “plants”,

‘rewrite’ => array( ‘slug’ => ‘plants’),

‘capability_type’ => ‘post’,

‘has_archive’ => true,

‘hierarchical’ => false,

‘menu_position’ => 4,

‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’ )

);

register_post_type(‘plants’, $args);

$labels = array(

‘name’ => _x( ‘Plants Categories’, ‘taxonomy general name’ ),

‘singular_name’ => _x( ‘Plants Category’, ‘taxonomy singular name’ ),

‘search_items’ => __( ‘Search Plants Categories’ ),

‘all_items’ => __( ‘All Plants Categories’ ),

‘edit_item’ => __( ‘Edit Plants Category’ ),

‘update_item’ => __( ‘Update Plants Category’ ),

‘add_new_item’ => __( ‘Add New Plants Category’ ),

‘new_item_name’ => __( ‘New Plants Category Name’ ),

‘menu_name’ => __( ‘Categories’ ),

);

register_taxonomy(‘plants-category’,

array(‘plants’, ‘post’),

array(

‘hierarchical’ => true,

‘labels’ => $labels,

‘show_ui’ => true,

‘query_var’ => true,

‘rewrite’ => array( ‘slug’ => ‘plants-cat’ ),

));

flush_rewrite_rules( false );

}

add_action( ‘init’, ‘plants_custom_init’ );

// add custom column in plants custom post type

add_filter(‘manage_plants_posts_columns’, ‘plants_category_columns_head’);

function plants_category_columns_head($defaults) {

$defaults[‘plants_category’] = ‘Category’;

return $defaults;

}

//Add rows data

add_action(‘manage_plants_posts_custom_column’, ‘manage_plants_columns_content’, 10, 2);

function manage_plants_columns_content($column_name, $post_id) {

if ($column_name == ‘plants_category’) {

$categories = get_the_terms($post_id, “plants-category”);

//echo”<pre>”;print_r($categories);echo”</pre>”;

$catName = ”;

if ($categories) {

foreach($categories as $cat){

$catName .= ($catName !=” ? “<br>”.$cat->name : $cat->name);

}

}

echo $catName;

}

}

// Make these columns sortable

add_filter( ‘manage_edit-plants_sortable_columns’, ‘palnts_category_sortable_column’ );

function palnts_category_sortable_column( $columns ) {

$columns[‘plants_category’] = ‘plants-cat’;

//To make a column ‘un-sortable’ remove it from the array

//unset($columns[‘date’]);

return $columns;

}

Code Download here: custom-post-type-and-taxonomy

Prestashop module

Preastashop module with add, edit, delete and tab using controller and classes

 

Code here: Download