Pages

Thursday, December 19, 2013

Add custom class to wordpress navigation

Please write below code in functions.php

add_filter('nav_menu_css_class', 'current_type_nav_class', 10, 2);
function current_type_nav_class($classes, $item) {
// Get post_type for this post
// Go to Menus and add a menu class named: {custom-post-type}-menu-item
// This adds a current_page_parent class to the parent menu item
if( in_array( $post_type.'-menu-item', $classes ) )
array_push($classes, 'current_page_parent');

return $classes;

}

Generate re-write rules for custom post type - wordpress

Please write below code in functions.php

function wp_rewrite_rules($wp_rewrite) {
    $rules = wp_generate_date_archives('CUSTOM_POST_TYPE_SLUG', $wp_rewrite);
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
    return $wp_rewrite;
}

add_action('generate_rewrite_rules', 'wp_rewrite_rules');
function wp_generate_date_archives($postslug, $wp_rewrite) {
    $rules = array();

    $post_type = get_post_type_object($postslug);
    $slug_archive = $post_type->has_archive;
    if ($slug_archive === false) return $rules;
    if ($slug_archive === true) {
        $slug_archive = $post_type->name;
    }

    $dates = array(
        array(
            'rule' => "([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})",
            'vars' => array('year', 'monthnum', 'day')),
        array(
            'rule' => "([0-9]{4})/([0-9]{1,2})",
            'vars' => array('year', 'monthnum')),
        array(
            'rule' => "([0-9]{4})",
            'vars' => array('year')),
        array(
            'rule' => "([^/]+)",
            'vars' => array('name'))
        );

    foreach ($dates as $data) {
        $query = 'index.php?post_type='.$postslug;
        $rule = $slug_archive.'/'.$data['rule'];

        $i = 1;
        foreach ($data['vars'] as $var) {
            $query.= '&'.$var.'='.$wp_rewrite->preg_index($i);
            $i++;
        }
        $rules[$rule."/?$"] = $query;
        $rules[$rule."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
        $rules[$rule."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
        $rules[$rule."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i);
    }
    return $rules;
}

Create yearly archive listing for custom post type + wordpress - solved


Please write below code in functions.php

function get_cpt_archives( $cpt, $echo = false ) // $cpt = custom post type slug
{
    global $wpdb;
    $sql = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' GROUP BY YEAR(wp_posts.post_date), MONTH(wp_posts.post_date) ORDER BY wp_posts.post_date DESC", $cpt);
    $results = $wpdb->get_results($sql);

    if ( $results )
    {
        $archive = array();
        foreach ($results as $r)
        {
            $year = date('Y', strtotime( $r->post_date ) );
            $month = date('F', strtotime( $r->post_date ) );
            $month_num = date('m', strtotime( $r->post_date ) );
            $link = get_bloginfo('siteurl') . '/corporate/' . $cpt . '/' . $year . '/' . $month_num;
            $this_archive = array( 'month' => $month, 'year' => $year, 'link' => $link );
            array_push( $archive, $this_archive );
        }

        if( !$echo )
            return $archive;
        foreach( $archive as $a )
        {
            echo '<li><a href="' . $a['link'] . '">' . $a['month'] . ' ' . $a['year'] . '</a></li>';
        }
    }
    return false;
}

In sidebar page you have to write below function:

get_cpt_archives('Custom Post Type Slug');

Wordpress child page redirect to custom template - solved

I want to change template for all child page of news page.

For this I have write below code in functions.php

function switch_corporate_child_service_page_template() {
    global $post;
    if (is_page())
{
// Checks if page is parent, if yes, return
if ($post->post_parent == 0)
return true;
else if ($post->post_parent == '20064') // "20064" is "News" page id.
{
$template = TEMPLATEPATH . "/page-corporate_services.php";
if (file_exists($template)) {
load_template($template);
exit;
}
}
}
}


add_action('template_redirect','switch_corporate_child_service_page_template');

Custom post type redirect to home page + wordpress

Please write below code in functions.php

add_action("template_redirect", 'template_redirect_news');

function template_redirect_news()
{
global $wp;
$custom_post_types = array("faq" , "testimonial"); // please write here which custom post type you want to redirect to home page

if (in_array($wp->query_vars["post_type"], $custom_post_types))
{
if($wp->query_vars["name"]):
wp_redirect(get_site_url());
die();
endif;
}
}


Tuesday, December 10, 2013

How to change Custom Taxonomy Links?


function custom_term_link_url($content) {
    $current_path = 'live-topics';
    $new_path = 'live/topic';
    $content = str_replace($current_path, $new_path, $content);
    return $content;
}
add_filter('term_link', 'custom_term_link_url');

function add_query_vars_for_live_topics($aVars) {
    $aVars[] = "live-topics";    // represents the name of the product category as shown in the URL
    return $aVars;
}
add_filter('query_vars', 'add_query_vars_for_live_topics');

function add_rewrite_rules_live_topics($aRules) {
    $aNewRules = array('live/topic/([^/]+)/?$' => 'index.php?live-topics=$matches[1]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules_live_topics');