Get images from post's gallery WordPress 3.5 - wordpress

How to get images from a gallery in a post in WordPress 3.5 as gallery is no longer related to posts in 3.5. get_children() doesnot work as gallery is not attachment. Any help is appreciated.

You must probably parse shortcode:
http://codex.wordpress.org/Gallery_Shortcode
Use regular expression:
$post_content = get_the_content();
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);

`global $post;
$post_subtitrare = get_post( $post->ID );
$content = $post_subtitrare->post_content;
$pattern = get_shortcode_regex();
preg_match( "/$pattern/s", $content, $match );
if( isset( $match[2] ) && ( "gallery" == $match[2] ) ) {
$atts = shortcode_parse_atts( $match[3] );
$attachments = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID .'&order=ASC&orderby=menu_order ID' );
}`
The $attachments will get you what you are used to getting prior to WordPress 3.5.

Related

Avoid looking into Post content while searching

I have created a query using WP_Query for searching posts. Below is the query
$args = array(
's' => 'Keyword'
);
$the_query = new WP_Query( $args );
But the argument 's' always looks into post_title and post_content. I want to change it so it look into post_title only and not into post_content. How to do that?
At first you need to modify the search function of wp, using posts_search filter hook Check this Link, then run your query and argument.
After that, you need to remove that hook after use.
The example code is bellow
<?php
function wpse_11826_search_by_title( $search, $wp_query ) {
if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
global $wpdb;
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search = array();
foreach ( ( array ) $q['search_terms'] as $term )
$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );
if ( ! is_user_logged_in() )
$search[] = "$wpdb->posts.post_password = ''";
$search = ' AND ' . implode( ' AND ', $search );
}
return $search;
}
add_filter( 'posts_search', 'wpse_11826_search_by_title', 10, 2 );
$args = array(
's' => 'Keyword'
);
$the_query = new WP_Query( $args );
remove_filter( 'posts_search', 'wpse_11826_search_by_title', 500 );
//Then your code goes here....
?>

Wordpress - How to auto insert some words from title to tags

I want to auto-insert words in title of a post and begin by # in tags. and use this function. but when i publish a post, all words inserted.
function convert_to_tag( $post_id ) {
$post = get_post( $post_id );
$tax = isset( $types[$post->post_type] ) ? $types[$post->post_type] : 'post_tag';
if( isset( $post->post_title ) ) :
$title_words = explode( ' ', $post->post_title );
foreach( $title_words as $word ) :
if (0 == strpos($word, '#')) :
wp_insert_term(
$word,
$tax,
array()
);
endif;
endforeach;
endif;
}
add_action( 'save_post', 'convert_to_tag' );

I need to get all tags from woo commerce products in a page

I need to display all the product tags as a dropdown on main page. I tried the following code but it did not work.
$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$term_array[] = $term->name;
}
}
It always returns an empty array. Any suggestions? Thanks.
I'm not certain what you mean by dropdown, but will use <select> for this answer.
In your functions.php
function get_some_tags_man(){
$terms = get_terms( array(
'hide_empty' => false, // only if you want to hide false
'taxonomy' => 'product_tag',
)
);
$html = '';
if($terms){
$html .= '<select name="terms" id="someID">';
foreach($terms as $term){
$html .= "<option name='$term->name'>$term->name</option>";
}
$html .= '</select>';
}
return $html;
}
in your theme file:
<?php echo get_some_tags_man(); ?>
Try this, it will also show empty tags.
$terms = get_terms( array( 'taxonomy' => 'product_tag', 'hide_empty' => false ) );
I just tried the code (in the product page and Shop page) and it worked.
Main Page you mean Shop Page? The first page that appear when you enter the site.
The theme i'm using is StoreFront.
Can you show the hook you are using? or the code? Look. This is the complete code I use.
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_product_loop_tags', 15 );
function woocommerce_product_loop_tags() {
global $post, $product;
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) );
$idProducto=$product->id;
$product->get_title();
echo $product->get_tags( ', ', '<span class="tagged">' . _n( 'Tag:', 'Tags:', $tag_count, 'woocommerce' ) . ' ', '.</span>' );
$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo " ".$term_array[] = $term->name;
}
}
}

Download Count on WooCommerce Free Product

I provide freebies using WooCommerce plugin.
I want to ask, is it possible to determine the number of downloads on the freebies on the product detail page downloads?
Use this code:
add_action( 'woocommerce_single_product_summary', 'show_number_of_downloads' );
function show_number_of_downloads() {
global $wpdb, $product;
if ( empty( $product->id ) ) return;
if ( $product->product_type == 'variable' ) {
$product_ids = $product->get_children();
} else {
$product_ids = array( $product->id );
}
$query = "SELECT SUM( download_count ) AS count
FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE product_id IN (".implode( ',', $product_ids ).")";
$count = $wpdb->get_var( $query );
if ( ! empty( $count ) ) {
echo '<p><strong>' . __( 'Total downloads' ) . '</strong>: ' . $count . '</p>';
}
}
Keep this code either in your theme's function.php file or any custom plugin such that you don't loose this code after upgrade of either WordPress, plugins or theme.
The result:

Wordpress: Display Subcategory Name Without Link

I have this code that works great to show the subcategory name and link of a specific parent category of my choosing:
<?php
$taxonomy = 'category';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$categories = get_the_category();
$parentid = '6';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = strtr( wp_list_categories( 'title_li=&style=none&echo=0&child_of=' . $parentid . '&taxonomy=' . $taxonomy . '&include=' . $term_ids), array( '<br />' => ' <br /> ' ) );
echo preg_replace( '#\s<br />\s\n$#', '', $terms );
}
?>
Now I want to be able to do the same thing but WITHOUT the code above generating the link automatically. Any ideas?
Use get_categories() instead of wp_list_categories()
$terms = get_categories("child_of={$parentid}&include={$term_ids}");
foreach($terms as $term)
echo $term->name;

Resources