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.
Related
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..!
I am trying to get the following wp_dropdown_categories call to pre select values based on what the user has submitted before. This is part of a larger user profile edit form. The values are pulled in just fine but are not being automatically highlighted if they were selected previously. Any help would be appreciated!! job_ind_pref_call=custom user profiel field.
</label> <?php
$sel = 0;
$sel1 = get_user_meta($user_ID, 'job_ind_pref_call', true);
if(isset($_POST['job_ind_pref_call'])) {
$sel1 = $_POST['job_ind_pref_call'];
}
if (isset($posted['job_term_cat']) && $posted['job_term_cat']>0) $sel = $posted['job_term_cat'];
global $featured_job_cat_id;
$args = array(
'orderby' => 'name',
'exclude' => 3,
'order' => 'ASC',
'name' => 'job_ind_pref_call[]',
'hierarchical' => 1,
'echo' => 0,
'class' => 'job_cat',
'selected' => $sel1,
'taxonomy' => 'job_cat',
'hide_empty' => false
);
$dropdown = wp_dropdown_categories( $args );
$dropdown = str_replace('class=\'job_cat\' >','class=\'job_cat\' multiple="multiple" size="6" onClick=GetMDDselections("job_ind_pref_call") ><option value="">'.__('Select a Lineā¦', 'colabsthemes').'</option>',$dropdown);
echo $dropdown;
?> </p>
First of all, the WordPress crew recommends that you use wp_category_checklist() for multiple values.
If you still persist to use wp_dropdown_categories() you must be prepared to use an.. ehum, unofficial way to make the selected options work.
Here's what you need to do:
1- Pass a new custom argument AND a new Walker class (we'll define it in step 2) to the wp_dropdown_categories() function. Let's say we call the function as such:
<?php
wp_dropdown_categories( array(
'_selected' => $selected_cats_arr,
'walker' => 'CategoryDropdownMultiple'
) );
?>
2- Create a new Walker class that is configured to select the option based on our new custom argument. The code for the Walker is based on Walker_CategoryDropdown defined in wp-includes/category-template.php.
<?php
class Walker_CategoryDropdownMultiple extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
function start_el( &$output, $category, $depth, $args, $id = 0 ) {
$pad = str_repeat(' ', $depth * 3);
$cat_name = apply_filters('list_cats', $category->name, $category);
$output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
// HERE IS THE ONLY CHANGE FROM THE ORIGINAL FILE
// We check our custom parameter which is an array instead of a single value.
if ( isset( $args['_selected'] ) && in_array( $category->term_id, $args['_selected'] ) )
$output .= ' selected="selected"';
$output .= '>';
$output .= $pad.$cat_name;
if ( $args['show_count'] )
$output .= ' ('. $category->count .')';
$output .= "</option>\n";
}
}
?>
Notes:
The reason for why we can't use the original selected argument is that it's expected to be a single value in wp_dropdown_categories() and beyond.
Note that because we're using a custom argument for the selected options then native functionality such as automatically selecting the options given with the *show_option_all* and *show_option_none* arguments won't work.
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.
I am working on a Wordpress-Design and i want to creat a Custom Menu.
$items = wp_get_nav_menu_items('Menu', array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'nav_menu_item',
'post_status' => 'publish',
'output' => ARRAY_A,
'output_key' => 'menu_order',
'nopaging' => true,
'update_post_term_cache' => false));
echo '<pre>'; print_r($items); echo '</pre>';
foreach($items as $item){
echo '<div class="menu_entry">'.$item->title.'</div>';
}
The problem is, i need the "current-page"-Class, which is WordPress creating - in the Standard Menu.
Any Ideas how to add this class?
Solution time:
WordPress's function that adds these classes is _wp_menu_item_classes_by_context(). This is called already when you use wp_nav_menu but not wp_get_nav_menu_items. Fortunately, the latter provides a filter so we can do it ourselves:
add_filter( 'wp_get_nav_menu_items', 'prefix_nav_menu_classes', 10, 3 );
function prefix_nav_menu_classes($items, $menu, $args) {
_wp_menu_item_classes_by_context($items);
return $items;
}
You can do a compare on the current page / cat etc ID against the menu items object_id which is the ID of the page / category etc its linked to.
Something like (untested);
global $post;
$thePostID = $post->ID;
foreach($items as $item){
if($thePostID === $item->object_id) {
echo '<div class="menu_entry">'.$item->title.'</div>';
}else{
echo '<div class="menu_entry">'.$item->title.'</div>';
}
}
Using the function get_queried_object_id(). This works fine in all the pages, including the Blog page.
See an example:
if ( $menu_items = wp_get_nav_menu_items( 'menu' ) ) {
foreach ( $menu_items as $menu_item ) {
$current = ( $menu_item->object_id == get_queried_object_id() ) ? 'current' : '';
echo '<li class="' . $current . '">' . $menu_item->title . '</li>';
}
}
Ok here's one for ya...
On a custom template I'm using this code to retrieve & display a list of child pages/posts
$args = array(
'depth' => 1,
'show_date' => '',
'date_format' => get_option('date_format'),
'child_of' => $post->ID,
'exclude' => '',
'include' => '',
'title_li' => '',
'echo' => 1,
'authors' => '',
'sort_column' => 'menu_order, post_title',
'link_before' => '',
'link_after' => '',
'walker' => '' );
wp_list_pages( $args );
This works great, I'm also wondering how I can access/create an array of child post ID's. My goal is to access some custom fields meta data through the get_post_meta() function of each child post using it's ID.
Thanks guys.
I guess I wasn't very clear with this one as it's the first time I've never recieved an answer from SO.
I managed to find the information I needed and will place it here for anyone else browsing with the same request.
ok - To get all child IDs..
$pages = get_pages('child_of=X');
foreach($pages as $child) {
// Now you have an object full of Children ID's that you can use for whatever
// E.G
echo $child->ID . "<br />";
}
If you want to build an array of post ids for later use you can do this:
$pageids = array();
$pages = get_pages('child_of=X');
foreach($pages as $page){
$pageids[] = $page->ID;
}
And you have a clean array of just page ids.
$children = get_posts('post_parent=SLUG_OF_PARENT_POST&post_status=publish');
foreach($children as $child)
{
echo '<br/>ID:'.$child->ID;
}
you can use other attributes (i.e. $child->post_content)...
if you need to define post_type, then add this argument too : &post_type=POST_TYPE_NAME
Another way to do this:
$my_page_id = 12345;
$child_query_args = array(
'post_parent' => $my_page_id,
'post_type' => 'page',
'posts_per_page' => -1,
'fields' => 'ids',
);
$child_query = new WP_Query($child_query_args);
if ( $child_query && $child_query->have_posts() && $child_query->posts ) {
// (Since fields=ids, $child_query->posts is just an array of IDs)
$child_ids = $child_query->posts;
foreach ( $child_ids as $child_id ) {
$whatever = get_post_meta( $child_id, 'whatever', true );
echo esc_html($whatever);
}
}