ACF field display on a page - php

I've got an issue. I've created a custom field via ACF wordpress plugin. Its a field to custom post type categories (lets say its an additional description to the category). I've tried to add it to my page via such code:
$return_html.= '<h2 class="ppb_menu_title" ';
$return_html.= '>'.$menu_term->name.'</h2><br class="clear"/><br/>';
$displaytitle = the_field('category_subtitle');
$return_html.= '<div class="subtitledesc">'.$displaytitle.'</div>';
below code is a part of full page of code which you can find here [lines 1712-1715]:
https://codeshare.io/50QzqL
what i am doing wrong?

get_field() with a single parameter only works on the current post within the loop iirc, so you will have to provide a target if you're trying to get data for a category.
You'll need the termid of your category (when you're on a taxonomy-page, $term = get_queried_object(); $termid = $term->term_id; should work), then use get_field like so:
get_field( 'category_subtitle', "taxonomyname_$termid" );
get_field( 'category_subtitle', $term ); // if you have a term object
Further reading: https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/

You'll want to use get_field() instead of the_field() and include the term ID.
get_field() returns a value.
the_field() echoes a value.
Try this: get_field('category_subtitle', 'term_' . $menu_term->term_id)

Related

Get WooCommerce Product Attribute label name

Variable/variations in woocommerce products.
I can do this to get the attribute value from pa_size: <?php echo $product_variation->get_attributes()['pa_size']; ?> which is somewhere in /wp-admin/edit.php?post_type=product&page=product_attributes.
But how do I get the pa_size label (in this case: 'Size')? Have tried to fetch everything based on post_type, page and then the "term_group". But that won't work. I can see that this usually is visible per default, but this is a custom solution. Also in https://github.com/woocommerce/woocommerce/blob/3.8.0/templates/single-product/product-attributes.php I can't see where they print the actual label, only the "Attribute child label and value". But not actual parent (pa_size => Size).
Have googled like a maniac for hours now.
To get the label name of a WooCommerce product attribute you will use one of the 2 following ways:
1) Using Woocommerce wc_attribute_label() dedicated function:
$taxonomy = 'pa_size';
$label_name = wc_attribute_label( $taxonomy );
2) Using wordPress get_taxonomy() function:
$taxonomy = 'pa_size';
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
In your case, you already know the taxonomy itself pa_size.
All product attributes are just taxonomies, with the pa_ prefix beforehand.
Then you will be able to print them using get_term_by()
it should look something like this:
<?php
$term = get_term_by('slug', $product_variation->get_attributes()['pa_size'], 'pa_size');
echo $term->name;
?>

Get Categories of events in Wordpress Events Calendar Pro Plugin

I'm using Events Calendar Pro plugin (https://theeventscalendar.com/product/wordpress-events-calendar-pro/) and I need to get all categories for each event.
I tried single_cat_title() and get_the_category() but they are not what I needed.
Actually, single_cat_title() function shows only the first category and get_the_category() returns empty array.
You can use following code to get details of each terms.
$cats = get_the_terms( $post_id, 'tribe_events_cat' );
$term = get_term( $cats[1], $taxonomy ); // Getting the 2nd term item
$name = $term->name; //Getting names of terms
More details from https://codex.wordpress.org/Function_Reference/get_term.
Let me know if you have further questions.
I tried single_cat_title() and get_the_category() but they are not what I needed.
get_the_category() function retrieves default categories, post categories. As the categories defined by Events calendar pro plugin is not default, you need to use get_the_terms() function.
In get_the_terms() function, you need to pass post_id as first parameter and taxonomy / category name.
So, wrapping all up you can try below code :-
$event_cats = get_the_terms( $post_id, 'tribe_events_cat' )
Let me know if you need further assistance...
You could try to make a copy of the event.php via the following path:
[your-theme] /tribe/events/v2/list/event.php
and then write the following there:
<?php foreach (get_the_terms(get_the_ID(), 'tribe_events_cat') as $cat) { echo $cat->name; } ?>
And then put it in span/p and styled it.
Inspired from: https://wordpress.stackexchange.com/questions/220511/correct-use-of-get-the-terms

How to display number of posts from a tag by id?

Trying
<?php
$number = 'apartamenty';
$terms = get_terms('post_tag', "number=$number");
if($terms){
foreach ($terms as $t){
echo $t->name.' : '.$t->count;
}
}?>
But this code show two tags...
To get the number of posts that are attached to a specific post tag, such as apartamenty, you would use get_term_by(). This WordPress Core construct lets you grab the WP_Term object for the specific post tag. In your case, you are wanting the post tag of apartamenty.
Okay, you'd have a function (i.e. to make it reusable) that runs this code and you'd pass in the actual post tag that you want to explore. This function is using the post tag slug.
function render_post_tag_post_count( $post_tag ) {
$term = get_term_by( 'slug', $post_tag, 'post_tag' );
if ( ! $term ) {
return;
}
// You get back an WP_Term object
// You can then use $term->count, which gives you the
// number of posts attached to this post tag.
echo (int) $term->count;
}
The property $term->count gives you the number of posts that are attached to that term.
You use it like this:
render_post_tag_post_count( 'apartamenty' );
What if I want to get it by post tag ID?
Take the above code and change the first argument from slug to id. Then you would pass the post tag's ID instead of its slug.
You can learn more about the parameters in WordPress Codex.

target a WooCommerce category's advanced custom field

I'm trying to get the advanced custom field for a WooCommerce category. With the following code I get the woocommerce categories:
$categories = get_terms('product_cat');
var_dump($categories);
But why isn't any ACF info included? Is there another function which do gets the ACF info?
UPDATE
This is outside the loop. So I'm trying to get a custom field of a specific product category. I found this info: http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/
I can't get it to work.
Answer
To get the ACF with get_field(). I needed to use the cat_ID of the array I got with get_categories(). (Maybe it also works with get_terms())
I failed to grasp te second parameter in get_field()
I made it in the following way:
$id = 'product_cat_' . $category->cat_ID;
echo get_field ('field_name', $id);
Based on the documentation of ACF it appears you would get the custom term data as follows:
$category = get_term_by('slug', 'your-category', 'product_cat');
If( ! is_wp_error( $category ) && $custom_field = get_field('your_custom_field', $category ) ){
echo $custom_field;
}
// get the current taxonomy term
$term = get_queried_object();
// vars
$image = get_field('image', $term);
$color = get_field('color', $term);
see documentation here

how to get page id in wordpress if Permalink is Post name

i want the page id of current page in wordpress . i know get_the_ID() which is used to get the page id when Permalink Settings is Default . But in my case Permalink Settings is Post name and i want the page_id is it possible ? . if yes then how ?
Try This:
<?php
global $post;
echo "pageid: ".$post->ID;
?>
You can try this is you have page name
function get_page_id($page_name){
global $wpdb;
$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
return $page_name;
}
get_the_ID() gives you the current ID of a post/page in a loop. Wordpress loads the page or the post in a loop that's why when you run get_the_ID() it gaves you the ID. Now that function has nothing to do with the permalinks. If you're not in any loop (for example you're trying to run that when wordpress is being initialized, you won't get the ID you're looking for because that part is not already set.
get_the_ID() works anywhere no matter what the permalink structure is. The only case i noticed it gives you a result other than the current page or post is when you're already in a loop other that wordpress default's. In that case, get_the_ID() will return the ID of the current post ID in that loop. You can learn more about the loops in the codex.
Now if you're still lost, can you provide a sample of code where you're using that function and you don't get the result you're expecting?
you can use get_page_by_title() if you really want ..
Full parameters are
get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' );
so use like this
$custom_post = get_page_by_title( 'pagename', OBJECT, 'your_custom_post_type' );
$post = get_page_by_title( 'pagename', OBJECT, 'post' );
and just to complete the answer, there is also the similar
get_page_by_path()
$page = get_page_by_path( 'about/us' );

Categories