Woocommerce custom product slider shortcode - wordpress

I am working on woocommerce custom product slider shortcode similar to snapshot but I don't know how to use slider with woocommerce. ( https://www.buywholefoodsonline.co.uk/ )
My idea is to use shortcode to call this best selling product slider from database.
I have use this code ( How can I list best selling products in woocommerce ) but did not get similar layout.
I wanted to find a way fit this code in some slider eg. owl carousel ( https://owlcarousel2.github.io/OwlCarousel2/docs/started-installation.html)
I enqueue style and scripts of owl carousel in functions.php.
function loadup_scripts()
{
wp_register_script('jquery', 'https://code.jquery.com/jquery-1.11.0.min.js', NULL, '1.11.0', false);
wp_enqueue_script( 'jquery-migrate', 'http://code.jquery.com/jquery-migrate-1.2.1.min.js', array( 'jquery' ), '', true );
wp_enqueue_script( 'slick-min-js', 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.js', array( 'jquery' ), '', true );
wp_enqueue_style( 'slick-css', 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick-theme.css' );
}
add_action( 'wp_enqueue_scripts', 'loadup_scripts' );
function livewellnutritionuk_action() { ?>
<script>
jQuery(document).ready(function(){
jQuery('.slider').slick({
infinite: false,
speed: 100,
slidesToShow: 4,
slidesToScroll: 4,
autoplay: true,
prevArrow: '<span class="priv_arrow"><</span>',
nextArrow: '<span class="priv_arrow">></span>',
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
infinite: true,
dots: true
}
},
{
breakpoint: 600,
settings: {
slidesToShow: 2,
slidesToScroll: 2
}
},
{
breakpoint: 480,
settings: {
slidesToShow: 1,
slidesToScroll: 1
}
}
]
});
});
</script>
<?php
}
add_action('wp_footer', 'livewellnutritionuk_action');
But need to fit owl carousel for best selling products in this code.
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 1,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
<div>
<a href="<?php the_permalink(); ?>" id="id-<?php the_id(); ?>" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail( $loop->post->ID ))
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="product placeholder Image" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
</a>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

I was struggling to this problem and now I found solution.
function.php
function child_enqueue_styles() {
wp_enqueue_style( 'astra-child-theme-css', get_stylesheet_directory_uri() . '/style.css', array('astra-theme-css'), CHILD_THEME_ASTRA_CHILD_VERSION, 'all' );
wp_enqueue_style('owl-carousel','https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.0.0-beta.2.4/assets/owl.carousel.min.css', '', '','all');
wp_enqueue_style('owl-carousel-2.1.6','https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.0.0-beta.2.4/assets/owl.theme.default.css', '', '','all');
wp_enqueue_script('owl-js','https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2//2.0.0-beta.2.4/owl.carousel.min.js',array('jquery'),'1.12.4', false);
}
add_action( 'wp_enqueue_scripts', 'child_enqueue_styles', 15 );
function livewellnutritionuk_action() { ?>
<script>
jQuery(document).ready(function(){
jQuery('.slider').slick({
infinite: false,
speed: 100,
slidesToShow: 4,
slidesToScroll: 4,
autoplay: true,
prevArrow: '<span class="priv_arrow"><</span>',
nextArrow: '<span class="priv_arrow">></span>',
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
infinite: true,
dots: true
}
},
{
breakpoint: 600,
settings: {
slidesToShow: 2,
slidesToScroll: 2
}
},
{
breakpoint: 480,
settings: {
slidesToShow: 1,
slidesToScroll: 1
}
}
]
});
});
</script>
<?php
}
add_action('wp_footer', 'livewellnutritionuk_action');
function slider_test(){
ob_start();
get_template_part('best_product');
return ob_get_clean();
}
add_shortcode('best_seller','slider_test');
best_product.php
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
);
$loop = new WP_Query( $args );
echo '<div class="owl-carousel">';
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
<div>
<a href="<?php the_permalink(); ?>" id="id-<?php the_id(); ?>" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail( $loop->post->ID ))
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="product placeholder Image" width="65px" height="115px" />'; ?>
<h3 class="woocommerce-loop-product__title"><?php the_title();?></h3>
</a>
<h4><?php echo woocommerce_price($product->get_price());?></h4>
<div class="best_seller_button">
Add to Basket
</div>
<div class="stock_status">
<div class="status_in_stock"> <?php echo $product->get_stock_status();?></div>
<div class="star-rating"> <?php echo $product->get_average_rating();?></div>'
</div>
</div>
<?php endwhile;
echo '</div>';
?>
<?php wp_reset_query(); ?>
<style>
.owl-item > div:after {
font-family: sans-serif;
font-size: 24px;
font-weight: bold;
}
.owl-theme .owl-controls .owl-nav [class*='owl-']{
background: #8bc34a;
padding: 0px 8px;
}
</style>
<script>
jQuery(document).ready(function () {
var owl = jQuery('.owl-carousel');
owl.owlCarousel({
loop:true,
nav:true,
arrows: true,
margin:10,
navText : ["<i class='fa fa-chevron-left'></i>","<i class='fa fa-chevron-right'></i>"],
responsive:{
0:{
items:1
},
600:{
items:3
},
960:{
items:4
},
1200:{
items:4
}
}
});
owl.on('mousewheel', '.owl-stage', function (e) {
if (e.deltaY>0) {
owl.trigger('next.owl');
} else {
owl.trigger('prev.owl');
}
e.preventDefault();
});
});
</script>

Related

Anchor links in a WooCommerce categories widget

I use a code that displays products under each category on the archive page /shop:
Category 1
product 1 product 2 product 3
Category 2
product 1 product 2 product 3
Here is my code:
<?php
foreach( get_terms( array( 'taxonomy' => 'product_cat' ) ) as $category ) :
$products_loop = new WP_Query( array(
'post_type' => 'product',
'showposts' => -1,
'tax_query' => array_merge( array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'terms' => array( $category->term_id ),
'field' => 'term_id'
)
), WC()->query->get_tax_query() ),
'meta_query' => array_merge( array(
// You can optionally add extra meta queries here
), WC()->query->get_meta_query() )
) );
?>
<h2 class="category-title"><?php echo $category->name; ?></h2>
<?php
while ( $products_loop->have_posts() ) {
$products_loop->the_post();
/**
* woocommerce_shop_loop hook.
*
* #hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
wp_reset_postdata(); ?>
<?php endforeach; ?>
I also use a standard widget to display WooCommerce categories. As I understand it, the file is responsible for it - woocommerce / includes / widget / class-wc-widget-product-categories.php.
How can I modify this file (code for functions.php) to add anchor links? For example, in the categories menu, I select Category 2 and the page moves down to Category 2 with its products.
I just can’t find a ready-made solution, so I ask you for help. I hope this code will be useful to other users.
You need to add some javascript and add "data-link" attribute with a href for category term in your code
<h2 class="category-title" data-link="<?php echo get_term_link( (int) $category->term_id, 'product_cat' ); ?>"><?php echo $category->name; ?></h2>
I created a snippet to demonstrate:
$('.product-categories .cat-item > a').on('click', function(e) {
e.preventDefault();
var href = $(this).attr('href');
$('html, body').animate({
scrollTop: $("[data-link='" + href + "']").offset().top
}, 1000);
});
.left {
float: left;
width: 50%;
}
.right {
float: right;
width: 50%;
}
.category-wrapper {
height: 400px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="left">
<ul class="product-categories">
<li class="cat-item">
Category 1
</li>
<li class="cat-item">
Category 2
</li>
</ul>
</div>
<div class="right">
<div class="category-wrapper">
<h2 class="category-title" data-link="http://category1">Category 1</h2>
</div>
<div class="category-wrapper">
<h2 class="category-title" data-link="http://category2">Category 2</h2>
</div>
</div>

Create a shortcode in a plugin showing the content

I want to create a shortcode in an existing Wordpress/Woocommerce plugin. Where do I put the code to create the shortcode?
The plugin displays a table with all the available variations on any given variable product on the product page.
The table reacts to what the customer selects on the attributes form.
I want to make a shortcode so that I can show this in a tab on the product page instead of before the add to cart button as is now. Where should I put the code for the shortcode?
<?php
/*
Plugin Name: Variations On Product Page
Description: Display all the available variations on the product page
Version: 1.2
*/
define( 'VARIATIONS_ON_PRODUCT_VER', '1.2' );
define( 'VARIATIONS_ON_PRODUCT_PATH', plugin_dir_path( __FILE__ ) );
define( 'VARIATIONS_ON_PRODUCT_URL', plugin_dir_url( __FILE__ ) );
require_once 'settings.php';
if ( ! isset( Variations_Product_Settings::getSettings()['enabled'] ) ) {
return;
}
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_script( 'variations-on-product', VARIATIONS_ON_PRODUCT_URL . 'assets/variations-on-product.js', [ 'jquery' ], VARIATIONS_ON_PRODUCT_VER );
wp_localize_script( 'variations-on-product', 'variations_on_product_settings', apply_filters( 'variations_on_product_js_settings', [
'options' => Variations_Product_Settings::getSettings(),
] ) );
wp_enqueue_style( 'variations-on-product', VARIATIONS_ON_PRODUCT_URL . 'assets/variations-on-product.css', [], VARIATIONS_ON_PRODUCT_VER );
} );
add_action( 'woocommerce_before_add_to_cart_button', function () {
global $product;
/* #var WC_Product $product */
if( !$product->is_type('variable') ) return;
?>
<style>
#product-variations .attachment-woocommerce_thumbnail {
max-width: 100px;
}
table#product-variations .custom_form .quantity {
float: none !important;
width: 100% !important;
margin-right: 0 !important;
}
table#product-variations .custom_form .input-text.qty {
width: 100% !important;
box-sizing: border-box;
}
table#product-variations .custom_form button.button.alt {
width: 100%;
}
table#product-variations .custom_form p.stock {
margin-bottom: 3px !important;
text-align: center;
font-size: 14px
}
</style>
<table id="product-variations">
<thead>
<tr>
<?php if( !Variations_Product_Settings::getSettings()['hide_thumbnail'] ): ?>
<td>Image</td>
<?php endif; ?>
<td>Title</td>
<td>Price</td>
<td>*</td>
</tr>
</thead>
<tbody></tbody>
</table>
<?=Variations_Product_Settings::getSettings()['load_more_text'] ?: 'Load more'?>
<?php
} );
add_filter( 'woocommerce_available_variation', function ( $default, $product, $variation ) {
/* #var WC_Product_Variation $variation */
ob_start();
?>
<tr>
<?php if( !Variations_Product_Settings::getSettings()['hide_thumbnail'] ): ?>
<td>
<?= $variation->get_image() ?>
</td>
<?php endif; ?>
<td>
<?= $variation->get_name() ?>
<p>
<small><?= $variation->get_sku() ?></small>
</p>
</td>
<td>
<?= $variation->get_price_html() ?>
</td>
<td>
<?php if ( $variation->is_in_stock() ) : ?>
<form class="custom_form" action="<?php echo esc_url( get_permalink() ); ?>" method="post" enctype='multipart/form-data'>
<?php
if( !Variations_Product_Settings::getSettings()['hide_stock_status'] ) {
if ( $variation->managing_stock() ) {
echo wc_get_stock_html( $variation );
}
}
?>
<?php
if( !Variations_Product_Settings::getSettings()['hide_qty'] ) {
woocommerce_quantity_input( [
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $variation->get_min_purchase_quantity(), $variation ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $variation->get_max_purchase_quantity(), $variation ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $variation->get_min_purchase_quantity(),
] );
}
?>
<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $variation->get_id() ); ?>" class=" button alt"><?php echo esc_html( $variation->single_add_to_cart_text() ); ?></button>
</form>
<?php endif; ?>
</td>
</tr>
<?php
$default['c_template'] = apply_filters('vop_variation_template', ob_get_clean(), $variation);
return $default;
}, 10, 3 );
// Hide add to cart on product
if ( isset( Variations_Product_Settings::getSettings()['hide_product_addtocart'] ) ) {
add_action( 'woocommerce_before_add_to_cart_button', function () {
?>
<style>
.single_variation_wrap {
display: none !important;
}
</style>
<?php
} );
}
I tried to add the following code but did not work.
<?php
/*
Plugin Name: Variations On Product Page
Description: Display all the available variations on the product page
Version: 1.2
*/
add_shortcode( 'minkod', 'mitt_lilla_shortcode_handler' );
define( 'VARIATIONS_ON_PRODUCT_VER', '1.2' );
define( 'VARIATIONS_ON_PRODUCT_PATH', plugin_dir_path( __FILE__ ) );
define( 'VARIATIONS_ON_PRODUCT_URL', plugin_dir_url( __FILE__ ) );
I added the function:
function mitt_lilla_shortcode_handler () {
$myshortcode ='
add_action( 'woocommerce_before_add_to_cart_button', function () {
global $product;
/* #var WC_Product $product */
if( !$product->is_type('variable') ) return;
?>
And in the end:
> <style>
> .single_variation_wrap {
> display: none !important;
> } </style> <?php } ); ';} return $myshortcode; }

Wordpress options not saving

I'm trying to build a theme options page and have been successful until i added a tabs menu. Since adding the tabs the options no longer save.
Here is my code.
<?php
function mb_admin_menu() {
$page = add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'mb-theme-options', 'mb_theme_options' );
add_action( 'admin_print_styles-' . $page, 'mb_admin_scripts' );
}
add_action( 'admin_menu', 'mb_admin_menu' );
function mb_admin_scripts() {
wp_enqueue_style( 'farbtastic' );
wp_enqueue_script( 'farbtastic' );
wp_enqueue_script( 'my-theme-options', get_template_directory_uri() . '/js/theme-options.js', array( 'farbtastic', 'jquery' ) );
}
function mb_theme_options( $active_tab = '' ) {
?>
<div class="wrap">
<div id="icon-themes" class="icon32" ><br></div>
<h2>My Theme Options</h2>
<?php
if( isset( $_GET[ 'tab' ] ) ) {
$active_tab = $_GET[ 'tab' ];
} // end if
?>
<?php if( isset( $_GET[ 'tab' ] ) ) {
$active_tab = $_GET[ 'tab' ];
} else if( $active_tab == 'general-options' ) {
$active_tab = 'general-options';
} else {
$active_tab = 'color-options';
} // end if/else ?>
<h2 class="nav-tab-wrapper">
Color Options
General Options
</h2>
<form method="post" action="options.php">
<?php wp_nonce_field( 'update-options' ); ?>
<?php if( $active_tab == 'color-options' ) {
settings_fields( 'mb-color-options' );
do_settings_sections( 'mb-color-options' );
} else {
settings_fields( 'mb-general-options' );
do_settings_sections( 'mb-general-options' );
} // end if/else
submit_button(); ?>
</form>
</div>
<?php
}
function mb_admin_init() {
register_setting( 'mb-color-options', 'mb-color-options' );
add_settings_section( 'section_colors', 'Color Settings', 'mb_section_colors', 'mb-color-options' );
add_settings_field( 'link_color', 'Link Color', 'mb_setting_color', 'mb-color-options', 'section_colors' );
add_settings_field( 'link_hover_color', 'Link Hover Color', 'mb_hover_setting_color', 'mb-color-options', 'section_colors' );
}
add_action( 'admin_init', 'mb_admin_init' );
function mb_section_colors() {
_e( 'The general section description goes here.' );
}
function mb_setting_color() {
$options = get_option( 'mb-color-options' );
?>
<input class="link_color" type="text" name="mb-theme-options[link_color]" value="<?php echo esc_attr( $options['link_color'] ); ?>" />
<input type='button' class='select_color button-secondary' value='Select Color'>
<div class='color_picker' style='z-index: 100; background:#f1f1f1; position:absolute; display:none;'></div>
<input type='button' class='reset_color button-secondary' value='Reset'>
<?php
}
function mb_hover_setting_color() {
$options = get_option( 'mb-color-options' );
?>
<input class="link_color" type="text" name="mb-theme-options[link_hover_color]" value="<?php echo esc_attr( $options['link_hover_color'] ); ?>" />
<input type='button' class='select_color button-secondary' value='Select Color'>
<div class='color_picker' style='z-index: 100; background:#f1f1f1; position:absolute; display:none;'></div>
<input type='button' class='reset_color button-secondary' value='Reset'>
<?php
}
function mb_link_color() {
$options = get_option( 'mb-theme-options' );
$link_color = $options['link_color'];
$link_hover_color = $options['link_hover_color'];
echo "<style> a { color: $link_color; } a:hover { color: $link_hover_color; } </style>";
}
add_action( 'wp_enqueue_scripts', 'mb_link_color' );
?>
Is anyone able to point out why the options are not saving since adding the tabs ?
Is your problem solved ?? If not then try this one !! It is working for me.
I just changed the way you are registering the settings. Don't know what that was but it worked. Might work for you as well.
<?php
function mb_admin_menu() {
$page = add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'mb-theme-options', 'mb_theme_options' );
add_action( 'admin_print_styles-' . $page, 'mb_admin_scripts' );
}
add_action( 'admin_menu', 'mb_admin_menu' );
function mb_admin_scripts() {
wp_enqueue_style( 'farbtastic' );
wp_enqueue_script( 'farbtastic' );
wp_enqueue_script( 'my-theme-options', get_template_directory_uri() . '/js/theme-options.js', array( 'farbtastic', 'jquery' ) );
}
function mb_theme_options( $active_tab = '' ) {
?>
<div class="wrap">
<div id="icon-themes" class="icon32" ><br></div>
<h2>My Theme Options</h2>
<?php
if( isset( $_GET[ 'tab' ] ) ) {
$active_tab = $_GET[ 'tab' ];
}
?>
<h2 class="nav-tab-wrapper">
Color Options
General Options
</h2>
<form method="post" action="options.php">
<?php wp_nonce_field( 'update-options' ); ?>
<?php if( isset( $_GET[ 'tab' ] ) ) {
$active_tab = $_GET[ 'tab' ];
switch($active_tab) {
case 'color-options' :
echo $active_tab;
settings_fields('mb-color-options');
do_settings_sections('mb-color-options');
break;
case 'general-options' :
echo $active_tab;
settings_fields('mb-general-options');
do_settings_sections('mb-general-options');
break;
}
submit_button();
}
?>
</form>
</div>
<?php
}
function mb_admin_init() {
register_setting( 'mb-color-options', 'link_color' );
register_setting( 'mb-color-options', 'link_hover_color' );
register_setting( 'mb-general-options', 'title' );
add_settings_section( 'section_colors', 'Color Settings', null, 'mb-color-options' );
add_settings_field( 'link_color', 'Link Color', 'mb_setting_color', 'mb-color-options', 'section_colors' );
add_settings_field( 'link_hover_color', 'Link Hover Color', 'mb_hover_setting_color', 'mb-color-options', 'section_colors' );
add_settings_section( 'section_options', 'General Options', null, 'mb-general-options' );
add_settings_field( 'title', 'Page Title', 'mb_title_option', 'mb-general-options', 'section_options' );
}
add_action( 'admin_init', 'mb_admin_init' );
function mb_general_option() {
_e( 'The general section description goes here.');
}
function mb_title_option() {
$options = get_option('title');
?>
<input type="text" name="title" id="title" value="<?php echo $options;?>"/>
<?php
}
function mb_section_colors() {
_e( 'The general section description goes here.' );
}
function mb_setting_color() {
$link_color = get_option( 'link_color' );
?>
<input class="link_color" type="text" name="link_color" value="<?php echo $link_color; ?>" />
<input type='button' class='select_color button-secondary' value='Select Color'>
<div class='color_picker' style='z-index: 100; background:#f1f1f1; position:absolute; display:none;'></div>
<input type='button' class='reset_color button-secondary' value='Reset'>
<?php
}
function mb_hover_setting_color() {
$link_hover_color = get_option( 'link_hover_color' );
?>
<input class="link_color" type="text" name="link_hover_color" value="<?php echo $link_hover_color; ?>" />
<input type='button' class='select_color button-secondary' value='Select Color'>
<div class='color_picker' style='z-index: 100; background:#f1f1f1; position:absolute; display:none;'></div>
<input type='button' class='reset_color button-secondary' value='Reset'>
<?php
}
function mb_link_color() {
$options = get_option( 'mb-theme-options' );
$link_color = $options['link_color'];
$link_hover_color = $options['link_hover_color'];
echo "<style> a { color: $link_color; } a:hover { color: $link_hover_color; } </style>";
}
add_action( 'wp_enqueue_scripts', 'mb_link_color' );

Alphabetical list/index for custom tags -> even if empty - Wordpress

In wordpress (3.4) I created some code which returns an alphabetical list/index of custom tags and filters the custom posts in a grid layout. The tags are named as "tagdirectory". The custom post is named "directory".
This is the code:
<?php $list = '';
$tags = get_terms( 'tagdirectory' );
echo '<ul id="portfolio-filter">';
echo'<li>All</li>';
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= "\n\t" . '<h2>' . apply_filters( 'the_title', $letter ) .'</h2>';
$list .= "\n\t" . '<ul>';
foreach( $tags as $tag ) {
$lower = strtolower($tag->name);
$name = str_replace(' ', ' ', $tag->name);
$naam = str_replace(' ', '-', $lower);
$list .= "\n\t\t" . '<li>'.$name.'</li>';
}}}}else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';print $list;
echo "</ul>";
?>
This works perfectly but I would also like for empty letters from the alphabet to be shown.
For example, now it returns:
A
Aicher Otl
Apeloig Philippe
B
Bass Saul
F
Fitszimmons Maureen
... and so on
But it doesn't show the empty letter groups because there are no tags starting with this letter. I do need it to show the capital letter for empty groups though, like so:
A
Aicher Otl
Apeloig Philippe
B
Bass Saul
C
D
E
F
Fitszimmons Maureen
G
... and so on
Can anybody help me and tell me what code I should add for this to work?
Thanks!
I have just edited this solution> http://wordpress.mcdspot.com/2010/12/03/template-to-list-posts-by-first-letter-of-title/
I got it to display a custom post type by changing the 'post-type' on line #37 to the correct custom taxonomy ('distributors' as I am using to list companies by name) and it's done the trick.
Here is the code:
<?php
/*
Template Name: A-Z Pages
A WordPress template to list page titles by first letter.
You should modify the CSS to suit your theme and place it in its proper file.
Be sure to set the $posts_per_row and $posts_per_page variables.
*/
$posts_per_row = 3;
$posts_per_page = 15;
?>
<?php get_header(); ?>
<style type="text/css">
.letter-group { width: 100%; }
.letter-cell { width: 5%; height: 2em; text-align: center; padding-top: 8px; margin-bottom: 8px; background: #e0e0e0; float: left; }
.row-cells { width: 70%; float: right; margin-right: 180px; }
.title-cell { width: 30%; float: left; overflow: hidden; margin-bottom: 8px; }
.clear { clear: both; }
</style>
<div id="main-background">
<div id="main-column">
<h1><?php the_title(); ?></h1>
<div class="margin-top"></div>
<div id="a-z">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array (
'posts_per_page' => $posts_per_page,
'post_type' => 'page',
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged
);
query_posts($args);
if ( have_posts() ) {
$in_this_row = 0;
while ( have_posts() ) {
the_post();
$first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));
if ($first_letter != $curr_letter) {
if (++$post_count > 1) {
end_prev_letter();
}
start_new_letter($first_letter);
$curr_letter = $first_letter;
}
if (++$in_this_row > $posts_per_row) {
end_prev_row();
start_new_row();
++$in_this_row; // Account for this first post
} ?>
<div class="title-cell"><?php the_title(); ?></div>
<?php }
end_prev_letter();
?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Higher Letters') ?></div>
<div class="alignright"><?php previous_posts_link('Lower Letters »') ?></div>
</div>
<?php } else {
echo "<h2>Sorry, no posts were found!</h2>";
}
?>
</div><!-- End id='a-z' -->
</div><!-- End class='margin-top -->
</div><!-- End id='rightcolumn' -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php
function end_prev_letter() {
end_prev_row();
echo "</div><!-- End of letter-group -->\n";
echo "<div class='clear'></div>\n";
}
function start_new_letter($letter) {
echo "<div class='letter-group'>\n";
echo "\t<div class='letter-cell'>$letter</div>\n";
start_new_row($letter);
}
function end_prev_row() {
echo "\t</div><!-- End row-cells -->\n";
}
function start_new_row() {
global $in_this_row;
$in_this_row = 0;
echo "\t<div class='row-cells'>\n";
}
?>

display items in grid?

i'm trying to display my portfolio items in a gird, but can't quite figure it out. ideally, i'd like the thumbnails to display 3 or 4 across(with the rest forming a second and/or third line) and once the thumbnails are clicked on, i'd like it to go to my normal post page.
css-
/* Portfolio */
#content {
width: 603px;
}
#portfolio-item {
display: inline;
float: left;
margin: 0 0 0 60px;
width: 280px;
}
#portfolio-item h1 {
font-size: 16px;
margin: 20px 0 5px 0;
}
#portfolio-item img {
height: 50%;
width: 50%;
}
portfolio.php:
<?php
/*
Template Name: Portfolio
*/
?>
<?php get_header(); ?>
<div id="portfolio">
<?php
$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 10));
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$screenshot_url = $custom["screenshot_url"][0];
$website_url = $custom["website_url"][0];
?>
<div id="portfolio-item">
<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
</div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
functions.php:
<?php // sidebar functions
if(function_exists('register_sidebar'))
{
register_sidebar();
}
// portfolio functions
add_action('init', 'create_portfolio');
function create_portfolio() {
$portfolio_args = array(
'label' => __('Portfolio'),
'singular_label' => __('Portfolio'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'thumbnail')
);
register_post_type('portfolio',$portfolio_args);
}
// custom input- portfolio backend
add_action("admin_init", "add_portfolio");
add_action('save_post', 'update_website_url');
function add_portfolio(){
add_meta_box("portfolio_details", "Portfolio Options", "portfolio_options", "portfolio", "normal", "low");
}
function portfolio_options(){
global $post;
$custom = get_post_custom($post->ID);
$website_url = $custom["website_url"][0];
}
function update_website_url(){
global $post;
update_post_meta($post->ID, "website_url", $_POST["website_url"]);
}
// detail columns- portfolio backend
add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");
add_action("manage_posts_custom_column", "portfolio_columns_display");
function portfolio_edit_columns($portfolio_columns){
$portfolio_columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Project Title",
"description" => "Description",
);
return $portfolio_columns;
}
function portfolio_columns_display($portfolio_columns){
switch ($portfolio_columns)
{
case "description":
the_excerpt();
break;
}
}
// add thumbnail support
add_theme_support('post-thumbnails');
// disable autoformat with raw
function my_formatter($content) {
$new_content = '';
$pattern_full = '{(\[raw\].*?\[/raw\])}is';
$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));
}
}
return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'my_formatter', 99);
?>
http://www.cssbakery.com/2010/07/image-grid-using-css-floats_6950.html
This should be what you're looking for.
You can hardcode it was well with PHP if you'd like using a table if you can't get that CSS working, but that's not good coding technically.

Resources