Just wondering if anyone can help with this - banging my head for days...
From a custom taxonomy page (taxonomy_genre.php), I need to output the child terms with images.
I'm using the 'Taxonomy Images' plugin to set the image.
Code below - thanks in advance if anyone can give me some pointers!
code #1 outputs all terms including parent terms.
code #2 outputs the correct terms but with no images
essentially I'm trying to amalgamate the two.
code #1:
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
'taxonomy' => $current_term->taxonomy,
'child_of' => $current_term->term_id,
'term_args' => array(
'orderby' => 'id',
'order' => 'ASC',
'hierarchical' => 0,
),
);
$cats = apply_filters( 'taxonomy-images-get-terms', '', $args );
foreach ($cats as $cat) {
echo '<li><a href="' . get_category_link($cat) . '" title="'. $cat->name .'">' ;
echo wp_get_attachment_image( $cat->image_id, 'detail' );
echo $cat->name ;
echo '</a></li>';
}
code #2
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$cats = wp_list_categories( array(
'child_of' => $current_term->term_id,
'taxonomy' => $current_term->taxonomy,
'hide_empty' => 0,
'hierarchical' => false,
'depth' => 2,
'title_li' => ''
));
foreach ((array)$cats as $cat) {
$catdesc = $cat->category_description;
echo '<li>'. wp_get_attachment_image( $cat->image_id, 'detail' ) . $cat->cat_name . '</li>'; }
Sorted - used:
'parent' => $current_term->term_id,
instead of:
child_of => $current_term->term_id,
Related
Is it possible to add an image to the gutenber block preview when using ACF to register the block ?
Here's the code to register the block:
acf_register_block(array(
'name' => 'bk-raisons',
'title' => __('Les raisons', 'diezel'),
'description' => __('Les raisons', 'diezel'),
'render_callback' => 'my_acf_block_render_callback',
'category' => 'spira-custom',
'icon' => 'align-wide',
'keywords' => array('bk-raisons'),
));
The preview appears when hovering the block.
Thank you !
I finally found a solution.
I don't know if you use Twig (Timber) or not.
If not check this : https://stackoverflow.com/a/67846162/6696150
For my part with Timber
When you declared your block add example attributes :
$img_quadruple = array(
'name' => 'img-quadruple',
'title' => __('Quatre images'),
'title_for_render' => 'img-quadruple',
'description' => __(''),
'render_callback' => 'ccn_acf_block_render_callback',
'category' => 'imgs',
'icon' => '',
'mode' => 'edit',
'keywords' => array( 'quatre images' ),
'example' => array(
'attributes' => array(
'mode' => 'preview',
'data' => array(
'preview_image_help' => get_template_directory_uri().'/assets/img/preview/preview_img_quadruple.jpg',
),
)
)
);
And when your declared your callback :
function ccn_acf_block_render_callback( $block, $content = '', $is_preview = false ) {
$context = Timber::context();
// Store block values.
$context['block'] = $block;
// Store field values.
$context['fields'] = get_fields();
// back-end previews
if ( $is_preview && ! empty( $block['data'] ) ) {
echo '<img src="'. $block['data']['preview_image_help'] .'" style="width:100%; height:auto;">';
return;
} elseif ( $is_preview ) {
echo 'Other condition';
return;
}
// Store $is_preview value.
$context['is_preview'] = $is_preview;
// Render the block.
Timber::render('gutenberg/gut_' . strtolower($block['title_for_render']) . '.twig', $context );
}
I try to find the code (short code) in the woo comm plugin that made the list of all the product from one category, to be able to modify it... no luck after 1 hours, still no find.
So i start coding it myself (reinventing the wheel) and here what i try to get
get me all the product from category ID="151" and be able to output the name, the permalink etc etc...
this is the code now, that return everything.... way too much ! and i don't know how to filter it
{
$args = array(
'post_type' => 'product',
'posts_per_page' => 99
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//echo get_title()."<br/>";
var_dump($loop);
endwhile;
}
here is the code i have found, and modify to my needs
function get_me_list_of($atts, $content = null)
{
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => $atts[0], 'orderby' => 'rand' );
$loop = new WP_Query( $args );
echo '<h1 class="upp">Style '.$atts[0].'</h1>';
echo "<ul class='mylisting'>";
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<li>'.get_the_post_thumbnail($loop->post->ID, 'thumbnail').'</li>';
endwhile;
echo "</ul>";
wp_reset_query();
}
?>
The [product_category] shortcode defined in /woocommerce/includes/class-wc-shortcodes.php is a great starting place for this, particularly as Woocommerce is constantly evolving!
The guts of it is just a standard WP_Query with some extra code added on to do pagination, setting sort orders from the Woocommerce settings and some checking to see if the products are marked as visible or not.
So if you strip out the shortcode related code and wanted a function that just got visible products from a category with a specific slug, it would look like this:
function getCategoryProducts($category_slug) {
// Default Woocommerce ordering args
$ordering_args = WC()->query->get_catalog_ordering_args();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category_slug ) ),
'field' => 'slug',
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
if ( isset( $ordering_args['meta_key'] ) ) {
$args['meta_key'] = $ordering_args['meta_key'];
}
$products = new WP_Query($args);
woocommerce_reset_loop();
wp_reset_postdata();
return $products;
}
Pass in the slug and you'll get back a standard wordpress posts collection using the same ordering that you've configured in your Woocommerce settings.
I had similar problem as I wanted to get "Fabrics" product for a "Custom Page", here is the code I used.
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'product_cat' => 'fabrics'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
Using the above code means you get the default inline styles, classes and other necessary tags.
I have made a drupal 7 site.
I'm looking for a solution for measuring the authors (blog writers, article writers) efficiensy.
I would like to see someweher, how many nodes are created by a user (author) per month and how many letters do these nodes contain.
Can you suggest me a modul or something else?
Thanks,
Tamás
Here is my solution:
<?php
/**
* Implements hook_menu().
*/
function authors_efficiency_menu() {
$items = array();
$items['admin/config/authors_efficiency'] = array(
'title' => 'Own Settings',
'description' => 'Parent item',
'position' => 'left',
'weight' => -100,
'page callback' => 'system_admin_menu_block_page',
'page arguments' => array(),
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
// Need at least one child item before your section will appear.
$items['admin/config/authors_efficiency/efficiency'] = array(
'title' => 'Authors efficiency',
'description' => 'Authors efficiency',
'page callback' => 'authors_efficiency_page_callback',
'page arguments' => array(),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function authors_efficiency_page_callback($para=array()){
$output= '';
// define number of results per page
$num_per_page = 50;
$header = array('User','Month','Count','Length sum','Avarage length');
// define sql to fetch results
$query= db_select('node','n');
$query->condition('n.status', '1');
$query->condition('n.created', (time()-(120*24*60*60)), '>');
$query->join('field_data_body', 'b', 'n.nid = b.entity_id');
$query->join('users', 'u', 'n.uid = u.uid');
$query->condition('b.entity_type', 'node');
$query->addExpression('CONCAT(YEAR(FROM_UNIXTIME(n.created)),\'-\',LPAD(MONTH(FROM_UNIXTIME(n.created)), 2, \'0\'))', 'node_month');
$query->addExpression('SUM(CHAR_LENGTH(b.body_value))', 'node_char_length');
$query->addExpression('COUNT(*)', 'node_count');
$query->fields('n', array('uid'));
$query->fields('u', array('name'));
$query->groupBy('CONCAT(n.uid, \'#\', node_month)');
$query->orderBy('node_month', 'DESC');
$query->orderBy('u.name', 'ASC');
$count_query= db_select('node','n');
$count_query->condition('n.status', '1');
$count_query->condition('n.created', (time()-(120*24*60*60)), '>');
$count_query->addExpression('COUNT(*)', 'node_count');
$count_query->fields('n', array('uid'));
$count_query->groupBy('CONCAT(n.uid, \'#\', CONCAT(YEAR(FROM_UNIXTIME(n.created)),\'-\',LPAD(MONTH(FROM_UNIXTIME(n.created)), 2, \'0\')))');
$count_query = $count_query->countQuery();
$maxCount = $count_query->execute()->fetchField();;
$page = pager_default_initialize($maxCount, $num_per_page);
$offset = $num_per_page * $page;
$query->range($offset, $num_per_page);
$result= $query->execute()->fetchAll();
$rows = array();
foreach($result as $record){
$row = array(
$record->name,
$record->node_month,
$record->node_count,
$record->node_char_length,
round($record->node_char_length/$record->node_count),
);
$rows[] = $row;
}
// Depending on the context there may be a better choice than this
$output.= theme('table', array('header' => $header, 'rows' => $rows));
$output.= theme('pager');
return($output);
}
?>
Having a little trouble in getting image attachment urls in wordpress. This is my code so far:
<?php // find images attached to this post / page.
global $post;
$thumb_ID = get_post_thumbnail_id( $post->ID );
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'exclude' => $thumb_ID,
'post_parent' => $post->ID
);
$images = get_posts($args);
?>
<?php // Loop through the images and show them
if($images)
{
foreach($images as $image)
{
echo wp_get_attachment_image_url($image->ID, $size='attached-image');
}
}
?>
Which returns nothing. If I swap out wp_get_attachment_image_url($image->ID, $size='attached-image'); for wp_get_attachment_image($image->ID, $size='attached-image'); this works fine, but brings in the image rather than just the url.
The following code will loop through all image attachments and output the src url. Note that there are two methods shown, depending on your need.
<?php
global $post;
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'image', 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
// Method #1: Allows you to define the image size
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {echo $src[0];}
// Method #2: Would always return the "attached-image" size
echo $attachment->guid;
}
}
?>
i had to add custom options automatically when a product is added , the code works fine but i need to create a drop down menu with options and i dont know how to add options to the drop down created ,
my code is
public function Add_CustomOptions_Automatically($observer) {
$product = $observer->getEvent()->getProduct();
$save = false; if (!$product->getOptions()) $save = true;
$optionData = array(
'previous_group' => 'text',
'title' => 'Size',
'type' => 'drop_down',
'is_require' => 0,
'sort_order' => 0,
'price' => 0,
'price_type' => 'fixed');
if($save):
$product->setHasOptions(1)->save();
$option = Mage::getModel('catalog/product_option')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($optionData);
$option->save();
$product->addOption($option);
endif;
}
}
I've created 'type' => 'drop_down' but how can I add options? I have no idea how to add the options, and any help would be very much appreciated.
thanks,
You can supply an array of values to the option array, this will then be added
to the option. as below :-)
$product = Mage::getModel('catalog/product');
$product->load(200); // product id here
$opt = array(
'is_delete' => 0,
'is_require' => false,
'previous_group' => '',
'title' => 'New Example Option',
'type' => 'drop_down',
'price_type' => 'fixed',
'price' => '20.0000',
'sort_order' => 0,
/** array of values for this option **/
'values' => array(
array(
'is_delete' => 0,
'title' => 'Option One Here',
'price_type' => 'fixed',
'price' => 999,
'sku' => 'test-sku-here',
'option_type_id'=> -1,
),
array(
'is_delete' => 0,
'title' => 'Another Option',
'price_type' => 'fixed',
'price' => 999,
'sku' => 'another-sku-here',
'option_type_id'=> -1,
)),
);
$option = Mage::getModel('catalog/product_option')
->setProduct($product)
->addOption($opt)
->saveOptions();
public function Add_CustomOptions_Automatically($observer) {
$product = $observer->getEvent()->getProduct();
$optionData = array(
'previous_group' => 'text',
'title' => 'Size',
'type' => 'drop_down',
'is_require' => 0,
'sort_order' => 0,
'price' => 0,
'price_type' => 'fixed');
if(!(bool)$product->getOptions()):
$product->setHasOptions(true)->save();
$option = Mage::getModel('catalog/product_option')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($optionData);
// Answer starts here
$value = Mage::getModel('catalog/product_option_value');
$value->setOption($option)
->setTitle('Hello world!');
$option->addValue($value);
// Answer ends here
$option->save();
$product->addOption($option);
endif;
}
As you can see you are adding a Mage_Catalog_Model_Product_Option_Value with a title and associating it with the $option you created. It can also have an SKU, price and sort order. Create as many values as you want in this way.
<!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 TransitionalEN" "http:www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http:www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<pre>
<?php
$res = mysql_pconnect('localhost', 'your data base name', 'passward');
mysql_select_db('your database name');
mysql_query("SET NAMES 'utf8';", $res);
mysql_query("SET CHARACTER SET 'utf8';", $res);
$query = 'select entity_id from catalog_product_entity';
$res = mysql_query($query);
$products=array();
while ($ret = mysql_fetch_array($res)) {
$products[]=$ret[0];
$query = "UPDATE catalog_product_entity set has_options=1 where entity_id=$ret[0]";
//echo "$query<br>";
//$res1 = mysql_query($query);
}
echo "Set all products for has_options in catalog_product_entity.<br>";
//$res = mysql_query('DELETE from catalog_product_option');
//$res = mysql_query('DELETE from catalog_product_option_title');
//$res = mysql_query('DELETE from catalog_product_option_type_price');
//$res = mysql_query('DELETE from catalog_product_option_type_title');
//$res = mysql_query('DELETE from catalog_product_option_type_value');
echo "Deleted all rows from catalog_product_option* tables.<br>";
$ress=array();
foreach ($products as $product){
$query = "insert into catalog_product_option (product_id,type,is_require,image_size_x,image_size_y,sort_order) values ($product,'drop_down',0,0,0,0)";
echo "$query<br>";
$res1 = mysql_query($query);
$ress[] = array("option_id" => mysql_insert_id());
}
echo '<pre>';
print_r($ress);
echo "Populated catalog_product_option.<br>";
$option_titles=array('en_US'=> 'Warrenty' );// here change title of option titles #Optional Coating
$res = mysql_query('SELECT * FROM `core_config_data` WHERE path="general/locale/code"');
while ($ret = mysql_fetch_array($res)) {
$stores[$ret['value']][]=$ret['scope_id'];
}
$res = mysql_query('select * from catalog_product_option');// get all product here which agains data inserted
$sort_orders=array(
1,
2,
3,
4);
//while ($ret = mysql_fetch_array($res)) {
foreach ($ress as $key => $ret)
{
//echo '<br>'.$ret[$key]["option_id"];
foreach($stores as $locale=>$scopes){
foreach($scopes as $scope){
$query = "insert into catalog_product_option_title (option_id,store_id,title) values ($ret[option_id],$scope,'$option_titles[$locale]')";
echo "$query<br>";
$res1 = mysql_query($query);
}
}
foreach($sort_orders as $order){
$query = "insert into catalog_product_option_type_value (option_id,sort_order) values ($ret[option_id],$order)";
echo "$query<br>";
$res1 = mysql_query($query);
}
}
echo "Populated catalog_product_option_title.<br>";
echo "Populated catalog_product_option_type_value.<br>";
$prices=array(
0.00,//Standard (12 months)
29.95,//Silver (12 months +loan phone + pic up)
59.95,//Gold(12 months)
89.95//Platinum (24 months +loan phone + pic up)
);
$option_type_titles=array('en_US'=>array(
'Standard (12 months).',
'Silver (12 months +loan phone + pic up).',
'Gold(12 months).',
'Platinum (24 months +loan phone + pic up).'
)
);
$res = mysql_query('select * from catalog_product_option_type_value');
$i = 0;
$j = count($prices)-1;
while ($ret = mysql_fetch_array($res)) {
foreach($stores as $locale=>$scopes){
foreach($scopes as $scope){
$query = "insert into catalog_product_option_type_price (option_type_id,store_id,price,price_type) values ($ret[0],$scope,$prices[$i],'fixed')";
echo "$query<br>";
$res1 = mysql_query($query);
$query = "insert into catalog_product_option_type_title (option_type_id,store_id,title) values ($ret[0],$scope,'{$option_type_titles[$locale][$i]}')";
echo "$query<br>";
$res1 = mysql_query($query);
}
}
($j==$i) ? $i= 0 : $i++ ;
}
echo "<br>Populated catalog_product_option_type_price.<br>";
echo "<br>Populated catalog_product_option_type_title.<br>";
?>
</pre>
</body>
</html>
dear this code is work and i check it . but this code when excute it will add this custom option with all products in your shop. you can change sort ordar and price and option values and also dropdown name . i think its work better.
Try this, its working for me...
$eavSetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,$attributeCode, [
'type' => 'varchar',
'label' => 'My Code',
'input' => 'select',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 101,
'position' => 101,
'system' => 0,
'option' =>
array (
'values' =>
array(
0 => 'January',
1 => 'February',
2 => 'March',
3 => 'April',
4 => 'May',
5 => 'June',
6 => 'July',
7 => 'August',
8 => 'September',
9 => 'October',
10 => 'November',
11 => 'December'
),
),
]);