Im trying to fix a plugin that lists post categories in relation to a restaurant menu. I need to somehow add a class to all subcategories. right now it lists categories and subcategories alike. The loop is here
<ul>
<?php
$menu_types = get_terms( 'rm-menu-type');
//list tabs first
$tab_count = 1;
foreach ( $menu_types as $menu_type ) {
$args = array(
'post_type' => 'rm-menu-entry',
'nopaging' => true,
'tax_query' => array(
array(
'taxonomy' => 'rm-menu-type',
'field' => 'slug',
'terms' => $menu_type
)
)
);
?>
<li><?php echo $menu_type->name; ?></li>
<?php
$tab_count++;
}
?>
</ul>
What i am looking for is a way to visually represent what is main, and what is subcategories.
Have you try? With Version 3.0 the taxonomy parameter was added to enable wp_list_categories()
http://codex.wordpress.org/Template_Tags/wp_list_categories#Display_Terms_in_a_custom_taxonomy
Trying to fix the plugin was simply to much hassle, and i decided to redo the functionality with a custom post type and the bootstrap tabs. Works alot better, and will be a lot easier to scale in the future.
Related
My woocommerce store sells books for which there are often several variations - ebook, audiobook, paperback...
I have these set up as variable products. But I would like to add pages where people can see just a single variation - so, all the audiobooks or all the ebooks. This would be linked from a menu.
Is this possible? Or should I just give up and do each one as a simple product? I know there are paid plugins out there, but my turnover is super small and I don't want to make a loss on my store.
I've picked up some understanding of code, but programming is not my thing. If you are kind enough to post something, I will need a teensy bit of hand-holding. So saying that I need to target the shop loop (or something) will leave me floundering. But saying, here is some code, just change X for your variation name and add the css or post in functions.php will be set me on the right path.
Thank you in advance.
Create new template in your active theme. In my case i named it template-audiobook-products.php - Read more about templates here - https://developer.wordpress.org/themes/template-files-section/page-template-files/
Inside the template add the follwoing code
/*
Template Name: Audio books
*/
get_header('shop');
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'variable',
),
array(
'taxonomy' => 'pa_color', //Change to proper attribute
'field' => 'slug',
'terms' => 'blue', //Change to proper value
),
),
'fields' => 'ids'
);
$get_all_variable_products = get_posts($args);
$product_ids_list = array();
foreach($get_all_variable_products as $parent_variation_id):
$_product = wc_get_product($parent_variation_id);
$variable_product_ids = $_product->get_children();
foreach($variable_product_ids as $variable_product_id):
$_variable_product = wc_get_product($variable_product_id);
$_variable_product_attributes = $_variable_product->get_attributes();
if($_variable_product_attributes['pa_color'] == 'blue'):
$product_ids_list[] = array( 'parent_id'=> $parent_variation_id, 'variation_id' => $variable_product_id);
endif;
endforeach;
endforeach;
woocommerce_product_loop_start();
foreach($product_ids_list as $_product_id):
//Since $product is obj we can pass it and use our default content-product template.
$product = wc_get_product($_product_id['variation_id']);
//Keep in mind this template by default doesnt support ajax (use css to hide button or use woo hook/filters)
wc_get_template_part( 'content', 'product');
//In case we want custom design or ajax button uncomment following.
// echo 'Add to cart';
endforeach;
woocommerce_product_loop_end();
get_footer('shop');
Create new page and select this template
To work properly the standart woocommerce template we need to include the classes and other html tags that we may need in our template. Add this function in your active theme functions.php file.
function my_custom_body_class($classes) {
if(is_page_template('template-audiobook-products.php')) {
$classes[] = 'woocommerce';
}
return $classes;
}
add_filter('body_class','my_custom_body_class');
I need to show a Category Product Page (Woocommerce)and grouped them by a Custom Taxonomy which is created the Custom Post Type (CPT) plugin.
Here a capture to see what I mean: https://docs.google.com/drawings/d/1JDxGOO2CCq6aGSUwzows7wtAn9DmshCF7M8g-PdcdUc/edit?usp=sharing
Here the Custom Post Type (CPT) plugin: https://es.wordpress.org/plugins/custom-post-type-ui/
Is there a way to do this? I think of a HOOK for the Category page where I can get the Custom Taxonomy value to group the product and finally print them out... not sure.
Should I start with this?
/***********************************************************/
/********** CATEGORY GROUPED BY CPT TAXONOMY ************/
/***********************************************************/
// define the woocommerce_shop_loop callback
function my_woocommerce_shop_loop( $array, $int ) {
Fetch products and Custom Taxonomies values to group them.
};
// add the action
add_action( 'woocommerce_shop_loop', 'my_woocommerce_shop_loop', 10, 2 );
It's not the exact solution to what you are asking but it's an alternate and it may help, so I'm posting it.
Either using a plugin or not, a way to loop through the terms of your taxonomy and display the items of each term, is the below code
<?php
$terms = get_terms([
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY_HERE',
]);
foreach($terms as $term) {
$args = array(
'post_type' => 'YOUR_CUSTOM_POST_TYPE',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY_HERE',
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$the_new_query = new WP_Query( $args );
if ( $the_new_query->have_posts() ) :
echo '<h1>'.$term->name.'</h1>';
echo '<ul>';
while ( $the_new_query->have_posts() ) : $the_new_query->the_post();
'<li>'.the_title().'</li>';
endwhile;
echo '</ul>';
endif;
}
?>
Building a wordpress site that needs to organize content by stream - e.g Mechanical, Electrical etc. Also, each page needs sections like News, Articles events etc. If you pick one page (say Mechanical) it has to have the following sections
News
Articles (category:articles)
Events (category:events)
The other streams will have the same sections as well
Is there a plugin for achieving or would I be better off building a template page for each vertical and writing php code? Shown code for a page with a single section.
<?php
$args = array(
'posts_per_page' => 1,
'category_name' => 'news',
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args );
$the_query = new WP_Query($args);
//EXAMPLE NEWS SECTION
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_content();
echo $content;
}
} else {
// no posts found
}
?>
In my opinion you could just write a plugin which does that filtering.
Said plugin would have some kind on shortcode that would take a parameter (category for instance) and would return or echo all the posts associated with that category.
Shortcode registration :
add_action('init','register_shortcode');
function register_shortcode(){
add_shortcode('shortcode_name', 'shortcode_function');
}
Shortcode function:
function shortcode_function($attr){
$attr = shortcode_atts(array('category' => 'default'),$attr); //Setting defaults parameters
$args = array(
'category_name' => $attr['category'],
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//echo other html related stuff
the_title();
the_content();
}
}
}
Usage
[shortcode_name category='news']
[shortcode_name] //category defaults at 'default'
In your case a page could be something like that
<div>
News : <br/>
[shortcode_name category='news']
</div>
<div>
Articles : <br/>
[shortcode_name category='articles']
</div>
<div>
Events : <br/>
[shortcode_name category='events']
</div>
I don't think there is one right way to do this... it depends on the scenario... How many posts, how often do sections or "main streams" get added / change, do you need other stuff on the pages etc.
One pretty simple and easily maintained solution would be to:
1. Add mechanical, electrical, etc. as categories alongside news, articles and events
2. Modify (or add) category.php to repeat the loop three times, and only display the posts that belong to each section (other category):
//Loop your sections
foreach(array('news', 'articles', 'events') as $category) {
echo '<h2>' . $category . '</h2>';
//Loop posts
while ( have_posts() ) {
the_post();
//Check if post is in section (by slug!)
if ( in_category($category) ) {
the_title();
the_content();
}
}
//Start the loop over
rewind_posts();
}
Now just assign each post to one (or more) "parents", fx mechanical, and also to one (and only one) of news, articles or events...
If you go to the archive page of mechanical you get three sections
if you go to news you get all news (but also two empty sections, so you should of course check for that).
Note that this could be done using tags or even a custom taxonomy if you wanted, you'd just need to edit a different theme file - see the template hierarchy.
Beware though that this will not work very nicely with pagination, so if that is a concern you will need to deal with that.
is there any way to active portfolio section by adding some code to functions of theme ?
i saw some themes that have this feature , that themes add a new section named portfolio to back-end of Word-press
You can use custom post type to accomplish what you want.
WordPress can hold and display many different types of content. A
single item of such a content is generally called a post, although
post is also a specific post type. Internally, all the post types are
stored in the same place, in the wp_posts database table, but are
differentiated by a column called post_type.
PHP EXAMPLE:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'portfolio',
array(
'labels' => array(
'name' => __( 'Portfolios' ),
'singular_name' => __( 'Portfolio' )
),
'public' => true,
'has_archive' => true,
)
);
}
Then to add it in your theme you can use WP_Query.
EDIT:
WP_Query Example
$args = array(
'post_type' => 'portfolio'
); // these arguments are telling WP_Query to only look for the post types called portfolio.
$query = new WP_Query( $args );
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
the_post_thumbnail();
<?php endwhile; ?>
<!-- end of the loop -->
Ask me for any confusions.
NOTE: I am showing you a method without using any plugins. A custom approach.
Situation
I am using the Advanced Custom Fields plugin to give Custom Post Types a checkbox. If this box is checked, it should be returned by this Query. The Query works this far.
The issue
The pages I want it to display are all childs of a page. And this Query returns all pages with the checkbox, instead of just the childs of the current page with the checkbox.
My code
top of page
<?php $tid = get_the_ID(); ?>
Further along the page
<?php
$i == 0;
global $post;
$myposts = new WP_Query(array(
'post_type' => 'keuringen',
'child_of' => $tid,
'meta_query' => array(
array(
'key' => 'first_to_load',
'value' => '1',
'compare' => '=='
)
)
));
?>
The question
What can I do to make it return only the children? Or should this already return only the children and is this a bug in one of the plugins?
Use post_parent instead of child_of in WP_Query