Hi I am just learning to work with the settings API and I cant seem to get to sections on the same page to work.The first section works perfetly and saves the daya but the second displays an error on save :
Options Page not Found
This is the code for my entire options page:
<?php
/* ------------------------------------------------------------------------ *
* REGISTER MENU AND SUBMENU
* ------------------------------------------------------------------------ */
function thanathos_theme_menu()
{
add_menu_page('Thanathos',
'Thanathos',
'administrator',
'thanathos_menu_id',
'thanathos_display'
);
}
add_action('admin_menu', 'thanathos_theme_menu');
/*------------------------------------------------------------------------ *
* Social & Logo Options
* ------------------------------------------------------------------------ */
function thanathos_initialize_frontpage_options()
{
if (false == get_option('thanathos_front_page_options')) {
add_option('thanathos_front_page_options');
}
add_settings_section(
'thanathos_front_page',
'',
'thanathos_front_page_section_callback',
'thanathos_front_page_options'
);
add_settings_field(
'logo_path',
'Logo Path',
'thanathos_logo_path_url_callback',
'thanathos_front_page_options',
'thanathos_front_page'
);
register_setting(
'thanathos_front_page_options',
'thanathos_front_page_options',
'thanathos_front_page_options_sanitize'
);
}
add_action('admin_init', 'thanathos_initialize_frontpage_options');
function thanathos_front_page_section_callback()
{
}
function thanathos_logo_path_url_callback()
{
$options = get_option('thanathos_front_page_options');
echo '<input type="text" id="logo" name="thanathos_front_page_options[logo_path]" value="' . $options['logo_path'] . '" />';
}
function thanathos_front_page_options_sanitize($input)
{
$output = array();
foreach ($input as $key => $val) {
if (isset($input[$key])) {
$output[$key] = strip_tags(stripslashes($input[$key]));
}
}
return apply_filters('thanathos_front_page_options', $output, $input);
}
/* ------------------------------------------------------------------------ *
* Slider Options
* ------------------------------------------------------------------------ */
function thanathos_front_page_slider_options()
{
if (false == get_option('thanathos_front_page_slider')) {
add_option('thanathos_front_page_slider');
}
add_settings_section('thanathos_front_page_slider',
'',
'thanathos_front_page_slider_callback',
'thanathos_display',
'thanathos_front_page_slider'
);
}
add_action('admin_init', 'thanathos_front_page_slider_options');
function thanathos_front_page_slider_callback()
{
}
/* ------------------------------------------------------------------------ *
* Display on Thanathos Menu Page
* ------------------------------------------------------------------------ */
function thanathos_display()
{
?>
<style>
fieldset {
border: 1px solid #ddd;
margin-top: 20px;
padding-bottom: 20px;
}
legend {
margin-left: 5px;
padding: 0 5px;
color: #2481C6;
text-transform: uppercase;
}
p.submit {
margin-left: 10px;
}
td input {
width: 360px;
}
</style>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2>Thanathos General Options</h2>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<fieldset>
<legend><strong>Logo and Social Options</strong></legend>
<?php
settings_fields('thanathos_front_page_options');
do_settings_sections('thanathos_front_page_options');
?>
</fieldset>
<?php submit_button(); ?>
</form>
<form method="post" action="options.php">
<fieldset>
<legend><strong>Slider Options</strong></legend>
<?php
settings_fields('thanathos_front_page_slider');
do_settings_sections('thanathos_front_page_slider');
?>
</fieldset>
<?php submit_button(); ?>
</form>
</div>
<?php
}
?>
If this is not the corect approch of adding two sections to the same page then what is?
Below is an excellent tutorial for the settings api. Altough a bit long if you want to do it as a whole.
In part 5 it explains why you can't have 2 settings_sections on one page.
The solution: use tabs.
This will help you with your problem.
http://wp.tutsplus.com/tutorials/the-complete-guide-to-the-wordpress-settings-api-part-1/
Related
I am trying to display a block or DIV when the product is out of stock but the condition does not appear to work
add_action( 'woocommerce_get_availability', 'display_welcome', 10);
function display_welcome() {
global $_product;
if ( ! $_product->is_in_stock() ) {
?>
<style>
#hello {display:block }
</style>
<?
}
}
html
<div id="hello">Try this product instead</div>
add_action('woocommerce_get_availability', 'display_welcome', 10, 2);
function display_welcome($availability_class, $_product) {
if ($availability_class['availability'] == 'Out of stock') {
$availability_class['class'] = $availability_class['class'] . ' my-customclass';
add_action('woocommerce_single_product_summary', 'hooks_add_div', 33);
function hooks_add_div() {
echo '<div id="helllo">mujeebu rahman</div>';
}
}
}
I have problem displaying my wp_list_categories with Style. I am already using args, etc but that does not seem to help with the problem.
I want to display same sub-categories for single article.
Here my code :
<?php
if (is_home()) {
wp_list_categories('orderby=id&title_li=none&depth=1');
} else {
$category = get_the_category();
$cat_term_id = $category[0]->term_id;
$cat_category_parent = $category[0]->category_parent;
$listcat = wp_list_categories('echo=0&child_of='.$cat_category_parent.'&title_li=');
$listcat = str_replace("cat-item-".$cat_term_id, "cat-item-".$cat_term_id." current-cat", $listcat);
if ( in_category( $cat_term_id ) || post_is_in_descendant_category( $cat_term_id )) {
echo $listcat;
}
}
?>
</ul>
and here is the result :
http://i.stack.imgur.com/buYyl.jpg
I want the result will be like this :
http://i.stack.imgur.com/U3Pc6.jpg
I think this might work for you, but I'm not exactly sure which theme file you're referencing above.
<?php $parent_cat_id = 1; // Change this ID to match the ID value of the "Sepeda" category ?>
<?php $categories = get_categories( "child_of=$parent_cat_id" ); ?>
<?php if ($categories) : ?>
<ul id="category-list">
<?php // Print the link for 'Sepeda' ?>
<?php echo "<li class='category-name'><a href='" . get_category_link($parent_cat_id) . "'>" . get_cat_name($parent_cat_id) . "</a></li>"; ?>
<?php // Loop through the sub-categories of 'Sepeda' and print the names and links ?>
<?php foreach ($categories as $cat) {
echo "<li class='category-name'><a href='" . get_category_link( $cat->term_id ) . "'>" . $cat->name . "</a></li>";
} ?>
<?php wp_reset_query(); // Restore global post data ?>
</ul>
<?php endif; ?>
Then you could use this CSS to style the category list appropriately:
#category-list {
width: 250px;
padding: 5px;
background: white;
list-style: none;
}
.category-name {
background-color: #694489;
color: white;
min-height: 40px;
text-align: center;
line-height: normal;
padding-top: 10px;
}
.category-name + .category-name {
margin-top: 10px;
}
I have wordpress 3.9.1 installed and am using a plugin to customize the login page.
The plugin is called Branded Login Screen. It is working well and I've been able to edit most of what I'd wanted to.
Now, there's a text just above the form fields that isn't on the default wordpress login screen and I want this text removed.
Please visit the login page here. The text says:
"BRANDED LOGIN SCREEN 3.2. Enter your login credentials".
I don't want this text.
Here's what the code looks like:
if ( !function_exists( 'add_action' ) ) {
echo 'This plugin cannot be called directly.';
exit;
}
if ( class_exists( 'Branded_Login_Screen_Pro' ) ) {
$msg = '<img src="/wp- content/plugins/branded-login-screen/assets/i/err-header.png" border="0" /><br/>';
$msg .= '<b>Branded Login Screen</b> cannot be activated with \'Branded Login Screen Pro\' active.<br/>';
$msg .= 'Please deactivate \'Branded Login Screen Pro\' before activating <b>Branded Login Screen.</b><br/>';
$msg .= '<b>Branded Login Screen</b> has not been activated.<br/>';
$msg .= '<hr><p>';
$msg .= 'Back to the plugins page.';
$msg .= '</p>';
wp_die( __( $msg ) );
}
// constant definition
if( !defined( 'IS_ADMIN' ) )
define( 'IS_ADMIN', is_admin() );
define( 'BLS_VERSION', '3.2' );
define( 'BLS_DIR', WP_PLUGIN_DIR . '/' . basename( dirname( __FILE__ ) ) );
define( 'BLS_URL', rtrim( plugin_dir_url( __FILE__ ), '/' ) . '/' );
define( 'BLS_UPLOAD_URL', BLS_URL . 'assets/i' );
define( 'BLS_REDIRECT', '' );
define( 'BLS_SITE_URL', get_site_url() ); //wordpress function
define( 'BLS_SITE_NAME', get_bloginfo() ); //wordpress function
define( 'BLS_BG_IMG_DIR', BLS_UPLOAD_URL.'/background_image/' );
define( 'BLS_HDR_DIR', BLS_UPLOAD_URL.'/logo_main' );
define( 'BLS_RHDR_DIR', BLS_UPLOAD_URL.'/logo_responsive' );
/* --------------------------------------------------------------------------------------------- */
/* you can edit the variables below to make changes to the login form and the images used for */
/* the background. Please refer to the plugins documentation for examples and explanation */
/* --------------------------------------------------------------------------------------------- */
define( 'BLS_LOCATION', 0 ); //used to change location of login form on the page. See documentation for value meanings
define( 'BLS_FULL_SCREEN', true ); // if using a repeatable background image, set this to false.
define( 'BLS_BG_IMG', 'default-img.jpg' ); // place your 'background' image in the '/assets/i/background_image' folder.
define( 'BLS_HDR_LOGO', 'header.png' ); // place your 'header logo' image in the '/assets/i/logo_main' folder.
define( 'BLS_RHDR_LOGO', 'header-sm.png' ); // place your 'responsive logo' image in the '/assets/i/logo_responsive' folder.
/* --------------------------------------------------------------------------------------------- */
class Branded_Login_Screen
{
private $plugin_name = 'Branded Login Screen';
private $plugin_version = '3.2';
public function __construct() {
register_activation_hook( __FILE__, array( __CLASS__, 'branded_login_screen_activate' ) );
register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
add_action( 'login_enqueue_scripts', array( __CLASS__, 'branded_login_screen_styles' ) );
add_action( 'login_enqueue_scripts', array( __CLASS__, 'branded_login_screen_scripts' ) );
add_action( 'login_enqueue_scripts', array( __CLASS__, 'branded_login_screen_vars' ) );
add_filter( 'plugin_row_meta', array( __CLASS__, 'branded_login_screen_plugin_meta_links'), 10, 2 );
add_action( 'login_footer', array( __CLASS__, 'branded_login_screen_footer' ) );
if( IS_ADMIN ) {
add_action( 'admin_init', array( __CLASS__, 'branded_login_screen_environment_check' ) );
}
} //End of Branded_Login_Screen_Pro __construct
/**
* Css additions for login page.
*
* Css for login form location on page. Header/responsive header images media queries. Admin Css link.
*
* #param
* #return void
* #author Kerry Webster
*/
public function branded_login_screen_styles() {
switch ( BLS_LOCATION ) {
case 0: //middle-center ?>
<style>div#login { top: 0; right: 0; bottom: 0; left: 0; padding: 0; }</style>
<?php
break;
case 1: //middle-left ?>
<style>div#login { top: 0; right:auto; bottom: 0; left: 0; padding: 0 0 0 40px; }</style>
<?php
break;
case 2: //middle-right ?>
<style>div#login { top: 0; right: 0; bottom: 0; left:auto; padding: 0 40px 0 0; }</style>
<?php
break;
case 3: //top-center ?>
<style>div#login { top: 10px; right: 0; bottom: auto; left: 0; padding: 0; }</style>
<?php
break;
case 4: //top-left ?>
<style>div#login { top: 10px; right:auto; bottom: auto; left: 0; padding: 0 0 0 40px; }</style>
<?php
break;
case 5: //top-right ?>
<style>div#login { top: 10px; right: 0; bottom: auto; left:auto; padding: 0 40px 0 0; }</style>
<?php
break;
default: //middle-center ?>
<style>div#login { top: 0; right: 0; bottom: 0; left: 0; padding: 0; }</style>
<?php
}
?>
<style> .login h1 a { background:url(<?php echo BLS_HDR_DIR . '/' . BLS_HDR_LOGO; ?>) no-repeat scroll center top transparent; } </style>
<style> #media all and (max-width: 360px), (max-height: 465px){ .login h1 a { background:url(<?php echo BLS_RHDR_DIR . '/' . BLS_RHDR_LOGO; ?>) no-repeat scroll center top transparent; width: 260px; height: 67px; } } </style>
<link rel="stylesheet" id="custom_button_css" href="<?php echo plugins_url( '/assets/c/custom-button.css', __FILE__ ); ?>" type="text/css" media="all" />
<link rel="stylesheet" id="branded_login_css" href="<?php echo plugins_url( '/assets/c/branded-login-screen.css', __FILE__ ); ?>" type="text/css" media="all" />
<?php
}
/**
* Plugin javacsript enqueue
*
* Add the plugins javascript file
*
* #param
* #return void
* #author Kerry Webster
*/
public function branded_login_screen_scripts() {
wp_enqueue_script( 'branded-login-screen', plugins_url('/assets/j/branded-login-screen.js', __FILE__ ), array( 'jquery' ), '1.0', TRUE );
}
/**
* Get header url and title
*
* Header URL and title used for redirection after the user clicks the login logo
*
* #param
* #return void
* #author Kerry Webster
*/
public function branded_login_screen_vars() {
add_filter( 'login_headerurl', array( __CLASS__, 'bls_login_header_url' ) );
add_filter( 'login_headertitle', array( __CLASS__, 'bls_login_site_name' ) );
}
/**
* Fullscreen or repeatable background image
*
* Inserts the proper .css for the selected background image type
*
* #param
* #return void
* #author Kerry Webster
*/
public function branded_login_screen_footer(){
$imageName = BLS_BG_IMG_DIR.BLS_BG_IMG;
if ( BLS_FULL_SCREEN ) {
?>
<style>
body.login { background:#fff url('<?php echo $imageName; ?>') no-repeat fixed top center;
margin: 0;
padding: 0;
background-size: cover;
-moz-background-size: cover;
-webkit-background-size: cover;
}
</style>
<?php
} else {
?>
<style> body.login { background:#fff url('<?php echo $imageName; ?>') repeat fixed top; width:100%; height:100%; } </style>
<?php
} ;
}
/**
* Pro version link
*
* Link to get the Pro version of the plugin
*
* #param $links
* #param $file
* #return $links
* #author Kerry Webster
*/
public function branded_login_screen_plugin_meta_links( $links, $file ) {
$plugin = plugin_basename( __FILE__ );
// create link
if ( $file == $plugin ) {
return array_merge(
$links,
array( '<strong>Get the PRO Version</strong>' )
);
}
return $links;
}
/*------------------------------------------------------------------------------------*/
/**
* Check for proper WordPress and PHP versions
*
* Fails plugin load due to version limitations. Suggests upgrades where appropriate.
*
* #return void or die
* #author Kerry Webster
*/
function branded_login_screen_environment_check()
{
$wp_version = get_bloginfo( 'version' );
$php_ver_needed = '5.2.4';
$wp_ver_needed = '3.3';
$php_ok = version_compare( PHP_VERSION, $php_ver_needed, '>=' );
$wp_ok = version_compare( $wp_version, $wp_ver_needed, '>=' );
$php_ok_status = $php_ok ? '<span style="color: #00cc00;">OK</span>' : '<span style="color: #ff0000;">Please upgrade your PHP install.</span>';
$wp_ok_status = $wp_ok ? '<span style="color: #00cc00;">OK</span>' : '<span style="color: #ff0000;">Please upgrade your WordPress install.</span>';
if( !$php_ok || !$wp_ok )
{
if( IS_ADMIN && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) )
{
require_once ABSPATH.'/wp-admin/includes/plugin.php';
deactivate_plugins( __FILE__ );
$msg = '<img src="/wp-content/plugins/branded-login-screen/assets/i/err-header.png" border="0" /><br/>';
$msg .= '<b>Branded Login Screen</b> requires WordPress version ';
$msg .= $wp_ver_needed . '+';
$msg .= ' and PHP version ';
$msg .= $php_ver_needed . '+';
$msg .= '. <br/>It has been automatically deactivated.<br/>';
$msg .= '<hr><p>';
$msg .= 'Your current installed versions: <br/>';
$msg .= 'WordPress: ' . $wp_version . ' - ' . $wp_ok_status . '<br/>';
$msg .= 'PHP: ' . PHP_VERSION . ' - ' . $php_ok_status . '</p>';
$msg .= 'Back to the plugins page.';
wp_die( __( $msg ) );
}
else
{
return;
}
}
}
/**
* Plugin activation
*
* Set plugin defaults
*
* #author Kerry Webster
* #since 3.0
*/
public function branded_login_screen_activate()
{
}
/**
* CLASS PRIVATE FUNCTIONS
*/
function bls_login_header_url() {
if ( BLS_REDIRECT != NULL ) {
return BLS_REDIRECT;
} else {
return BLS_SITE_URL;
}
}
function bls_login_site_name() {
if ( BLS_REDIRECT != NULL ) {
return BLS_REDIRECT;
} else {
return 'Back to ' . BLS_SITE_NAME . ' >> Home Page';
}
}
Edit: Thanks #Rahil. Here is the Js code. You were correct I can see the text here:
jQuery(document).ready(function( $ ) {
$('#backtoblog a').prop('title','Back to Home Page');
$('form#loginform').prepend('<h2>Enter your login credentials.</h2><br class="clear">');
$('form#lostpasswordform').prepend('<h2>Enter the required information.You will receive a new password via e-mail.</h2><br class="clear">');
$('form#resetpassform').prepend('<h2>Enter your new password below.</h2><br class="clear">');
$('form#registerform').prepend('<h2>Create your own personalized account. A password will be<br\>e-mailed to you.</h2><br class="clear">');
$('form').prepend('<p class="ver">Branded Login Screen 3.2</p>');
//TODO: make the alert boxes look prettier. :)
$("p.reset-pass:contains('Enter your new password below')").hide();
$("p.reset-pass:contains('Your password has been reset')").show().addClass('backtologin').removeClass('message').removeClass('reset-pass');
});
One solution could be adding your own stylesheet to the login page:
http://codex.wordpress.org/Customizing_the_Login_Form#Styling_Your_Login
You could then display:none the text with CSS (#loginform .ver, #loginform h2)
I am creating a theme in which I have created some custom post type products and some custom meta boxes to submit their respective custom information like its cost and expiry date.
By doing some search add_theme_support( 'post-thumbnails' );, I was able to add a featured image column to upload featured image related to a particular post. But it only allows me to add single image and I want to upload multiple images related to one particular custom post .For example different images of single product.
For this I found different solutions on internet and one of them which I found the most suitable to my problem is this, but again it only allows me to upload only one image but different than featured image.
Does WordPress has any functionality to upload multiple images like featured image upload particularly for single post or is there any plugin for this task?
<?php
add_action( 'admin_init', 'add_post_gallery_so_14445904' );
add_action( 'admin_head-post.php', 'print_scripts_so_14445904' );
add_action( 'admin_head-post-new.php', 'print_scripts_so_14445904' );
add_action( 'save_post', 'update_post_gallery_so_14445904', 10, 2 );
/**
* Add custom Meta Box to Posts post type
*/
function add_post_gallery_so_14445904()
{
add_meta_box(
'post_gallery',
'Studio Image Uploader',
'post_gallery_options_so_14445904',
'post',// here you can set post type name
'normal',
'core'
);
}
/**
* Print the Meta Box content
*/
function post_gallery_options_so_14445904()
{
global $post;
$gallery_data = get_post_meta( $post->ID, 'gallery_data', true );
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'noncename_so_14445904' );
?>
<div id="dynamic_form">
<div id="field_wrap">
<?php
if ( isset( $gallery_data['image_url'] ) )
{
for( $i = 0; $i < count( $gallery_data['image_url'] ); $i++ )
{
?>
<div class="field_row">
<div class="field_left">
<div class="form_field">
<label>Image URL</label>
<input type="text"
class="meta_image_url"
name="gallery[image_url][]"
value="<?php esc_html_e( $gallery_data['image_url'][$i] ); ?>"
/>
</div>
</div>
<div class="field_right image_wrap">
<img src="<?php esc_html_e( $gallery_data['image_url'][$i] ); ?>" height="48" width="48" />
</div>
<div class="field_right">
<input class="button" type="button" value="Choose File" onclick="add_image(this)" /><br />
<input class="button" type="button" value="Remove" onclick="remove_field(this)" />
</div>
<div class="clear" /></div>
</div>
<?php
} // endif
} // endforeach
?>
</div>
<div style="display:none" id="master-row">
<div class="field_row">
<div class="field_left">
<div class="form_field">
<label>Image URL</label>
<input class="meta_image_url" value="" type="text" name="gallery[image_url][]" />
</div>
</div>
<div class="field_right image_wrap">
</div>
<div class="field_right">
<input type="button" class="button" value="Choose File" onclick="add_image(this)" />
<br />
<input class="button" type="button" value="Remove" onclick="remove_field(this)" />
</div>
<div class="clear"></div>
</div>
</div>
<div id="add_field_row">
<input class="button" type="button" value="Add Field" onclick="add_field_row();" />
</div>
</div>
<?php
}
/**
* Print styles and scripts
*/
function print_scripts_so_14445904()
{
// Check for correct post_type
global $post;
if( 'post' != $post->post_type )// here you can set post type name
return;
?>
<style type="text/css">
.field_left {
float:left;
}
.field_right {
float:left;
margin-left:10px;
}
.clear {
clear:both;
}
#dynamic_form {
width:580px;
}
#dynamic_form input[type=text] {
width:300px;
}
#dynamic_form .field_row {
border:1px solid #999;
margin-bottom:10px;
padding:10px;
}
#dynamic_form label {
padding:0 6px;
}
</style>
<script type="text/javascript">
function add_image(obj) {
var parent=jQuery(obj).parent().parent('div.field_row');
var inputField = jQuery(parent).find("input.meta_image_url");
tb_show('', 'media-upload.php?TB_iframe=true');
window.send_to_editor = function(html) {
var url = jQuery(html).find('img').attr('src');
inputField.val(url);
jQuery(parent)
.find("div.image_wrap")
.html('<img src="'+url+'" height="48" width="48" />');
// inputField.closest('p').prev('.awdMetaImage').html('<img height=120 width=120 src="'+url+'"/><p>URL: '+ url + '</p>');
tb_remove();
};
return false;
}
function remove_field(obj) {
var parent=jQuery(obj).parent().parent();
//console.log(parent)
parent.remove();
}
function add_field_row() {
var row = jQuery('#master-row').html();
jQuery(row).appendTo('#field_wrap');
}
</script>
<?php
}
/**
* Save post action, process fields
*/
function update_post_gallery_so_14445904( $post_id, $post_object )
{
// Doing revision, exit earlier **can be removed**
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// Doing revision, exit earlier
if ( 'revision' == $post_object->post_type )
return;
// Verify authenticity
if ( !wp_verify_nonce( $_POST['noncename_so_14445904'], plugin_basename( __FILE__ ) ) )
return;
// Correct post type
if ( 'post' != $_POST['post_type'] ) // here you can set post type name
return;
if ( $_POST['gallery'] )
{
// Build array for saving post meta
$gallery_data = array();
for ($i = 0; $i < count( $_POST['gallery']['image_url'] ); $i++ )
{
if ( '' != $_POST['gallery']['image_url'][ $i ] )
{
$gallery_data['image_url'][] = $_POST['gallery']['image_url'][ $i ];
}
}
if ( $gallery_data )
update_post_meta( $post_id, 'gallery_data', $gallery_data );
else
delete_post_meta( $post_id, 'gallery_data' );
}
// Nothing received, all fields are empty, delete option
else
{
delete_post_meta( $post_id, 'gallery_data' );
}
}
?>
ooh thanks for telling me that Brasofilo.
And the right answer is one can upload multiple images through the media upload button bellow the title of the post and can display those images in a gallery on front end by using gallery shortcode for-eg:-
<?php echo do_shortcode('[gallery size="thumbnail"]'); ?>
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";
}
?>