Create a shortcode in a plugin showing the content - wordpress

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; }

Related

Repeater Fields in Widget Form Function in Wordpress

I've been trying to add repeater fields in my wordpress widget form. I found two plugin examples but they don't work anymore. When I click on "Add row", nothing happens.
https://www.commoninja.com/discover/wordpress/plugin/social-repeater-widget
https://www.zealousweb.com/wordpress-plugins/repeater-entries-widget/
I noticed both use jquery in their widgets which I guess doesn't work anymore in the newer Wordpress versions?
/**
* Back-end widget form.
*
* #see WP_Widget::form()
*
* #param array $instance Previously saved values from database.
*/
public function form($instance)
{
$max_entries = get_option( 'max_entries' );
$max_entries = (empty($max_entries)) ? '5' : $max_entries;
$widget_add_id = $this->id . "-add";
$title = !empty($instance['title']) ? $instance['title'] : __('Social Repeater Widget', 'srw-widget');
$srw_html = '<p>';
$srw_html .= '<label for="'.$this->get_field_id('title').'"> '. __( 'Widget Title', 'srw-widget' ) .' :</label>';
$srw_html .= '<input id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$title.'" />';
$srw_html .= '<div class="'.$widget_add_id.'-input-containers"><div id="entries">';
for( $i =0; $i<$max_entries; $i++){
if(isset($instance['block-' . $i]) || isset($instance['social_platform-' . $i]))
{
$srw_tab_title = !empty($instance['social_platform-' . $i]) ? $instance['social_platform-' . $i] : __( 'Add Social Profile Details', 'srw-widget' );
$display = (!isset($instance['block-' . $i]) || ($instance['block-' . $i] == "")) ? 'style="display:none;"' : '';
$srw_html .= '<div id="entry'.($i+1).'" '.$display.' class="entrys"><span class="entry-title" onclick = "slider(this);"> '.$srw_tab_title.' </span>';
$srw_html .= '<div class="entry-desc cf">';
$srw_html .= '<input id="'.$this->get_field_id('block-' . $i ).'" name="'.$this->get_field_name('block-' . $i ).'" type="hidden" value="'.$instance['block-' . $i].'">';
$social_platform = esc_attr( $instance['social_platform-' . $i] );
$social_platform_link = esc_attr( $instance['social_platform_link-' . $i] );
$srw_html .= '<p class="last desc">';
$srw_html .= '<label for="'.$this->get_field_id('social_platform-' . $i).'"> '. __( 'Social Platform', 'srw-widget' ) .' :</label>';
$srw_html .= '<input class="widefat" id="'.$this->get_field_id('social_platform-' . $i).'" name="'.$this->get_field_name('social_platform-' . $i).'" type="text" value="'.$social_platform.'" placeholder="'.__( 'Enter Social Platform name', 'srw-widget' ).'" />';
$srw_html .= '</p><p>';
$srw_html .= '<label for="'.$this->get_field_id('social_platform_link-' . $i).'"> '. __('Social platform Link', 'srw-widget' ) .' :</label>';
$srw_html .= '<input class="widefat" id="'.$this->get_field_id('social_platform_link-' . $i).'" name="'.$this->get_field_name('social_platform_link-' . $i).'" type="url" value="'.$social_platform_link.'" placeholder="'.__( 'Enter Social Platform Link', 'srw-widget' ).'"/>';
$srw_html .= '</p>';
/* end wrapper with delete entry option */
$srw_html .= '<p><span class="delete-row">'. __( 'Delete Row', 'srw-widget' ) .'</span></p>';
$srw_html .= '</div></div>';
}
}
$srw_html .= '</div></div>';
$srw_html .= '<div id="message">'. __( 'Sorry, you reached to the limit of','srw-widget') .' "'.$max_entries.'" '. __( 'maximum entries', 'srw-widget' ) .'.</div>' ;
$srw_html .= '<div class="'.$widget_add_id.'" style="display:none;">' . __('Add New Platform', 'srw-widget') . '</div>';
?>
<script>
jQuery(document).ready(function(e) {
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() != ''){
jQuery(this).show();
}
});
jQuery(".<?php echo $widget_add_id; ?>" ).bind('click', function(e) {
var rows = 0;
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() == ''){
jQuery(this).find(".entry-title").addClass("active");
jQuery(this).find(".entry-desc").slideDown();
jQuery(this).find('input').first().val('0');
jQuery(this).show();
return false;
}
else{
rows++;
jQuery(this).show();
jQuery(this).find(".entry-title").removeClass("active");
jQuery(this).find(".entry-desc").slideUp();
}
});
if(rows == '<?php echo $max_entries;?>')
{
jQuery("#rew_container #message").show();
}
});
jQuery(".delete-row" ).bind('click', function(e) {
var count = 1;
var current = jQuery(this).closest('.entrys').attr('id');
jQuery.each(jQuery("#entries #"+current+" .entry-desc").children(), function(){
jQuery(this).val('');
});
jQuery.each(jQuery("#entries #"+current+" .entry-desc p").children(), function(){
jQuery(this).val('');
});
jQuery('#entries #'+current+" .entry-title").removeClass('active');
jQuery('#entries #'+current+" .entry-desc").hide();
jQuery('#entries #'+current).remove();
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() != ''){
jQuery(this).find('input').first().val(count);
}
count++;
});
});
});
</script>
<style>
.cf:before, .cf:after { content: ""; display: table; }
.cf:after { clear: both; }
.cf { zoom: 1; }
.clear { clear: both; }
.clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
.clearfix { display: inline-block; }
* html .clearfix { height: 1%; }
.clearfix { display: block;}
#rew_container input,select,textarea{ float: right;width: 60%;}
#rew_container label{width:40%;}
<?php echo '.'.$widget_add_id; ?>{
background: #ccc none repeat scroll 0 0;font-weight: bold;margin: 20px 0px 9px;padding: 6px;text-align: center;display:block !important; cursor:pointer;
}
.desc{height:55px;}
#entries{ padding:10px 0 0;}
#entries .entrys{ padding:0; border:1px solid #e5e5e5; margin:10px 0 0; clear:both;}
#entries .entrys:first-child{ margin:0;}
#entries .delete-row{margin-top:20px;float:right;text-decoration: underline;color:red;}
#entries .entry-title{ display:block; font-size:14px; line-height:18px; font-weight:600; background:#f1f1f1; padding:7px 5px; position:relative;}
#entries .entry-title:after{ content: '\f140'; font: 400 20px/1 dashicons; position:absolute; right:10px; top:6px; color:#a0a5aa;}
#entries .entry-title.active:after{ content: '\f142';}
#entries .entry-desc{ display:none; padding:0 10px 10px; border-top:1px solid #e5e5e5;}
#rew_container #entries p.last label{ white-space: pre-line; float:left; width:39%;}
#message{padding:6px;display:none;color:red;font-weight:bold;}
</style>
<div id="rew_container">
<?php echo $srw_html;?>
</div>
<?php
}
I'm pretty new to php and wordpress so any help would be appreciated.
Please try the below code to create a repeater field in the widget.
dd_action( 'widgets_init', 'services_widget' );
function services_widget() {
register_widget( 'services_widget' );
}
class services_widget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'services_widget',
'description' => 'Add a service description.'
);
$control_ops = array( 'width' => 400, 'height' => 350 );
parent::__construct( 'services_widget', 'Services', $widget_ops, $control_ops );
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
if( have_rows('service', 'widget_' . $args['widget_id']) ):
echo '<ul>';
while ( have_rows('service', 'widget_' . $args['widget_id']) ) : the_row();
echo '<li class="service one-half">';
$title = get_sub_field( 'title', 'widget_' . $args['widget_id'] );
$body = get_sub_field( 'body', 'widget_' . $args['widget_id']);
$button = get_sub_field( 'button', 'widget_' . $args['widget_id'] );
$button_link = get_sub_field( 'button_link', 'widget_' . $args['widget_id'] );
if( $title ) {
echo '<h4>' . $title . '</h4>';
}
if( $body ) {
echo '<p>' . $body . '</p>';
}
if( $body ) {
echo '<a class="more" href="' . $button_link . '">' .$button . '</a>';
}
echo '</li><div class="clearfix"></div></ul>';
endwhile;
endif;
echo $args['after_widget'];
}
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
$filter = isset( $instance['filter'] ) ? $instance['filter'] : 0;
$title = sanitize_text_field( $instance['title'] );
?>
<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 esc_attr($title); ?>" /></p>
<p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox"<?php checked( $filter ); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['text'] = $new_instance['text'];
} else {
$instance['text'] = wp_kses_post( $new_instance['text'] );
}
$instance['filter'] = ! empty( $new_instance['filter'] );
return $instance;
}
}

The site is experiencing technical difficulties

When I paste code in the function.php, my website stop working and an error shows up "The site is experiencing technical difficulties"
/**
* Adding Shortcode 'box'
*/
function boxShow($atts, $content = null ){
//default values
$option = shortcode_atts( array(
'type' => '',
), $atts );
ob_start();
$class = $option[ 'type' ] ? str_replace( '"', '', $option[ 'type' ] ) : 'normal';
//HTML goes here
?>
<div class="box <?php echo $class; ?>"><?php echo $content; ?></div>
<?php
$output = ob_get_clean();
return $output;
}
add_shortcode( 'box', 'boxShow' );
and CSS Code
.shadow , .success , .info{
padding: 12px 12px;
background-color: #fafafa;
border: 1px solid black;
margin-bottom:8px;

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