Remove from Array if Contains Lowercase Letter - php

I have a number of WordPress posts that will each have two tags, one tag is the State and the other tag is an Airport Code.
I'm able to generate a dropdown menu from the tags using the instructions here: http://www.wprecipes.com/wordpress-hack-display-your-tags-in-a-dropdown-menu
But, I would like to actually have two different dropdowns, one that lists the States in alpha order, the other lists the Airports in alpha order. Every Airport will always be three uppercase letters. Is there an argument that I can add to to this so that I can create one dropdown for Airports and another for States?
If it contains a lowercase letter, it goes in the State dropdown. If no lowercase, it's an airport.

I modify the snippet from your attached tutorials into something like this:
function dropdown_tag_cloud( $args = '' ) {//supported: 'all', 'airport', 'state'
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
'exclude' => '', 'include' => '', 'tags_mode' => 'all'
);
$args = wp_parse_args( $args, $defaults );
print_r($args);
$tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
if ( empty($tags) )
return;
$return = dropdown_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
if ( is_wp_error( $return ) ){
echo "wp error...";
return false;
}else
echo apply_filters( 'dropdown_tag_cloud', $return, $args );
}
function dropdown_generate_tag_cloud( $tags, $args = '' ) {
global $wp_rewrite;
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
);
$args = wp_parse_args( $args, $defaults );
extract($args);
if ( !$tags )
return;
$counts = $tag_links = array();
foreach ( (array) $tags as $tag ) {
if($tags_mode == 'airport'){
//if uppercased tag is equal to the tag
//which means current tag already uppercased.
if(!(strtoupper($tag->name) == $tag->name))
continue;//skip current tag
} else if($tags_mode == 'state'){
//if uppercased tag is equal to the tag
//which means current tag already uppercased.
if((strtoupper($tag->name) == $tag->name))
continue;//skip current tag
}
$counts[$tag->name] = $tag->count;
$tag_links[$tag->name] = get_tag_link( $tag->term_id );
if ( is_wp_error( $tag_links[$tag->name] ) )
return $tag_links[$tag->name];
$tag_ids[$tag->name] = $tag->term_id;
}
$min_count = min($counts);
$spread = max($counts) - $min_count;
if ( $spread <= 0 )
$spread = 1;
$font_spread = $largest - $smallest;
if ( $font_spread <= 0 )
$font_spread = 1;
$font_step = $font_spread / $spread;
// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
if ( 'name' == $orderby )
uksort($counts, 'strnatcasecmp');
else
asort($counts);
if ( 'DESC' == $order )
$counts = array_reverse( $counts, true );
$a = array();
$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
foreach ( $counts as $tag => $count ) {
$tag_id = $tag_ids[$tag];
$tag_link = clean_url($tag_links[$tag]);
$tag = str_replace(' ', ' ', wp_specialchars( $tag ));
$a[] = "\t<option value='$tag_link'>$tag ($count)</option>";
}
switch ( $format ) :
case 'array' :
$return =& $a;
break;
case 'list' :
$return = "<ul class='wp-tag-cloud'>\n\t<li>";
$return .= join("</li>\n\t<li>", $a);
$return .= "</li>\n</ul>\n";
break;
default :
$return = join("\n", $a);
break;
endswitch;
return apply_filters( 'dropdown_generate_tag_cloud', $return, $tags, $args );
}
Basically, I was just adding new parameter called tags_mode with the following parameter supported:
all
airport
state
And then, in dropdown_generate_tag_cloud(), I add this code:
if($tags_mode == 'airport'){
if(!(strtoupper($tag->name) == $tag->name))
continue;//skip current tag
} else if($tags_mode == 'state'){
if((strtoupper($tag->name) == $tag->name))
continue;//skip current tag
}
The main idea in this added snippet is this:
strtoupper($tag->name) == $tag->name
It work like this: if the uppercased tag name is equal to the original tag name. That means, current tag is already uppercased (or, equal to airport code).
To implement it, just do as the tutorial says. Only, you need to add new parameter:
<?php dropdown_tag_cloud('number=0&order=asc&tags_mode=state'); ?>
Notice the &tags_mode=state
Just try it out and tell me if this is what you want.

Related

Dynamically populate two Gravityform select menus

I have two select menus built with Gravityforms and I am trying to use the filters below to dynamically populate them with WooCommerce order ids and WooCommerce products. The first foreach loop works as expected. I thought I could the replicate the loop for the second select menu. The queries work as I have tried them in a non-gravityforms form.
I suspect its the variable names I'm using in the second loop?
add_filter('gform_pre_render', 'populate_rma_dropdowns');
add_filter( 'gform_pre_validation', 'populate_rma_dropdowns' );
add_filter( 'gform_admin_pre_render', 'populate_rma_dropdowns' );
add_filter( 'gform_pre_submission_filter', 'populate_rma_dropdowns' );
function populate_rma_dropdowns( $form ) {
if ( $form['title'] != "RMA" ) return $form;
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'order-list' ) === false ) {
continue;
}
$query = new WC_Order_Query( array(
'limit' => -1,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$orders = $query->get_orders();
$choices = array(array('text' => 'Please find your order number', 'value' => 0 ));
foreach ( $orders as $order ) {
$choices[] = array( 'text' => $order, 'value' => $order, 'isSelected' => false );
}
$field['choices'] = $choices;
}
foreach ( $form['fields'] as &$field2 ) {
if ( $field2->type != 'select' || strpos( $field2->cssClass, 'product-list' ) === false ) {
continue;
}
$args2 = array( 'post_type' => 'product' ,'posts_per_page' => 100);
$products = get_posts( $args2 );
$choices2 = array(array('text' => 'Please select product', 'value' => 0 ));
foreach ( $products as $product ) {
$choices2[] = array( 'text' => $product->post_title, 'value' => $product->post_title, 'isSelected' => false );
}
$field2['choices2'] = $choices2;
}
return $form;
}
Looks like you've made a small typo in the second loop. You're setting $field2['choices2'] rather than $field2['choices']
I've fixed this: in the second foreach loop I had added a number to the &$field array element
foreach ( $form['fields'] as &$field ) {
...
}
#Marc was correct as well about his suggestion as well. Both drop downs now work as required.

Wordpress Filter Posts by Tags

I am trying to filter posts by tags.
Posts with all tags are available by default.
But a user can increase and decrease the number of tags in the filter.
What is the best way to filer post that way?
Should I count all omitted tags and work with URL arguments or is there an easier way?
$args =
array(
'tag' => $tags_arr, //array('sport', 'art', 'news')
);
$posts_by_tags = get_posts( $args );
foreach ( $posts_by_tags as $post_by_tags ){
echo '<li>' . $post_by_tags->post_title . '</li>';
}
UPD:
I decided to make links like:
<ul>
<li>Bananas</li>
<li>Apples</li>
<li>Oranges</li>
</ul>
PHP:
$tags = array();
foreach ( get_tags() as $before_tag ) {
$tags[] = $before_tag->name;
}
if ( isset( $_REQUEST['tags'] ) && $_REQUEST['tags'] != '' && $_REQUEST['tags'] != null ) {
$filter_tags = explode( ',', $_REQUEST['tags'] );
} else {
$filter_tags = $tags;
}
$args =
array(
'tag' => $filter_tags,
);
$posts_by_tags = get_posts( $args );
$each_sixth = 1;
foreach ( $posts_by_tags as $post_by_tags ){
// do stuff
echo get_the_title( $post_by_tags->ID );
}
Try this,
$query=new WP_Query(array('posts_per_page=-1', array('tag' => array($tags_arr))));
(OR)
$args=array('posts_per_page' => 5, 'tag' => $tags_arr);
$wp_query = new WP_Query( $args );
After loop your posts...
Finally, reset post data:
wp_reset_postdata();

add shortcode heading showing multiple time

Hi my code is below for adding shortcode in posts. when i am adding shortcode two times it shows me heading two times that i added in code "Recent Posts" is there is way to show this heading only top means one time?
/*shortcode start*/
add_shortcode( 'recent-posts', 'PL_recent_posts' );
function PL_recent_posts( $atts ) {
extract( shortcode_atts( array(
'numbers' => '5',
'order' => 'ASC',
), $atts ) );
$rposts = new WP_Query( array( 'posts_per_page' => $numbers, 'orderby' => 'date' , 'colorss' => $color ) );
if ( $rposts->have_posts() ) {
$html = '<h3>Recent Posts</h3><ul class="recent-posts">';
while( $rposts->have_posts() ) {
$rposts->the_post();
$html .= sprintf(
'<li>%s</li>',
get_permalink($rposts->post->ID),
get_the_title(),
get_the_title()
);
}
$html .= '</ul>';
}
wp_reset_query();
return $html;
}
Define a global variable to detect whether title is already added.
function PL_recent_posts( $atts ) {
global $title_added;
...
if ( $rposts->have_posts() ) {
if ( $title_added ) {
$html = '<ul class="recent-posts">';
} else {
$html = '<h3>Recent Posts</h3><ul class="recent-posts">';
$title_added = true;
}
Hope that helps..!

Customising excerpt in theme

I've been reading a lot of how to customize excerpt function in WordPress but I have no idea how to proceed with this.
The theme that I am using already have 4 pre-customized excerpt functions and the one that I will show here is closest to my desired but still needs to improve.
My question is how to stop erasing HTML formating from my content (line breaks, paragraphs, font variants, etc)?
add_shortcode('display_news_s5', 'be_display_posts_shortcode5');
function be_display_posts_shortcode5($atts) {
// Pull in shortcode attributes and set defaults
extract( shortcode_atts( array(
'post_type' => 'post',
'post_parent' => false,
'id' => false,
'tag' => '',
'category' => '',
'offset' => 0,
'posts_per_page' => '1',
'order' => 'DESC',
'orderby' => 'date',
'include_date' => false,
'include_excerpt' => false,
'excerpt_l' => 8,
'taxonomy' => false,
'tax_term' => true,
'tax_operator' => 'IN'
), $atts ) );
// Set up initial query for post
$args = array(
'post_type' => explode( ',', $post_type ),
'tag' => $tag,
'category_name' => $category,
'p' => $id,
'posts_per_page' => $posts_per_page,
'order' => $order,
'orderby' => $orderby,
'offset' => $offset
);
// If Post IDs
if( $id ) {
$posts_in = explode( ',', $id );
$args['post__in'] = $posts_in;
}
// If taxonomy attributes, create a taxonomy query
if ( !empty( $taxonomy ) && !empty( $tax_term ) ) {
// Term string to array
$tax_term = explode( ', ', $tax_term );
// Validate operator
if( !in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) )
$tax_operator = 'IN';
$tax_args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $tax_term,
'operator' => $tax_operator
)
)
);
$args = array_merge( $args, $tax_args );
}
// If post parent attribute, set up parent
if( $post_parent ) {
if( 'current' == $post_parent ) {
global $post;
$post_parent = $post->ID;
}
$args['post_parent'] = $post_parent;
}
$listing = new WP_Query( apply_filters( 'display_posts_shortcode_args', $args, $atts ) );
$count = 0;
if ( !$listing->have_posts() )
return apply_filters ('display_posts_shortcode_no_results', false );
$inner = '';
while ( $listing->have_posts() ): $listing->the_post(); global $post;
$count++;
if( $count == 1 ){
$style = ' news-main-post';
} else {
$style = ' news-list-posts';
}
$title = '<div class="news-listing-title"><a class="title" href="'. get_permalink() .'">'. get_the_title() .'</a></div>';
if ($include_date == 'true') $date = ' <div class="news-listing-meta"><span class="news-listing-date">'. get_the_date() . '</span><span class="news-listing-comment">('. get_comments_number() .')</span></div>';
else $date = '';
if ($include_excerpt == 'true') $excerpt = '<span>' .excerpt($excerpt_l) . '</span>';
else $excerpt = '';
$output = '<div class="news-listing' . $style . '"><div class="news-listing-item">'. $title . $excerpt . $date . '</div></div>';
$inner .= apply_filters( 'display_posts_shortcode_output', $output, $atts, $title, $excerpt, $date );
endwhile; wp_reset_query();
$open = apply_filters( 'display_posts_shortcode_wrapper_open', '<div class="news-listing-wrapper-s3">' );
$close = apply_filters( 'display_posts_shortcode_wrapper_close', '<div class="clear"></div></div>' );
$return = $open . $inner . $close;
return $return;
}
Have a look here: LINK looks like its doing what you want to acchieve.

inclue link list category as css class

I'd like to render some extra css classes in my wordpress links list, specifically id like to render the link category as a css class, so for example:
link : http://www.foobar.com/
gategories : friends, colleagues
name : Foo Bar
Currentyly renders as:
Foo Bar
But I want it to render as:
Foo Bar
I know that you use the following function to build the links list but i cant work out how to modify it to do what I need:
function wp_list_bookmarks($args = '') {
$defaults = array(
'orderby' => 'name', 'order' => 'ASC',
'limit' => -1, 'category' => '', 'exclude_category' => '',
'category_name' => '', 'hide_invisible' => 1,
'show_updated' => 0, 'echo' => 1,
'categorize' => 1, 'title_li' => __('Bookmarks'),
'title_before' => '<h2>', 'title_after' => '</h2>',
'category_orderby' => 'name', 'category_order' => 'ASC',
'class' => 'linkcat', 'category_before' => '<li id="%id" class="%class">',
'category_after' => '</li>'
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$output = '';
if(1) {
//output one single list using title_li for the title
$bookmarks = get_bookmarks($r);
if ( !empty($bookmarks) ) {
if ( !empty( $title_li ) ){
$output .= str_replace(array('%id', '%class'), array("linkcat-$category", $class), $category_before);
$output .= "$title_before$title_li$title_after\n\t<ul class=\"xoxo blogroll $category\">\n";
$output .= _walk_bookmarks($bookmarks, $r);
$output .= "\n\t</ul>\n$category_after\n";
} else {
$output .= _walk_bookmarks($bookmarks, $r);
}
}
}
$output = apply_filters( 'wp_list_bookmarks', $output );
if ( !$echo )
return $output;
echo $output;
}
?>
Thanks!
Firstly, you should never modify the Wordpress Core functions. In the case of Bookmarks, however, I don't really blame you for wanting to. They're kind of a pain.
I would just build it from scratch using get_bookmarks(). Here's a working example:
foreach(get_bookmarks() as $bm)
{
$terms = get_the_terms($bm->link_id, 'link_category');
$classes = array('wp_link');
if($terms)
foreach($terms as $term)
$classes[] = $term->slug;
echo '<a class="'.implode(' ', $classes).'" href="'.$bm->link_url.'"'.($bm->link_target ? 'target="'.$bm->link_target.'"' : '').'>'.$bm->link_name.'</a><br/>';
}
Just place this wherever it is in your template that you want to generate your bookmarks. Or you can wrap it up in a custom function call, and call that function from your template.

Categories