Pages

Wednesday, September 25, 2013

wp_get_archives highlight current archive



function pc_theme_get_archives_link ( $link_html ) {
    global $wp;
    static $current_url;
    if ( empty( $current_url ) ) {
        $current_url = add_query_arg( $_SERVER['QUERY_STRING'], '', home_url( $wp->request ) );
    }
    if ( stristr( $link_html, $current_url ) !== false ) {
        $link_html = preg_replace( '/(<[^\s>]+)/', '\1 class="active"', $link_html, 1 );
    }
    return $link_html;
}
add_filter('get_archives_link', 'pc_theme_get_archives_link');

Monday, September 16, 2013

jQuery scrolling dynamic content Box


jQuery(document).ready(function($){
$(window).scroll(function() {
var windowHeight = $(window).height();
var documentHeight = $(document).height();
var footerHeight = $(".footer-container").height();
if($(window).scrollTop() >= ($(document).height() - $(window).height() - footerHeight)) {
if($(".news_listing .news_showmore").length == 1){
if($(".news_listing .news_showmore").hasClass("hasloader") == false){
$(".news_listing .news_showmore").addClass("hasloader");
$(".loadmoreimg").show();
/* Write your ajax code here*/
}
}
}
});
});

Wednesday, September 11, 2013

Recent posts widget, with limited text and 'read more' link - wordpress - [resolved]

Please write below code in functions.php


add_action( 'widgets_init', 'switch_recent_posts_widget' );

function switch_recent_posts_widget() {

    unregister_widget( 'WP_Widget_Recent_Posts' );
    register_widget( 'WP_Widget_Recent_Posts_Truncated' );

}

class WP_Widget_Recent_Posts_Truncated extends WP_Widget {

    function __construct() {
        $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site") );
        parent::__construct('recent-posts', __('Recent Posts'), $widget_ops);
        $this->alt_option_name = 'widget_recent_entries';

        add_action( 'save_post', array(&$this, 'flush_widget_cache') );
        add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
        add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
    }

    function widget($args, $instance) {
        $cache = wp_cache_get('widget_recent_posts', 'widget');

        if ( !is_array($cache) )
            $cache = array();

        if ( isset($cache[$args['widget_id']]) ) {
            echo $cache[$args['widget_id']];
            return;
        }

        ob_start();
        extract($args);

        $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
        if ( ! $number = absint( $instance['number'] ) )
            $number = 10;

        $r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true));
        if ($r->have_posts()) :
?>
        <?php echo $before_widget; global $post ?>
        <?php if ( $title ) echo $before_title . $title . $after_title; ?>
        <ul>
        <?php  while ($r->have_posts()) : $r->the_post(); ?>
        <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
            <?php
echo (strlen(get_the_title()) > 25)?substr(get_the_title(),0,25).'...':get_the_title();
            ?>
        </a></li>
        <?php endwhile; ?>
        </ul>
        <?php echo $after_widget; ?>
<?php
        // Reset the global $the_post as this query will have stomped on it
        wp_reset_postdata();

        endif;

        $cache[$args['widget_id']] = ob_get_flush();
        wp_cache_set('widget_recent_posts', $cache, 'widget');
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['number'] = (int) $new_instance['number'];
        $this->flush_widget_cache();

        $alloptions = wp_cache_get( 'alloptions', 'options' );
        if ( isset($alloptions['widget_recent_entries']) )
            delete_option('widget_recent_entries');

        return $instance;
    }

    function flush_widget_cache() {
        wp_cache_delete('widget_recent_posts', 'widget');
    }

    function form( $instance ) {
        $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
        $number = isset($instance['number']) ? absint($instance['number']) : 5;
?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>

        <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
        <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<?php
    }
}

Saturday, September 7, 2013

Woocommerce Products show on homepage with pagination

Please first install "WP-Pagenavi" plugin to your store.
and then write below code to your home page template.

<?php
                // Setup your custom query
                global $paged;
                if( get_query_var( 'paged' ) )
                $my_page = get_query_var( 'paged' );
                else {
                if( get_query_var( 'page' ) )
                $my_page = get_query_var( 'page' );
                else
                $my_page = 1;
                set_query_var( 'paged', $my_page );
                $paged = $my_page;
                }
                $args = array( 'post_type' => 'product', 'paged' => $my_page, 'posts_per_page' => 8 );
                $loop = new WP_Query( $args );
                //print_r($loop);
                echo '<ul>';
                while ( $loop->have_posts() ) : $loop->the_post();
                   
                    echo '<li class="product">';
                            echo '<a class="prod_image" href="'.esc_url( get_permalink( $post->ID ) ).'" title="'.esc_attr($post->post_title ? $post->post_title : $post->ID).'">';
                            if ( has_post_thumbnail($post->ID) ) {
                                echo get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
                            } else {
                                echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
                            }
                            echo '</a>';                               
                            if(get_post_meta( $post->ID, '_regular_price', true) != ''){
                                echo '<span class="price"><span class="amount">$'.number_format(get_post_meta( $post->ID, '_regular_price', true), 2, '.', '').'</span></span>';
                            } else {
                                echo '<span class="price"><span class="amount">&nbsp;</span></span>';
                            }
                            echo '<p>'.substr(strip_tags($post->post_content),0,25).'...</p>';
                            echo '<div class="info"><a href="'.get_permalink().'">INFO</a></div>';
                       
                    echo '</li>';
                endwhile;
                wp_pagenavi(array( 'query' => $loop ));
                wp_reset_query();
                ?>

Friday, September 6, 2013

[resolved] WP-PageNavi with custom query and $paged variable


<?php

global $paged;
if( get_query_var( 'paged' ) )
$my_page = get_query_var( 'paged' );
else {
if( get_query_var( 'page' ) )
$my_page = get_query_var( 'page' );
else
$my_page = 1;
set_query_var( 'paged', $my_page );
$paged = $my_page;
}

$jobquery = new WP_Query( array( 'post_status' => 'publish', 'post_type' => 'job', 'paged' => $my_page )  );
if(count($jobquery->posts) > 0){
foreach($jobquery->posts as $jpost) {

?> <header class="entry-header">
<h1 class="entry-title"><?php echo $jpost->post_title; ?></h1>
</header><!-- .entry-header -->

<div class="entry-content">
<?php echo $jpost->post_content; ?>
</div><!-- .entry-content -->

<?php } wp_pagenavi( array( 'query' => $jobquery ) ); ?>
<?php }  wp_reset_query(); ?>

Thursday, September 5, 2013

How to Add Placeholder Text in Gravity Forms


To do that, you need to open your theme’s functions.php file and paste the following code:

/* Add a custom field to the field editor (See editor screenshot) */
add_action("gform_field_standard_settings", "my_standard_settings", 10, 2);

function my_standard_settings($position, $form_id){
// Create settings on position 25 (right after Field Label)
if($position == 25){
?>
<li class="admin_label_setting field_setting" style="display: list-item; ">
<label for="field_placeholder">Placeholder Text<!-- Tooltip to help users understand what this field does -->
<a href="javascript:void(0);" class="tooltip tooltip_form_field_placeholder" tooltip="&lt;h6&gt;Placeholder&lt;/h6&gt;Enter the placeholder/default text for this field.">(?)</a>
</label>
<input type="text" id="field_placeholder" class="fieldwidth-3" size="35" onkeyup="SetFieldProperty('placeholder', this.value);">
</li>
<?php
}
}

/* Now we execute some javascript technicalitites for the field to load correctly */
add_action("gform_editor_js", "my_gform_editor_js");

function my_gform_editor_js(){
?>
<script>
jQuery(document).bind("gform_load_field_settings", function(event, field, form){
jQuery("#field_placeholder").val(field["placeholder"]);
});
</script>
<?php
}

/* We use jQuery to read the placeholder value and inject it to its field */
add_action('gform_enqueue_scripts',"my_gform_enqueue_scripts", 10, 2);
function my_gform_enqueue_scripts($form, $is_ajax=false){
?>
<script>
jQuery(function(){
<?php
/* Go through each one of the form fields */
foreach($form['fields'] as $i=>$field){
/* Check if the field has an assigned placeholder */
if(isset($field['placeholder']) && !empty($field['placeholder'])){
/* If a placeholder text exists, inject it as a new property to the field using jQuery */
?>
jQuery('#input_<?php echo $form['id']?>_<?php echo $field['id']?>').attr('placeholder','<?php echo $field['placeholder']?>');
<?php
}
}
?>
});
</script>
<?php
}

Reference URL: http://www.wpbeginner.com/wp-tutorials/how-to-add-placeholder-text-in-gravity-forms/

Wednesday, September 4, 2013

Wordpress get menu list


$menu_name = 'primary';

    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );

$menu_items = wp_get_nav_menu_items($menu->term_id);

$menu_list = '<ul id="menu-' . $menu_name . '">';

foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
}
$menu_list .= '</ul>';
    } else {
$menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
    }
echo $menu_list;

Tuesday, September 3, 2013

How To: Enable the Use of Sessions On Your WordPress - [Resolved]


If you want to activate session variables within your WordPress installation the only thing you have to do is call session_start(); before any output is send to the client. Normally upgrading your WordPress installation will replace all files, so we will want to install the code within our site theme to avoid the changes from being lost.

We will add the next lines of code to our functions.php file within our theme:

if ( !session_id() )
add_action( 'init', 'session_start' );



It is best place to add these lines is at the top of functions.php, immediately after the php start tag (<?php).