All · Bootstrap · Google Analytics · Sage · Uncategorised · Wordpress

Custom number of posts per page

function custom_posts_per_page($query) {
if (is_search()) {
$query->set(‘posts_per_page’, 25);
}
} //function

//this adds the function above to the ‘pre_get_posts’ action
add_action(‘pre_get_posts’, ‘custom_posts_per_page’);

Bootstrap carousel fade effect

/* slide fade effect */
 .carousel .carousel-inner .item {
   opacity: 0;
   -webkit-transition-property: opacity;
   -moz-transition-property: opacity;
   -o-transition-property: opacity;
   transition-property: opacity;
 }
 .carousel .carousel-inner .active {
   opacity: 1;
 }
 .carousel .carousel-inner .active.left,
 .carousel .carousel-inner .active.right {
   left: 0;
   opacity: 0;
   z-index: 1;
 }
 .carousel .carousel-inner .next.left,
 .carousel .carousel-inner .prev.right {
   opacity: 1;
 }
 .carousel .carousel-control {
   z-index: 2;
 }
 html,
 body,
 .carousel,
 .carousel-inner,
 .carousel-inner .item {
   height: 100%;
 }

Custom post, custom tax and custom edit columns

//Create Service custom post type
 add_action( 'init', 'create_staff' );
 function create_staff() {
     register_post_type( 'staff',
         array(
             'labels' => array(
                 'name' => 'Staff',
                 'singular_name' => 'Staff',
                 'add_new' => 'Add Staff',
                 'add_new_item' => 'Add New Staff',
                 'edit' => 'Edit',
                 'edit_item' => 'Edit Staff',
                 'new_item' => 'New Staff',
                 'view' => 'View',
                 'view_item' => 'View Staff',
                 'search_items' => 'Search Staff',
                 'not_found' => 'No Staff found',
                 'not_found_in_trash' => 'No Staff found in Trash',
                 'parent' => 'Parent Staff'
             ),
 
             'public' => true,
             'menu_position' => 16,
             'menu_icon' => 'dashicons-groups',
             'supports' => array( 'title', 'editor', 'thumbnail' ),
             'has_archive' => true
         )
     );
 }
 function add_custom_taxonomies() {
     
      // Add new "Locations" taxonomy to Posts
      register_taxonomy('sector', 'staff', array(
           // Hierarchical taxonomy (like categories)
           'hierarchical' => true,
           // This array of options controls the labels displayed in the WordPress Admin UI
           'labels' => array(
                'name' => _x( 'Sector', 'taxonomy general name' ),
                'singular_name' => _x( 'Sector', 'taxonomy singular name' ),
                'search_items' =>  __( 'Search Sectors' ),
                'all_items' => __( 'All Sectors' ),
                'parent_item' => __( 'Parent Sector' ),
                'parent_item_colon' => __( 'Parent Sector:' ),
                'edit_item' => __( 'Edit Sector' ),
                'update_item' => __( 'Update Sector' ),
                'add_new_item' => __( 'Add New Sector' ),
                'new_item_name' => __( 'New Sector' ),
                'menu_name' => __( 'Sectors' ),
           ),
           // Control the slugs used for this taxonomy
           'rewrite' => array(
                'slug' => 'sector', // This controls the base slug that will display before each term
                'with_front' => false, // Don't display the category base before "/locations/"
                'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
           ),
      ));
 }
 add_action( 'init', 'add_custom_taxonomies', 0 );
add_filter( 'manage_edit-staff_columns', 'my_edit_staff_columns' ) ;
 function my_edit_staff_columns( $columns ) {
 
      $columns = array(
           'cb' => '<input type="checkbox" />',
           'title' => __( 'Name' ),
           'role' => __( 'Role' ),
           'sector' => __( 'Sector' ),
           'date' => __( 'Date' )
      );
 
      return $columns;
 }
 add_action( 'manage_staff_posts_custom_column', 'my_manage_staff_columns', 10, 2 );
 
 function my_manage_staff_columns( $column, $post_id ) {
      global $post;
 
      switch( $column ) {
 
           /* If displaying the 'duration' column. */
           case 'role' :
 
                /* Get the post meta. */
                $role = get_post_meta( $post_id, 'role', true );
 
                /* If no duration is found, output a default message. */
                if ( empty( $role ) )
                     echo __( 'Unknown' );
 
                /* If there is a duration, append 'minutes' to the text string. */
                else
                     printf( __( '%s' ), $role );
 
                break;
 
           /* If displaying the 'genre' column. */
           case 'sector' :
 
                /* Get the genres for the post. */
                $terms = get_the_terms( $post_id, 'sector' );
 
                /* If terms were found. */
                if ( !empty( $terms ) ) {
 
                     $out = array();
 
                     /* Loop through each term, linking to the 'edit posts' page for the specific term. */
                     foreach ( $terms as $term ) {
                          $out[] = sprintf( '<a href="%s">%s</a>',
                               esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'sector' => $term->slug ), 'edit.php' ) ),
                               esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'sector', 'display' ) )
                          );
                     }
 
                     /* Join the terms, separating them with a comma. */
                     echo join( ', ', $out );
                }
 
                /* If no terms were found, output a default message. */
                else {
                     _e( 'No Sectors' );
                }
 
                break;
 
           /* Just break out of the switch statement for everything else. */
           default :
                break;
      }
 }

ACF image sized

<?php $image = get_field('image');
 $url = $image['url'];
 $alt = $image['alt'];
 $size = 'thumbnail';
 $thumb = $image['sizes'][ $size ]; ?>
 <img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" />

WordPress stylesheet directory

<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/img/slider-bottom-image.png" alt="" />

Numerical Post Navigation

index.php:
 <?php if ($wp_query->max_num_pages > 1) : ?>
 <nav class="post-nav">
 <ul class="pager">
 <?php
$big = 999999999; // need an unlikely integer
echo 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,
 'show_all' => FALSE, //this will make paginate not to show all links.
 'end_size' => 2, //will show 2 numbers on either the start and the end list edges.
 'mid_size' => 1 //so that you won't have 1,2...,3,...,7,8
 ) );
 ?>
 </ul>
 </nav>
 <?php endif; ?>
app.css:
 .page-numbers.current {
 background-color: #000;
 border: 1px solid #000;
 color: #FFFFFF;
 }
 .page-numbers {
 border: 1px solid #DEDEDE;
 border-radius: 4px;
 color: #000000;
 float: left;
 font-size: 11px;
 margin-right: 9px;
 padding: 2px 9px;
 }
 .page-numbers:hover {
 background-color: #000;
 border: 1px solid #000;
 color: #FFFFFF;
 text-decoration: none;
 }

PHP in widget function

add_filter('widget_text','execute_php',100);
 functionexecute_php($html){
 if(strpos($html,"<"."?php")!==false){
 ob_start();
 eval("?".">".$html);
 $html=ob_get_contents();
 ob_end_clean();
 }
 return$html;
 }