Tag Info

Hot answers tagged

7

<?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>";   } ?>  


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


5

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

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

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

You need to enqueue the scripts as well, not only register them. You can, however, just simply enqueue a script without registering it if you are not going to enqueue it conditionally. I would try something like this: (Untested and requires PHP5.4+) add_action( 'wp_enqueue_scripts', enqueue_scripts, 11 ); function enqueue_scripts() { /** * Build ...


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

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


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

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


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

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


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.


3

You might be running up against WordPress's global $blog_id variable in test case #2. Try this: foreach($sites as $my_blog_id => $name) { switch_to_blog($my_blog_id); $sites[$my_blog_id] = get_bloginfo('name'); restore_current_blog(); }


3

What about this: <?php $already_used = array(); foreach ( $terms_tags as $term_tag ) : if( ! in_array( $term_tag, $already_used ): ?> <button class="filter" data-filter=".<?php echo $term_tag->slug; ?>"><?php echo $term_tag->name; ?></button> <?php $already_used[] = $term_tag; endif; ...


2

I would tend to do this: $terms = array(); $terms_tags = wp_get_post_terms( $query->post->ID, array( 'post_tag' ) ); foreach ( $terms_tags as $term_tag ) { $terms[$term_tag->slug] = '<button class="filter" data-filter=".'.$term_tag->slug.',">'.$term_tag->name.'</button>'; } echo implode($terms,', '); It leverages the fact ...


2

What about using: echo get_the_term_list( $post->ID, 'artist-genre', '<li>', ',</li><li>', '</li>' ); instead, to generate the HTML list? Or simply: the_terms( $post->ID, 'artist-genre', '<li>', ',</li><li>', '</li>' ); that's a wrapper for get_the_term_list(). Also notice that you're missing the ...


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

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

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


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

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

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



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