Page 1 of 1

I need to write a Wordpress Function, but really stuck

Posted: Wed Feb 13, 2019 4:32 am
by simonmlewis
Hi
Not sure if this is the right place to ask this. I've used this forum extensively for PHP coding and learnt a massive amount, but this is about Wordpress.

There are tutorials on how to do functions, but they are all about writing "hello there" in the header or footer of Wordpress; not how to tap into an existing bit of code and edit it.

Here's what we need:
To take the Woocommerce Brands plugin, find it's brand entry (ie. brand-name), and add that into the shop/archive pages, so that you get:

Product Title
£price
Brand name

Maybe someone can help.

Re: I need to write a Wordpress Function, but really stuck

Posted: Thu Feb 14, 2019 5:40 pm
by Celauran
What have you tried so far and where are you getting stuck?

Re: I need to write a Wordpress Function, but really stuck

Posted: Fri Feb 15, 2019 3:23 am
by simonmlewis
This is where I am going blind down an alley, with no idea where any door is.

So I found a function that should extract brand name, but I think it is more for other parts of the site, but it's a start.

Code: Select all

// add brand to archive pages
function bbloomer_show_woocommerce_brands_loop($productBrandTerm) {
    $productBrandTerm = wp_get_post_terms( $post->id, 'product_brand', array("fields" => "name"));
    print_r($post->id);
             echo '<div>here'.$productBrandName.'</div>';
}
add_action( 'woocommerce_after_shop_loop_item_title', 'bbloomer_show_woocommerce_brands_loop', 8 );
I assumed this would gather the product_brand from the array, based on the $post->id in the loop.
Then it would echo that on screen after the title. It does echo 'here'. So the function isn't producing a fatal error.
My issue is I never know how to:

a) find the term within the database and then;
b) how to say 'select term from db where postid = $postid'.

It's not like a mysql query is it, but I REALLY want to learn it. I don't really want someone to just paste in the answer. I want to know that means that, this bit relates to this part of the database, and that renders that.