I am using the Category and Taxonomy Meta Fields plugin to create some necessary fields in the product categories.
When trying to display this information in php, it does not appear.
$cate = get_queried_object();
$cateID = $cate->term_id;
if (function_exists('get_all_wp_terms_meta'))
{
print_r( get_all_wp_terms_meta($cateID) );
}
in page archive-product.php, return Array ( )
Use the plugin Advanced Custom Fields
In file archive-product.php use:
$cateID = get_queried_object();
$return = get_field('NameField', $cateID);
if using a flexible field, name the layout in the field settings and use
if ( have_rows( 'NameItem', $cateID ) ) :
while ( have_rows( 'NameItem', $cateID ) ) : the_row();
if( get_row_layout() == 'NameLayout' ):
echo get_sub_field('NameFiel');
endif;
endwhile;
else :
Related
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.
Before, sorry for my bad grammar/English and bad code understanding
I'm testing a new website about short or series story with custom post type generated by Writeshare plugin. What I would like to achieve on my dynamic page is like this
Custom term (term name: Short Stories | slug: short term | tag ID: 2) from custom taxonomies (tax name: Format)
Post | Post | Post | Post | Post
Custom term (term name: Series | slug: series | tag ID: 3) from custom taxonomies (tax name: Format)
Post | Post | Post | Post | Post
My website is: https://qisa.xyz/ | and I'm using justread theme https://wordpress.org/themes/justread/
but I always failed when try to adding the tax to the loop, while custom post type (called WPWS content) successfully added to the home page via pre_get_post in functions.php
I've been trying modified the code trying to only get post from needed taxonomy in pre_get_post function and loop but nothing worked so far.
Now my code is consisting default first loop (all content from custom post type, worked perfectly) and second loop (modified to specific taxonomy, as I write this thread in 'nothing found' status
And what the best approach to achieve this. Adding new WP_query? or through pre_get_post function?
code in function.php
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'wpws_content' ) );
return $query;
}
now code in index.php
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) : the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</main><!-- #main -->
<?php the_posts_pagination(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main grid grid--4">
<?php $temp_query = clone $wp_query; ?>
<?php query_posts( 'taxonomy_name=short' ); ?>
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( $temp_query->have_posts() ) : $temp_query->the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
Update:
I tried implementing the solution from Manoj Webvertex, but that was sent me into technical error. Where did the code go wrong?
<?php
$post_type = 'post';
// Get all the taxonomies for this post type $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$posts = new WP_Query( "taxonomy=$format&term=$short->slug&posts_per_page=5" );
if( $posts->have_posts() ):
while( $posts->have_posts() ) : $posts->the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
<?php wp_reset_query(); ?>'
<?php
$post_type = 'post';
// Get all the taxonomies for this post type $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );
if( $posts->have_posts() ):
while( $posts->have_posts() ) : $posts->the_post();
//Do you general query loop here
endwhile;
endif;
endforeach;
endforeach;
I am developing a wordpress website which uses woocommerce for e-commerce functionality. I have 3 categories on the website and each one will have it's own template assigned for products within these categories.
I have created the templates and have got two of them working fine. However I'm not sure how to call the third template within my single-product.php file which contains the following code to change the templates depending on what category the product is assigned to:
<?php while ( have_posts() ) : the_post(); ?>
<?php global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'legal', $categories ) ) {
woocommerce_get_template_part( 'content', 'single-product-legal' );
} else {
woocommerce_get_template_part( 'content', 'single-product-merc' );
} ?>
<?php endwhile; // end of the loop. ?>
the templates i have are:
single-product-legal (custom template)
single-product-merc (default woocommerce template)
single-product-show (custom template)
The categories are legal, show and merchandise.
I need help with the php code so I can switch between the 3 templates. Not sure if I should use a switch statement, or how to implement it or if I could use elseif or how to implement that. Even if there's a completely different way to achieve this, I'd love to know.
Any pointers would be appreciated.
The best it to use elseif:
if ( in_array( 'legal', $categories ) ) {
woocommerce_get_template_part( 'content', 'single-product-legal');
}elseif (in_array('show', $categories)){
woocommerce_get_template_part('content', 'single-product-show');
}else {
woocommerce_get_template_part( 'content', 'single-product-merc');
}
Rather than adding a seperate elseif for merchandise you can just do it in the else like above because by default if it is not legal or show it must be merchandise, but you may just add another elseif as well with the same basic structure.
I have 1 single post page template which will display 2 different taxonomy terms, and I like to call 2 different sidebar for these 2 different terms. This post template is "single-recipe.php", in which I call "content-single_recipe.php". In "content-single_recipe.php", I call for 2 different sidebars base on the different terms with a condition statement:
SINGLE-RECIPE.PHP
while ( have_posts() ) : the_post();
get_template_part( 'content', 'single_recipe' );
endwhile; // end of the loop.
CONTENT-SINGLE_RECIPE.PHP
php the_content();
// Here are code for sidebars:
$term = get_the_term_list($post->ID, 'recipe_type');
if ( $term = 'salad' ){
dynamic_sidebar('sidebar-salad');
}elseif($term = 'sandwich'){
dynamic_sidebar('sidebar-sandwich' );
}
However, no matter what the $term is, it always call the "sidebar-salad".
get_the_term_list gives you HTML list of terms. Use has_term instead. And comparing is done with ==, not =. Since you use =, which is assigning value to variable, you first if will always be true.
if( has_term( 'salad', 'recipe_type', $post->ID ) ){
dynamic_sidebar('sidebar-salad');
}
elseif( has_term( 'sandwich', 'recipe_type', $post->ID ) ){
dynamic_sidebar('sidebar-sandwich');
}
// Sidebars
if(is_front_page())
$op_sidebar_id = __('Sidebar Home','options');
// Single page and post type sidebars
elseif(is_attachment())
$op_sidebar_id = __('Sidebar Attachment','options');
elseif(is_single())
$op_sidebar_id = __('Sidebar Single','options');
elseif(is_page())
$op_sidebar_id = __('Sidebar Page','options');
else
$op_sidebar_id = __('Sidebar Home','options');
After that, I add the normal widgetized section with the variable set:
// If using the No Sidebar template
if(is_page_template('no-sidebar.php')) :
echo '';
// Else, show the sidebar
else :
echo "";
if(function_exists('dynamic_sidebar') &&
dynamic_sidebar($op_sidebar_id)) :
else :
// Default to Home page sidebar if no widgets are set
if(function_exists('dynamic_sidebar') && dynamic_sidebar(__('Sidebar
Home','options'))) :
else : _e('Add content to your sidebar through the widget control
panel.','options');
endif;
endif;
echo '</div>';
endif;
I am using ACF to build a product catalogue.
I need to create a list like that:
+Category name
-list
-of
-products
-in
-that
-category
+Another category name
-list
-of
-...
-in the matching category
For all of the categories.
Categories names are the terms of the 'product_categories' taxonomy. The products belong to a custom post type 'products'.
I've already build a loop that displays links to all of the categories with names and images:
$terms = get_terms("product_categories", array('hide_empty' => false));
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo '<a href="'.get_term_link($term).'">';
echo '<p>'.$term->name.'</p>';
echo '<img src="'.get_field('image-field-name', $term).'"></a></div>';
}
}
It works fine.
Then I rebuilded it to display the thing i want:
$terms = get_terms("product_categories", array('hide_empty' => false));
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$category= $term->name;
echo '<p>'.$category.':</p>';
product_list($category);
echo '<br/><br/>';
}
}
function product_list($category_name){
$inner_args=array(
'post_type' => 'products',
'product_categories' => $category_name
);
$my_query = null;
$my_query = new WP_Query($inner_args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php echo '<br/>Product: '.get_field('name_of_the_product');
?>
<?php
endwhile;
}
}
It almost works. The problem is that it displays all of the product names BUT only for the first category, after all of the products from the first category in loop are echoed, following categories are empty (just the category names, no matching items).How can I make it display products in all of the categories, not just the first one? And what's wrong with the code?
Have you tried adding wp_reset_query(); before the end of the foreach loop?
More info http://codex.wordpress.org/Function_Reference/wp_reset_query