Hello you are well I hope, I create a shortcode for my wordpress site to display the products everywhere on the site.
My concern is the display of products, I would like to know if there is a specific magniere to have the same design as on the shop page?
I have looked at the content of the page: plugins/woocommerce/templates/content-product.php but I can't use it.
For example:
$products = wc_get_products( array( 'status' => 'publish', 'limit' => -1 ) );
echo wc_show_product($products);
// OU
foreach ($products as $key => $product) {
echo wc_show_product($product);
}
Thank you for your help.
As I said, the goal is to reuse my plus on another site, otherwise I would have just made a loop with the HTML inside.
Related
I really hope you can help me here because it's literally driving me crazy and keeping me from sleeping for 3 days now.
I've been looking everywhere to find a solution to my questioning and everytime I think I'm finally getting somewhere the answers are either too old or the links down, or when someone is facing the exact same issue they never get an answer.
SO. Here is the thing.
In my function.php I created a CPT so my users can create posts with WP User Frontend without polluting my blogs. Now, since the posts are in a CPT, they no longer show up in the built-in Posts tab that Buddypress provides. So, I then added a custom tab to display their posts on their profile.
And now it's getting messy.
I managed to get the posts from the CPT, everything works fine (I know it because if I echo the post thumbnail and title I see them). But I can't figure out how to get the freaking Buddypress template to style the posts list. I think I've read something like more than a hundred threads, I went through Wordpress and Buddypress codex, I even dug into my Buddypress plugin files to find the templates and blog loops and try to understand how it works. But still, nothing.
Here is the code I have so far
function bp_costumes_tab_setup_nav() {
global $bp;
$parent_slug = 'profil-costumes';
bp_core_new_nav_item( array(
'name' => 'Costumes tab',
'slug' => $parent_slug,
'parent_url' => $bp->loggedin_user->domain . $parent_slug.'/',
'screen_function' => 'costumes_tab_show_screen',
'position' => 3,
'default_subnav_slug' => 'profil-costumes'
) );
}
add_action( 'bp_setup_nav', 'bp_costumes_tab_setup_nav' );
function costumes_tab_show_screen() {
add_action( 'bp_template_content', 'bptab_costumes_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function bptab_costumes_content() {
$myposts = get_posts( array(
'posts_per_page' => -1, // set the number of post to show, -1 if all
'author' => bp_displayed_user_id(),
'post_type' => 'cptgalerie',
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish'
));
if( ! empty( $myposts ) ) {
foreach($myposts as $post) {
setup_postdata( $post );
**HERE I WANT THE POSTS FROM CPT TO BE DISPLAYED LIKE IN THE BUILT-IN POSTS TAB**
}
wp_reset_postdata();
} else {
echo '<div class="info" id="message">
<p>Aucun album publiƩ.</p>
</div>';
}
}
As I'm using Elementor Pro I also tried creating a loop and add the shortcode in my php with
echo do_shortcode ("[elementor-template ID='xxx']");
but it gets the template without the post info. It just keeps repeating the user's name (which is technically the "post title" since I used the "post title" widget in my template, I know, but it's not the title I want). Here too I tried to understand how it works by digging into another plugin I use that gets Elementor templates to display posts.
I found other answers where "if ( ! empty...)" is replaced by "if (have posts)... while" but it doesn't seem to work. I've read also that now the "bp_core_load_template" should be replaced by something else... many tests but no results. I also found that it has something to do with some archive template.
I'm lost.
Please, pleeeeeaaaase, can someone help me?
I need to sort it out, get it out of my mind and SLEEP.
Thank you!
I'm trying to set up e-commerce website. Ran into a problem with Categories element.
I want the element to represent not only categories, but also subcategories below itself.
I also cannot edit page without UX builder, I think that's because Flatsome theme elements.
I want something like that :
Category_1 Category_2
Subcat1.1 ___Subcat2.1
Subcat1.2 ___Subcat2.2
Like that:
But, currently I can put categories only. Cannot insert subcategories below. Cannot change Category element.
Currently tried few examples here on stack, using php scripts, but failed.
I ask you for your help, because I don't now where to seek for info I need.
Is it even possible with given circumstances? Or I need to create whole Page without Flatsome, and then add custom element?
Here is the code to get parent and child category.
$parent_cat_arg = array('hide_empty' => false, 'parent' => 0 );
$parent_categories = get_terms('category',$parent_cat_arg);
foreach ($parent_categories as $category) {
echo '<h2>'.$category->name.'</h2>'; //Parent Category
$child_arg = array( 'hide_empty' => false, 'parent' => $category->term_id );
$child_cat = get_terms( 'category', $child_arg );
echo '<ul>';
foreach( $child_cat as $child_term ) {
echo '<li>'.$child_term->name . '</li>'; //Child Category
}
echo '</ul>';
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.
I am creating a little shortcode that acts like the custom menu widget, but I'm choosing the pages from a dropdown list, instead of creating a menus inside the wordpress (I'm also adding a color to it, so I can't just use regular wordpress menus widget).
So far so good, it's just one thing that's bothering me. I want to check if the page I'm on matches the one in my list (on the real page). I googled and searched a bit and found that with
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
I can get the current page I'm on (the link I see in my browser). Perfect!
So if I want to add a class called current page, all I need to see if my $actual_link matches with my selected link from the dropdown list.
I'm working this within a page bulder plugin The Creator, so I know how to create a working shortcode in it. My pages dropdown is created by making an array that will have page url as key and page name as a value. My loop is:
$args = array (
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$pages = get_posts($args);
$forms = array();
if(is_array($pages)){
foreach ($pages as $page) {
$forms[$page->url] = $page->post_title;
}
}
And it works perfectly. Except that the url I get from this is of the form:
http://www.example.com/?page_id=150
Whereas I set my permalinks to be nice so the actual link in my browser is
http://www.example.com/my_page_name
The id is correct, and if I click on my 'menu' link I'll get to that page (desired result). But now I cannot just go and say:
$current = ($actual_link == $link) ? 'current_page' : '';
where $link is the variable that holds the link to the page from the dropdown, so that I can append this to my list to check if I'm on the current page (adds a current_page class). I need this class for the styling purposes - if I'm on the page that has this menu shortcode, next to the link that matches this page I'll get chevron (>).
So my question is, how to get the matching urls, no matter what permalink setting I use? Is there a way to specify this in the get_posts() query, or with the $_SERVER[] variable?
I am trying to avoid javascript with this one, and do everything server side.
You need to place get_the_ID() inside get_permalink() it will return the link which you have to match with $actual_link
$link = get_permalink(get_the_ID());
something like this.
Ok, found the answer.
I changed my query to
$args = array (
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$pages = get_posts($args);
$forms = array();
if(is_array($pages)){
foreach ($pages as $page) {
$forms[$page->ID] = $page->post_title;
}
}
So that I can get page ID, then use
$link_out = get_permalink($link);
$current = ($actual_link == $link_out) ? 'current_page' : '';
And it works! :D
I have a custom post type in Wordpress called People (each post in people is a person with some bio details etc).
Inside each People post I've created a custom field called 'tags' which has a series of checkboxes.
If someone clicks on the System Manager checkbox I want to be able to only pull through all the system managers on a particular page.
The most success I've had is using the wp_get_recent_posts instead of new wp_query .. this will output everybody UNTIL I add my extra meta query search. Then nothing appears.
$people_args = array(
'post_type' => 'people',
'post_status'=>'publish',
'meta_query' => array(
array(
'key' => 'tags', // name of custom field
'value' => 'systemmanager',
'compare' => 'LIKE'
)
)
);
$people_posts = wp_get_recent_posts($people_args);
foreach( $people_posts as $people ){
$fields = get_fields($people["ID"]);
if (get_post_thumbnail_id( $people["ID"] )>0) {
$myimgsp = wp_get_attachment_image_src(get_post_thumbnail_id($people["ID"]),'full');
echo '
<li class="item">
<img src="'.$myimgsp[0].'" alt="'.$people["post_title"].'" class="img-responsive" />
<div class="profile">
<h3>'.$people["post_title"].'</h3>
<p>'.$fields["job_title"].'</p>
View Profile
</div>
</li>';
}
}
wp_reset_query();
I've been stuck on this for hours trying out all the different types of loops but I've had no luck. Has anyone come across this problem before?
If you got stuck on the query maybe try different approach and use the post_id from the query and make small loop inside your code there use get_post_meta(post_id) to check the meta key and get the tags value... something like this...
$meta = get_post_meta($post_ID);
foreach($meta as $key=>$val)
{
if ($key == 'tags')
echo $val;
}
So I'm feeling a little silly! In my functions.php I had forgotten to set that the custom post type should support custom fields. So even though my Advanced Custom Fields were showing .. it wasn't getting pulled through when I was calling it.
To rectify I added to my custom post type config:
'supports' => array('title','thumbnail','custom-fields')