PHP Developer

Showing posts with label category. Show all posts
Showing posts with label category. Show all posts

Thursday, 15 September 2016

Wordpress Custom Category Template WIth Custom Pagination

Wordpress Custom Category Template WIth Custom Pagination


1) Create new file for custom taxonomy and upload in wordpress theme taxonomy-(taxonomy-name).php
    Example : Id Taxonomy name is "vacancies_uts". Then file name is "taxonomy-vacancies_uts.php".


2) Add this code in functions.php file for pagination anywhere in website

 
<?php
/************ Pagination Function START *************/
function wp_pagination() {
    global $wp_query;
    $big = 12345678;
    $page_format = paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages,
        'type'  => 'array'
    ) );
    if( is_array($page_format) ) {
        $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
        echo '<div><ul>';
        echo '<li><span>'. $paged . ' of ' . $wp_query->max_num_pages .'</span></li>';
        foreach ( $page_format as $page ) {
                echo "<li>$page</li>";
        }
       echo '</ul></div>';
    }
}
/************ Pagination Function END *************/
?>
   
3) Add below code in "taxonomy-vacancies_uts.php" and upload in activated theme.

<?php
/**
 * The template for displaying all pages.
 *
*/


get_header(); ?>
<?php

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$term_name = $queried_object->name;
?>
<div id="content" class="content" role="main">

<?php
$today = date('m/d/Y');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts(array(
//'posts_per_page' => 2,
'post_type' => 'vacancies', //Post Type
'post_status' => 'publish', //Post Status
'tax_query' => array(
    array(
        'taxonomy' => 'vacancies_uts', //Texonomy Name
        'field' => 'id',
        'terms' => $term_id
    )
),
'meta_query' => array(
    array(
        'key' => 'last_date',  //Meta Field name for compare
        'value' => $today,     //Meta Field Value compare with this value
        'compare' => '>='
    )
),
'paged' => $paged
)
);
?>
<?php if ( have_posts() ) : ?>
<div class="cat_heading"><div class="cat_name"><?php echo $term_name;?></div></div>
<table class="vacancies_tables">
<?php while ( have_posts() ) : the_post(); ?>
    <?php $vacancies_elgible = get_the_terms( get_the_ID(), 'vacancies_elgible' );?>
    <tr>
        <td class="vac_title" data-label="Exam"><a href="<?php echo get_the_permalink();?>"><?php echo get_the_title();?></a></td>
        <td class="detail_link" data-label="Details"><a class="home_viewmore" href="<?php echo get_the_permalink();?>">View More</a></td>
    </tr>
<?php endwhile; ?>
</table>
<div class="custom_pagination"><?php wp_pagination(); ?></div>
<?php else : ?>

<?php //get_template_part( 'no-results', 'page' ); ?>
<?php echo "Sorry, no vacancies available under this Category."; ?>

<?php endif;?>

</div><!-- #content -->

<?php get_footer(); ?>

Tuesday, 3 May 2016

How to create category from frontend in Wordpress



How to create category from frontend in Wordpress (Also for custom taxonomy)


<?php
$custom_texonmy = 'services_categories';   // Custom texonomy type slug
$job_category = 'Access Covers & Gratings';  //Category name
$cat = get_term_by('name',htmlspecialchars($job_category),'services_categories');  //get category by name
if($cat){
    //This code run id category with above name already exist
    $cat_ID = $cat->term_id; //term_id of category
    $job_type = $cat->slug; //Slug of category
}

if($cat_ID == 0) {
    //This code run id category with above name not exist and create new category
    $cat_name = $job_category;
    $cat_insert = wp_insert_term(
        $cat_name,
        $custom_texonmy
    );
    $cat_ID = $cat_insert['term_id'];
    if($cat_ID != 0){
        $new_cat = get_term_by( 'term_id', $cat_ID, $custom_texonmy );
        if($new_cat){
            $job_type = $new_cat->slug;
        }
    }
} else {
    $cat_ID;
    $job_type = $job_type;
}

?>

How to assign category or “custom taxonomy” category to post from front end



How to assign category or “custom taxonomy” category to post from front end


<?php
$custom_texonmy = 'services_categories';  // Custom texonomy type slug
$job_category = 'Access Covers & Gratings';  //Category name
$post_id = $post_id; //Post id for which assign above category
$cat = get_term_by('name',htmlspecialchars($job_category),$custom_texonmy);  //get category by name
if($cat){
    $cat_ID = $cat->term_id;  // term_id of category
    $job_type = $cat->slug;  //Slug of category
}
if ($job_type) {
    $services_categories = wp_set_object_terms($post_id, $job_type, $custom_texonmy );  //This code assign category of custom texonomy to post from front end
}

?>

Monday, 14 December 2015

Get product category list in woocommerce Wordpress

Get product category list in woocommerce :

<?php
echo "<div class='product_cat_sidebar'>";
echo "<h4>Wholesale Categories</h4>";
$taxonomy     = 'product_cat';
  $orderby      = 'ID';
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 1;      // 1 for yes, 0 for no
  $title        = '';
  $empty        = 0;

  $args = array(
         'taxonomy'     => $taxonomy,
         'orderby'      => $orderby,
         'order'      => "asc",
         'show_count'   => $show_count,
         'pad_counts'   => $pad_counts,
         'hierarchical' => $hierarchical,
         'title_li'     => $title,
         'hide_empty'   => $empty
  );
$all_categories = get_categories( $args );
echo "<ul class='sidebar_cat'>";
foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;
        if(isset($_GET["product_cat"]) && $cat->slug == $_GET["product_cat"]){
            echo '<li class="active_category"><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';
        } else {
            echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';
        }
      

        $args2 = array(
                'taxonomy'     => $taxonomy,
                'child_of'     => 0,
                'parent'       => $category_id,
                'orderby'      => $orderby,
                'show_count'   => $show_count,
                'pad_counts'   => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li'     => $title,
                'hide_empty'   => $empty
        );
        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
                echo  $sub_category->name ;
            } 
        }
    }
}
echo "</ul>";
echo "</div>";

?>

Saturday, 25 October 2014

How to Display Recent Posts From A Specific Category In WordPress

How to Display Recent Posts From A Specific Category In WordPress


<?php
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li>
    <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>


At the top where it says showpost= change the number to how many posts
you want to display, and cat=3 is the id of the category, so change the
ID of the category to pick which category will you be displaying.