Retrieve Wordpress icon box titles (plugin) - php

I'm using WordPress plugin icon box developed by husamrayan.
In my use case I make categories so every category contains few icons and every icon has its own title; and I can get the category in post with acf (advanced custom field) about its id, name, amount... etc.
But how could I get every icons' title belonging to the category?
For example, in the picture:
How could I get titles of icons belonging to shop_cate3
as I already get shop_cate3 info from acf by get_field('fields_name');
-------plus info-------
I use both plugins 'acf' and 'icon box'.
Here how do I setup iconbox as a taxonomy and custom field name 'shop_service', which I could define post related to one of the categories and have the category contain some icons.
Then in single.php, I retrieve custom field data by acf function:
$terms = get_field('shop_service');
Then if I parse the data inside $terms it would be:
{
"term_id":31,
"name":"shop_cate_3",
"slug":"%e5%ba%97%e9%9d%a23",
"term_group":0,
"term_taxonomy_id":31,
"taxonomy":"icoonboxcategory",
"description":"",
"parent":0,
"count":4,
"filter":"raw"
}
But I cannot retrieve icons related to this category.

For ppl encounter same issue, I sent mail to author, he said it's not a feature in plugin now, but I could check it in inc/shortcodes.php.
Then here is the solution:
$shop_service = get_field('shop_service');
$category = $shop_service->term_id;
$args = array ( 'post_type' => 'icoonbox',
'posts_per_page' => $num,
'orderby' => $orderby,
'order' => $order,
'suppress_filters' => true);
if($category > 0) {
$args['tax_query'] = array(array('taxonomy' => 'icoonboxcategory','field' => 'id','terms' => intval($category) ));
}
$icon_titles = array();
$icoonbox_query = new WP_Query( $args );
if ($icoonbox_query->have_posts()) {
$posts = $icoonbox_query->posts;
foreach ($posts as $key => $item) {
$pair = array();
$pair['title'] = $item->post_title;
$pair['id'] = $item->ID;
array_push($icon_titles, $pair);
}
}
Add the code above to where you want icons id and titles, all the data you need would be in $icon_titles!
Sorry maybe the code has unnecessary part due to I don't really master php language.

Related

Woocommerce how to display relevant tags under category title

I want to display meta tags under each category title. I can see there is this code to display product tag lists for each product, but what I really want is product tags for each category and then display it under the category title in the page.
<?php
global $product;
?>
<div class="product-tags">
<?php
echo wc_get_product_tag_list( $product->get_id(), ', ' );
?>
</div>
Example screenshot:
Well, you already know/have the category name (i.e 'Coffee Equipment'), so you could use that to get the relevant tags, but in order to do so, we'll create a function in the functions.php of your active theme, like so:
The following code goes to your functions.php file of your active theme:
function your_theme_get_tags_based_on_cat($cat_name)
{
$cat_id = get_cat_ID($cat_name);
$tag_query = new WP_Query(array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cat_id,
'operator' => 'IN'
)
)
));
$all_tags = array();
while ($tag_query->have_posts()) {
$tag_query->the_post();
$producttags = get_the_tags();
if ($producttags) {
foreach ((array)$producttags as $tag_obj) {
$all_tags[] = $tag_obj->term_id . '-' . $tag_obj->name;
}
}
};
$tags_array = array_unique($all_tags);
$new_array = array_map(function ($val) {
return explode("-", $val);
}, $tags_array);
return new_array;
}
The function above will return an associative array containing tag id and tag name of the corresponding tags of your PRODUCT category.
Side Note:
if you need to use it for the blog posts of your wordpress site, then you could change/modify the query by swapping 'post_type' => 'product' argument with 'post_type' => 'posts'. So your query for blog posts would be something like this:
$blog_tag_query = new WP_Query(array('post_type'=>'post','post_status' =>'publish','cat'=>$cat_id,'posts_per_page'=>-1));
If you decide to use it for your blog posts, also remember to change the get_term_link((int)$tag[0], 'product_tag') with get_term_link((int)$tag[0], 'post_tag') in your template.
Now you have a magical function :) that you can use anywhere that you need a list of tags for a specific category!
So let's use our function in your page template to populate the corresponding tags for the current category, like so:
$cat_name = 'Coffee Equipment';
$tags_array = your_theme_get_tags_based_on_cat($cat_name);
foreach ($tags_array as $tag)
{
echo '<a class="item" href="' . get_term_link((int)$tag[0], 'product_tag') . '">' . $tag[1] . '</a>';
};
Just tested and it works seamlessly fine! Feel free to customize it as needed on your html template/markup.

Create a shortcode to display related posts (posts in the same category) in the sidebar

I want to display in the article sidebar (in wordpress) a list of 5 recent articles from that category to which it belongs. I'm using the code below to (using a shortcode) show the 5 posts (from category 62 in this case). Is there a way to write this code in functions.php so that it is optimised, and I don't have to rewrite everything for each new category?
/**
* function to add recent posts widget
*/
function wpcat_postsbycategory_musculacao() {
// the query
$the_query = new WP_Query( array( 'cat' => '62', 'posts_per_page' => 5 ) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbytag widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<li>';
$string .= '' . get_the_post_thumbnail($post_id, array( 80, 80) ) . get_the_title() .'</li>';
} else {
// if no featured image is found
$string .= '<li>' . get_the_title() .'</li>';
}
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts-musculacao', 'wpcat_postsbycategory_musculacao');
/**
* function to change search widget placeholder
*/
function db_search_form_placeholder( $html ) {
$html = str_replace( 'placeholder="Pesquisar ', 'placeholder="Buscar ', $html );
return $html;
}
add_filter( 'get_search_form', 'db_search_form_placeholder' );
The complication here is that in WP a post can have multiple categories. You need to decide how to handle that - get from all categories, or if you only want to show posts from one category, how do you choose which?
I've given a few answers below depending on how you want to handle that.
1. Get posts in any of the categories of the current post
As posts can have multiple categories, you can get all of the ids and use this to query for posts that are in any of those categories:
// 1. get the categories for the current post
global $post;
$post_categories = get_the_category( $post->ID );
// 2. Create an array of the ids of all the categories for this post
$categoryids = array();
foreach ($post_categories as $category)
$categoryids[] = $category->term_id;
// 3. use the array of ids in your WP_Query to get posts in any of these categories
$the_query = new WP_Query( array( 'cat' => implode(",",$categoryids), 'posts_per_page' => 5 ) );
1a. Get posts in any of the categories but not their children
Note cat will include children of those category ids. If you want to include those exact categories only and not children, use category__in instead of cat:
$the_query = new WP_Query( array('category__in' => $categoryids, 'posts_per_page' => 5) );
2. Get posts that have all of the categories of the current post
If you want posts that have all the same categories as the current one, this is done in the same way as above, except we use category__and instead of cat (Note that this does not include children of those categories) :
$the_query = new WP_Query( array('category__in' => $categoryids, 'posts_per_page' => 5) );
3. If you know your post will only have one category
If you know you only have one category per post, then you can just use the first element from the category array:
// 1. Just use the id of the first category
$categoryid = $post_categories[0]->term_id;
$the_query = new WP_Query( array( 'cat' => $categoryid, 'posts_per_page' => 5 ) );
4. Pass the category into the shortcode
If you want to specify which category to show, you can pass the category id into the shortcode, letting you choose whichever category you want. (FYI you can also get it to work with slug instead of id, which might be a bit more user-friendly)
Your shortcode is currently used like this, I assume:
[categoryposts-musculacao]
We can change the function so you can pass the category like this:
[categoryposts-musculacao category=62]
Shortcode function can accept an $attributes argument that has the information or "attributes" we're adding to the shortcode, e.g. category. We use this to get the category passed in as a variable called $category and then just use it instead of the hardcoded value in the rest of your function.
// 1. include the $attributes argument
function wpcat_postsbycategory_musculacao( $attributes ) {
// 2. get the value passed in as category - this will save it into a variable called `$category`
extract( shortcode_atts( array(
'category' => ''
), $attributes ) );
// 3. if there is no category don't do anything (or sohw a message or whatever you want
if (!$category) return "";
// 4. Now just use your variable instead of the hardcoded value in the rest of the code
$the_query = new WP_Query( array( 'cat' => $category, 'posts_per_page' => 5 ) );
// do the rest of your stuff!
}
Reference: Wordpress Codex Shortcode API
4a. Pass the category into the shortcode by slug
If you want the category attribute in your shortcode to work with a slug instead of the id, you just need to change WP_Query to use category_name instead of cat:
$the_query = new WP_Query( array( 'category_name' => $category, 'posts_per_page' => 5 ) );

WP only display categories that contain posts from that post type

In wordpress I've created 2 custom post types Services & Work which can use the wp default category and tag taxonomies.
On any given single post page I need to list the categories available for that post type.
I've tried using
$args = array( 'hide_empty' => 1, 'taxonomy' => 'category' ); wp_list_categories( $args );
to list only those categories with posts associated but the list doesnt take the post types into account.
How would I only list the categories being used by that post type?
Question answered here:
Put the following in your functions.php:
function wp_list_categories_for_post_type($post_type, $args = '') {
$exclude = array();
// Check ALL categories for posts of given post type
foreach (get_categories() as $category) {
$posts = get_posts(array('post_type' => $post_type, 'category' => $category->cat_ID));
// If no posts found, ...
if (empty($posts))
// ...add category to exclude list
$exclude[] = $category->cat_ID;
}
// Set up args
if (! empty($exclude)) {
$args .= ('' === $args) ? '' : '&';
$args .= 'exclude='.implode(',', $exclude);
}
// List categories
wp_list_categories($args);
}
Now you can call wp_list_categories_for_post_type('photos'); or
wp_list_categories_for_post_type('videos',
'order=DESC&title_li=Cats'); and the like.

Match two different post types and add to array wordpress

I have two different custom post types, portfolio and project.
On the single-project.php page, I want to display content where get_field('housetype') in project matches with the taxonomy of the portfolio. The content comes from the project custom post type.
So far I've managed to get all relevant information but I cant find a way to only display the result where there's a match between the housetype and the taxonomy.
I thinking that pushing the item in an array when it's a match and then using a foreach loop to display the content in a table. I just don't know how to do this..
Here's my code so far:
$args = array(
'post_type' => array( 'portfolio', 'project' ));
$my_query = new WP_Query( $args );
if( $my_query->have_posts()) {
while ($my_query->have_posts()) : $my_query->the_post();
//Returns All Term Items for "my_taxonomy"
$term_list_portfolio = wp_get_post_terms($post->ID, 'portfolio_category', array("fields" => "all"));
// Just get the category
$category = $term_list[0]->name;
// Get the house type
$houseType = get_field('hustyp');
$result = array($term_list[0]->name, get_field('hustyp'));
endwhile;
}
You would have to check with an if statement somewhere.
Something similar to this:
// Get the house type
$houseType = get_field('hustyp');
if($houseType == $term_list[0]->name){
//Here you could save your post object
//(that you declare before your while loop) to an array
//or render what you want directly
$posts[] = $post;
}
$result = array($term_list[0]->name, get_field('hustyp'));

Wordpress show multiple post types in loop

I'm creating a Wordpress site were I would like to show "tiles" with content from the site on the front page. These tiles are custom post types from the site like "our services", "consultants", "blog posts" and so on.
I know how to show one custom post type in Wordpress, but the problem is that I need to pull multiple post types in the same loop as I want them to be displayed in a matrix. Another problem is that I need to shuffle all the items in a random order, so that for example not all blogs just show in one place but all objects show after different items in random.
The third problem is that I need to show all items for a certain post type and just the latest for another. For example do I need to show all "our services" tiles, but only a couple of the "blog" tiles.
Is this possible to do, or can you not pull out records in this way using Wordpress?
Thank you for the help!
I suggest reading up on custom wordpress queries https://codex.wordpress.org/Class_Reference/WP_Query
For the first question you just need to specify
'post_type' => array( 'tiles', 'consultants', 'post' )
for the second question
'orderby' => 'rand'
so you will have something like
$args = array(
'post_type' => array( 'tiles', 'consultants', 'post' ),
'orderby' => 'rand'
);
$query = new WP_Query( $args );
For the third question - I'm not sure if it is possible to achieve with one query.
you can customise the things like this ,
$posttypes = array('post_typ1','post_typ2','post_typ3');
$randompost_typs = shuffle($posttypes);
$counter = count($posttypes);
for($i=0; $i<$counter;$i++) {
// suppose you want to show all posts from post_type1 then
if($randompost_typs[$i]=='post_typ1') {
$posts_per_page = -1;
} elseif($randompost_typs[$i]=='post_typ2') { // will work for 2nd post type
$post_per_page = 5; // show 5 posts from this post type
} else {
$post_per_page = 3; // show 3 posts from last post type
}
// here you will use the WP_Query class from wordpress
$args = array(
'post_type' => $posttypes[$i],
'orderby' => 'rand',
'posts_per_page' => $post_per_page
);
$query = new WP_Query( $args );
if($query->have_posts()) : while($query->have_posts()): $query->the_post();
// all the remaining wp loop content for example
the_title();
the_excerpt();
endwhile;
else:
echo 'no posts';
endif;
}
hope this will help, let me know if it has any issue.

Categories