WordPress: Get Yoast SEO title for custom taxonomy - php

I'm trying to get the SEO title from a custom taxonomy.
Here's my current code for it:
$my_yoast_wpseo_title = get_term_meta( $term_id, '_wpseo_title', true );
if( $my_yoast_wpseo_title ){
echo $my_yoast_wpseo_title;
} else {
echo 'No title';
}
It doesn't work.
So I tried different meta keys like _yoast_wpseo_title and everything I could find in their docs and other snippets.
Nothing works.
So I checked the complete output of get_term_meta. Like this:
$my_yoast_wpseo_title = get_term_meta( $term_id );
print_r($my_yoast_wpseo_title);
It shows a lot of meta fields. But the Yoast meta isn't stored there?!
Is their any other place?
The taxonomy has a custom SEO title and shows it in the frontend.

The Yoast SEO Titles for Terms are stored in the options table.
This will at least give you the idea you're after.
$options = get_option( 'wpseo_taxonomy_meta' );
// $options will be an array of taxonomies where the taxonomy name is the array key.
$term_id = get_queried_object_id();
foreach ( $options['your_term_here'] as $id => $option ) {
if ( $term_id === $id ) {
/* This returns an array with the following keys
'wpseo_title'
'wpseo_focuskw'
'wpseo_linkdex'
*/
echo ( ! empty( $option['wpseo_title'] ) ) ? $option['wpseo_title'] : 'no title';
}
}

I found another way to do this:
$my_yoast_wpseo_title = WPSEO_Taxonomy_Meta::get_term_meta($id, 'produkt_vendor', 'title');
if( $my_yoast_wpseo_title ){
echo $my_yoast_wpseo_title;
} else {
echo 'No title';
}
You can used that with title and desc without the prefix wpseo_

$term = get_queried_object();
$options = get_option( 'wpseo_taxonomy_meta' );
echo $options[$term->taxonomy][$term->term_id]['wpseo_title']; //Meta Title
echo $options[$term->taxonomy][$term->term_id]['wpseo_title']; //Meta Description
This is another solution to get the meta title and meta description out of yoast seo in the taxonomy. Paste this code into archive.php or taxonomy.php of your theme.

Related

Getting custom taxonomy tags based on get_the_ID() (Outside for loop)

I have a custom taxonomy called type. Type has the following options:
Blog
News
Training
I've created a draggable element (in Visual Composer) which will show these tags. Because of this, the draggable element is not part of archive-resources.php, so I can't run it through a loop.
What I'm trying to do is:
Get blog post ID (which I've done via get_the_ID().
Display type that is assigned that blog post.
However, currently, all three type tags are displaying. Where am I going wrong?
$blogpostID = get_the_ID();
$termType = get_terms('type');
$output = '';
foreach ( $termType as $termT ) {
echo $output . ''.$termT->name.'';
}
Ok, Do it this way :
<?php
$terms = get_the_terms( $post->ID , 'type' );
$output = '';
if ( $terms != null ){
foreach ( $terms as $term ) {
echo $output . ''.$term->name.'';
}
}

Usable woocommerce template for custom taxonomy

I created a custom taxonomy book-author for my Woocommerce store, and now I'm trying to conjoint an archive template for it to display frontend like normal Woo archive. Yesterday I found this code, which helped me bring the taxonomy out of the 404 error when clicked, but it returned a shop page with No product notice (though I'd clicked on one of the existed taxonomies).
The point is, book-author taxonomy is a mother of small tags, or "authors", so I need to fix this tag or find a way to make it universal to the mother book-author and all its kids/authors.
add_filter('template_include', 'team_set_template');
function team_set_template( $template ){
if(is_tax('book-author')) :
$taxonomy = 'book-author';
$term = get_query_var($taxonomy);
$prod_term = get_terms($taxonomy, 'slug='.$term.'');
$term_slug = $prod_term[0]->slug;
$t_id = $prod_term[0]->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$term_meta['bookauthor_access_pin'];
wc_get_template( 'archive-product.php' );
else :
wc_get_template( 'archive-product.php' );
endif;
}
I've tried copying archive-product.php, renaming it taxonomy-book-author.php and putting it in my child theme folder. This seems to be a more better approach, but there was no result - still 404.
The reason why book-author is a tag, not a category because there is no hierarchy for an author. And I know there's plugin for this (Toolset), but they upgraded there free version to paid ones so I'm trying to find a more manual and permanent way.
Thank you in advance, guys.
There is many errors just when reviewing and trying your code…
Note: A filter hooked function needs always to return something.
In your code:
- get_query_var($taxonomy) will return always a term "slug"
- $term_meta['bookauthor_access_pin']; is not usefull and I really don't know what is for.
You say "The reason why book-author is a tag, not a category"… If you have created a custom taxonomy book-author, this can't be a product category or either a product tag (woocommerce custom taxonomies)…
So you should try the following code instead (without any guaranty, as you don't provide all your related code, and this can't be tested):
add_filter('template_include', 'team_set_template');
function team_set_template( $template ){
$taxonomy = 'book-author';
if( ! is_tax( $taxonomy ) ) return $template;
$term_slug = get_query_var($taxonomy);
if ( empty( $term_slug ) ) return $template;
$term = get_term_by('slug', $term_slug, $taxonomy );
if ( is_wp_error( $term ) ) return $template;
$term_id = $prod_term->term_id;
$term_meta = get_option( 'taxonomy_'. $term_id );
// $term_meta['bookauthor_access_pin']; // ???
return wc_locate_template( 'archive-product.php' );
}

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

woo commerece short codes not working on all posts

I have created a short code to display short description in woo commerce but it is not working on all posts. It is displaying the short description on some posts and not on others.
Function to create that short code in functions.php
function product_shortdesc_shortcode( $atts ){
// use shortcode_atts() to set defaults then extract() to variables
extract( shortcode_atts( array( 'id' => false ), $atts ) );
// if an $id was passed, and we could get a Post for it, and it's a product....
if ( ! empty( $id ) && null != ( $product = get_post( $id ) ) && $product->post_type = 'product' ){
// apply woocommerce filter to the excerpt
echo apply_filters( 'woocommerce_short_description', $product->post_excerpt );
}
}
// process [product_shortdesc] using product_shortdesc_shortcode()
add_shortcode( 'product_shortdesc', 'product_shortdesc_shortcode' );
The way i am getting the data in my single.php file
$custom = get_post_custom(get_the_ID());
$my_custom_field = $custom['woo_id'];
foreach ( $my_custom_field as $key => $value ) {
echo do_shortcode('[product_shortdesc id='.$value.']');
}
PS: in my normal post i have a custom field which has the value of product id of the product in woo commerece.
Your issue is that you are expecting shortcodes to function which no longer exist. On new installs, these pages won't be created, but if you are updating you may already have those pages in place.
Although the upgrade script does attempt to trash them for you, this might not have happened if they were customised. Delete them. Delete edit-account and change password, then go to your 'my account' page and click the change password/edit account links. You'll be taken to and endpoint which offers the same functionality.
Thanks
Short Code must not echo code instead return the things that needs to be rendered
Change this
echo apply_filters( 'woocommerce_short_description', $product->post_excerpt );
to
return apply_filters( 'woocommerce_short_description', $product->post_excerpt );

Display Custom Taxonomy Field in sidebar

Hi Guys, I'm trying to display a taxonomy field called clpr_store_video in my Wordpress sidebar for each store is it possible to call the field value outside the loop on the sidebar, but yet attached to the post id?
I don't really know if its possible or how to do that.
EDITED
What I've tried from another tutorial but no luck.
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$subheading = get_field('clpr_store_video', $taxonomy . '_' . $term_id);
echo $subheading;
See code block to display taxonomy associated with post and list of taxonomies and their links:
If you wish to get taxonomy associated with particular post
$terms = get_the_terms( get_the_ID(), 'clpr_store_video' );
foreach ( $terms as $term ) {
echo $subheading = $term->name;
}
If you wish to get list and link of all taxonomies
<ul>
<?php
$taxonomies = get_terms('clpr_store_video', array("fields" => "all"));
foreach ($taxonomies as $taxonomy) {
echo '<li>'.$taxonomy->name.'</li>';
}
?>
</ul>

Categories