Tag Info

Hot answers tagged

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.


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) { ... } }


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) ...


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

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

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 ...


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

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

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

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

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

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( ...


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

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, ...


1

@fischi's answer appears to answer the question you asked; I suggest this only as an alternative. You can create/update a site option every time a new post is published: add_action( 'new_to_publish', 'wpse133433_latest_post' ); function wpse_133433_latest_post( $post ) { $blog_id = get_current_blog_id(); $latest = array( 'blog_id' => ...


1

I can't help but think I am missing something but what seems like the obvious answer is to alter your $create_pages array: $create_pages = array( 'one' => 'content for one', 'two' => 'content for two', 'three' => 'content for three' ); foreach ($create_pages as $title => $content) { $add_pages = array( 'post_title' => ...


1

get_post_meta() should only return false under a couple of circumstances: 269 function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) { 270 if ( !$meta_type ) 271 return false; 272 273 if ( !$object_id = absint($object_id) ) 274 return false; ...


1

If you're using the WP-PageNavi plugin, there are several FAQs on using it with a secondary query loop: http://wordpress.org/plugins/wp-pagenavi/faq/ this code is from one of the pages linked from that FAQ: $my_query = new WP_Query( array( 'tag' => 'foo', 'paged' => get_query_var('paged') ) ); while ( $my_query->have_posts() ) : ...


1

I'm not sure I fully understand your question, but I wonder if you mean: if( 5 == $post->ID || 'archive' === mb_strtolower( $post->post_title ) || 'archive' === $post->post_name ) { echo 'Show me'; } when you generate the HTML for the tabs. ps: If you check the source of is_page() you will see that it returns $wp_query->is_page( ...


1

<?php // get_posts function: http://codex.wordpress.org/Template_Tags/get_posts#Examples $args = array( 'paged' => $paged, 'post_type' => 'page', 'post__in' => array( 3, 4, 5, 6 ), /* Bio, Client, Archives, Contact */ 'order' => 'ASC' ); $myposts = get_posts( $args ); ?> <ul class="tablinks"> <?php ...


1

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 ...


1

I normalize the output of these functions using this logic: Cast the value to an array. If it is not an array already, it will be converted to an array with one element, which is the value it was, e.g. (array) false becomes array (false). Use array_filter, which, when not passed a callable, simply filters falsy values from the array. Here is this logic ...


1

Yep, this is definitely possible. Grab your post types and insert them in $post_types to loop through every one of them to manually generate the archives and posts: <?php // post types of your choice $post_types = array( 'page', 'work', 'people' ); $wp_cats = array(); foreach ( $post_types as $post_type ) { $pages = get_pages( array( ...


1

From using var_dump($arr) it looked like it was still spitting out an array that WordPress didn't know what to do with. Same with $cat_name. You have to tell WordPress, for each id in that array that comes from $category_ids (that you're now calling $cat_id for the individual ids), you want to get each of their names as well as register a menu for each. ...


1

There should be no problem with unset. There is your answer. What matters with a filter is what you return and unseting those keys before your rebuild the string and return it, works. You are doing too much processing by explodeing your $keys_false inside a loop, and you have a typo, and you are better off just making an array instead of creating a string ...


1

You are using an associative array of $attach_id => $attach_obj and loop through it. While there are methods to get the next and previous elements in this array, I would prefer to rely on continuous index array to get these objects. First convert the array: $images = array_values($images); Now loop through using the contineous index: foreach ($images ...



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