Tag Info

Hot answers tagged

6

This answer was a joint effort with tnorthcutt so I made it community wiki. There are several interesting functions available: the_taxonomies(), which calls get_the_taxonomies(), which in turn calls get_object_taxonomies() and wp_get_object_terms(). the_taxonomies() is a documented template tag and is a light wrapper around a call to get_the_taxonomies(), ...


6

<?php if(count($related)) { echo "<div>Read More<ul>"; foreach($related as $id) { echo '<li><a href="'.get_permalink( $id ).'">'.get_the_title( $id ).'</a></li>';     }     echo "</ul></div>";   } ?>  


4

As Ben suggested, you need to pass the connection details when creating the wpdb class: $newdb = new wpdb( 'user', 'password', 'database', 'hostname' ); You should also test that the query actually returned something before using the result in a foreach loop: if ($rows) { foreach ($rows as $obj) { ... } }


4

function admin_trim_category_description( $terms, $taxonomies ){ if( 'category' != $taxonomies[0] )return $terms; $whatever_categories = array('category1', 'hidden', 'category2', 'category3'); foreach( $terms as $key => $term) if(!in_array($terms[$key]->name, $whatever_categories)) $terms[$key]->description = strip_tags(substr( ...


4

You can try this : foreach( $pages as $key => $post ) and : <div class = "event <?php if( $key == ( count( $pages ) - 1 ) ) echo 'last'; ?>" > It will add a last class to your last event div.


3

get_children will return an associative array of posts [...] with post IDs as array keys, or an empty array if no posts are found. This statement $attachment_id => $attachment is the same as the more commonly used $key => $value. Using this command echo '<pre>'.print_r($attachments,true).'</pre>'; we get the following: resumed ...


3

Insert a new $counter variable before the foreach loop starts, and at the very end of the loop, call $counter++ to increment the number. In the middle, just echo out the $counter variable. <?php $pages = get_children(array('orderby' => 'menu_order', 'order' => 'asc')); // Add a counter before foreach starts $counter = 1; foreach($pages as $post) ...


3

It sure can be written in a more efficient way (untested): $added = array(); global $wpdb; foreach($uniques as $unique){ $pagetitle = getTitle($unique); $new_post = array( 'post_title' => $unique, 'post_status' => 'publish', 'post_type' => 'websites' ); $pid = wp_insert_post($new_post); if ($pid) { $wpdb->query( ...


3

Looks like you missed the { for foreach Change this line: foreach ( $top_level_elements as $e ) to foreach ( $top_level_elements as $e ) { And don't forget to complete the curly bracket after the end of loop.


2

Why wouldn't you just pull them in the order you need instead of reversing the array? $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, // correct key is posts_per_page, not numberposts 'orderby' => 'date' 'order' => 'ASC', 'post_parent' => $post->ID ); $attachments = get_posts( $args );


2

You're going about this wrongly, I think. The theme_location parameter for wp_nav_menu() is a template location - i.e. a physical location in the template, as defined/registered by the Theme. It's not designed for an arbitrary number of locations based on arbitrary user content. If you want to output category-specific menus in specific template locations, ...


2

The orderby-parameter should be post_date instead of date. your code would look something like this: $blogs = get_last_updated(' ', 0, 1); foreach ($blogs AS $blog) { switch_to_blog($blog["blog_id"]); $args = array( 'orderby' => 'post_date', 'order' => 'DESC', 'numberposts' => 1, ...


2

update_post_meta() will update the value for the provided key if the key already exists in the database and then returns true, which is what you're seeing for subsequent calls. It only returns the ID of the post meta if the key didn't exist previously. If you want to store multiple values (rows) with the same key, use add_post_meta() instead.


2

Use array_unique() <?php foreach($users as $user) { $states[] = get_cimyFieldValue($user->ID, 'STATE'); // Grabing their state from their profile page } $states = array_unique($states); ?> <div class="state"> <input type="hidden" name="search_type" value="members"> <select ...


2

Ok I found a solution thanks to this: Using wp_dropdown_categories in widget options Here is the code I used instead: function form( $instance ) { /* Default Widget Settings */ $defaults = array( 'title' => 'Highlight Category' ); $instance = wp_parse_args( (array) $instance, $defaults ); ?> <!-- Widget Title --> ...


2

Yes, you could try this - foreach( $create_pages as $new_page ){ $content = "Default ". strtolower($new_page) ." page content"; $add_pages = array( 'post_title' => $new_page, 'post_content' => $content, 'post_status' => 'publish', 'post_type' => 'page' ); $page_id = wp_insert_post($add_pages); } ...


2

What you need is pretty much straight out of the Codex: $terms = get_the_terms( $post->ID, 'on-draught' ); if ( $terms && ! is_wp_error( $terms ) ) : $draught_links = array(); foreach ( $terms as $term ) { $draught_links[] = $term->name; } get_the_terms() can return a term object, false, or a WP_Error object. You are ...


2

Where you have this: $newdb = new wpdb(); You need to give the new database connection info so it can connect. Assuming you have the same user,password, and host for your new database, you could use a few of the available constants, but you will at least need to define the db name specifically: $newdb = new wpdb(DB_USER, DB_PASSWORD, 'myNewDbName', ...


2

When the foreach loop is from get_posts(), you need to run setup_postdata() for each post if you want to use themplate tags inside the loop like the_category(), if not you can get unexpected behaviour. Also, you will need to run wp_reset_postdata() after the foreach loop. Note that the_category and similar template tags actually displays/echoes the output, ...


2

You just need to use get_permalink( $id ) and get_the_title( $id ).


2

This is more of a basic PHP question, but still it's somewhat important. Inside your foreach loop, $category is a locally-defined variable. That's to say, it only exists inside the foreach loop. So if you have foreach ( $categories as $category ) { // You can use $category all you want in here } // Out here $category ceases to exist This is just ...


2

Edit: Re-reading your question I noticed I may have mixed some things up, but the point remains that you should combine these separate queries performed inside the loop, by using variables inside the loop to create a comprehensive list of the query arguments that you can use to perform one query outside the loop. I've unfortunately got no time to provide a ...


2

I'm assuming your $selected variable contains an array of values for the currently selected items? You can't use selected in this case (with multi select boxes) because it only compares two strings. It won't test to see if the value is in an array. Instead, use a ternary statement and in_array() <?php foreach ($cats as $cat_list ) { $selected = ...


2

I think what you're looking for is get_the_term_list . Give this a shot get_the_term_list( $post->ID, 'taxname', '', ', ', ''); otherwise, this function may pull them. just not sure how they'll spit out in terms of markup get_terms( $taxonomy, array( 'hide_empty' => false ) );


2

I have had similar problems with the number attribute in get_pages() I got around it by using get_children: $pages = get_children(array( 'numberposts' =>1, 'post_parent' => $heading_page->post_id )); and using post_parent instead of child_of.


2

The only thing I can see which is blatantly inefficient is the fact that you call get_cat_ID for every category for every term. That's a db query for each function call. It would be more efficient to call those functions before the foreach and put them in an array sent as a parameter to the function.


2

WP_Query seems to set two variables, $posts and $post_count when running(according to the source over at http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/query.php ). You can access these two and work something out to figure out if you're in the last three posts. var_dump those two to see if there's a count, add in your own counter and you ...


1

You can add another simple foreach loop to check if you have any values first: foreach ( $meta_boxes as $metabox ) { $has_value = false foreach ( $metabox['fields'] as $field ) { $meta = get_post_meta($post->ID, $field['id'], true); if(!isset($meta[0])){ $need_title = true; break; } } if ...


1

The reason it's not working is because you define modalcount inside the WordPress 'while' loop - put that before <?php if (have_posts()) : while (have_posts()) : the_post(); ?> and it should work perfectly. Edit: so what you want is: <?php $modalcount = 1; ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> ...


1

Why not just use the_ID() which will always be unique and won't require any extra code to be used.



Only top voted, non community-wiki answers of a minimum length are eligible