I'm trying to display a taxonomy description based on which taxonomy the page is set to display (set with a variable)
As it is, I'm only seeing the first description, regardless of which category variable is set.
Here's how I've set it up:
I've built a custom post type called 'product' and set up a taxonomy called'product_type'.
//Register product post type
add_action('init', 'product_register');
function product_register() {
$labels = array(
'name' => ('Products'),
'singular_name' => ('Product'),
'add_new' => ('Add New'),
'add_new_item' => ('Add New Product'),
'edit_item' => ('Edit Product'),
'new_item' => ('New Product'),
'view_item' => ('View Product'),
'search_items' => ('Search'),
'not_found' => ('Nothing found'),
'not_found_in_trash' => ('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'menu_icon' => 'dashicons-tag',
'public' => true,
'has_archive' => true,
'supports' => array('title', 'revisions', 'editor','thumbnail'),
'capability_type' => 'post',
'rewrite' => array("slug" => "product"), // Permalinks format
);
register_taxonomy('product_type', array('product'), array(
'hierarchical' => true,
'label' => 'Product Type',
'singular_label' => 'Type',
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => true)
);
register_post_type( 'product' , $args );
}
In the wordpress back end, I've loaded this with a few terms and have also added in descriptions.
archive-products.php works like a landing page, but will accept information from a variable called $type to filter the content.
if (isset($_GET['type']) || !empty($_GET['type']) ) {
$filtered = true;
$type = $_GET['type'];
}
With that information, I can set up a WP_Query for items that contain that taxonomy term.
if ($filtered == true) {
$type = explode(' ', $_GET['type']);
$taxonomy = array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => $type
)
);
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order' => 'DEC',
'orderby' => 'title',
'offset' => '0',
'tax_query' => $taxonomy
);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
$terms = get_the_terms( $post->ID, 'product_type' );
if($terms) {
foreach( $terms as $term ) {
echo $term->description; //always shows first term description, but should display description based on $type
}
}
while($the_query->have_posts()):$the_query->the_post();
the_title();
the_content();
endwhile;
else: //nothing to show here
endif;} else {
// non-filtered page design
}
The query works great but, as I mentioned earlier, it's only pulling the first taxonomy description.
This is the part that is meant to handle that:
$terms = get_the_terms( $post->ID, 'product_type' );
if($terms) {
foreach( $terms as $term ) {
echo $term->description;
}
}
I had guessed that it needed to be inside the query to land on the correct description, but it doesn't seem to make a difference.
I have also tried this:
$term = get_term_by( $post->ID, $type, 'product_type' );
echo $term->description;
but that doesn't return anything.
I'm out of ideas. I haven't found anything via google or by searching this site.
Any advice is appreciated, thanks in advance.
Related
So I'm working on a custom post type to work together with Advanced custom fields. And I have this little script that is supposed to block duplicate emails from being entered. But the script does not work as I want it to.
The code that u see below is for the custom post type page that I've linked to ACF.
function init_members() {
$labels = array(
'name' => 'Members',
'singular_name' => 'Member',
'menu_name' => 'Members',
'name_admin_bar' => 'Member',
'add_new' => 'New member',
'add_new_item' => 'New member',
'new_item' => 'New member',
'edit_item' => 'Edit member',
'all_items' => 'All members',
'search_items' => 'Search member',
'not_found' => 'No members found',
'not_found_in_trash' => 'No members found in trash'
);
$args = array(
'labels' => $labels,
'public' => true,
'exclude_from_search' => true,
'rewrite' => array('slug' => 'member'),
'has_archive' => false,
'supports' => array('title'),
'show_in_rest' => true,
'menu_icon' => 'dashicons-groups'
);
register_post_type('members', $args);
}
add_action('init', 'init_members');
function add_member_columns ( $columns ) {
unset($columns['date']);
return array_merge ( $columns, array (
'contactperson' => __ ( 'Contactperson' ),
'phone_number' => __ ( 'Phonenumber' ),
'email' => __ ( 'Email' ),
) );
}
add_filter ('manage_members_posts_columns', 'add_member_columns' );
function fill_member_columns ( $column, $post_id ) {
switch ( $column ) {
case 'contactperson':
echo get_post_meta ( $post_id, 'contactperson', true );
break;
case 'phone_number':
echo get_post_meta ( $post_id, 'phone_number', true );
break;
case 'email':
echo get_post_meta ( $post_id, 'email', true );
break;
}
}
add_action ('manage_members_posts_custom_column', 'fill_member_columns', 10, 2 );
So the script I have for trying to filter the use of duplicate emails is as follows.
the source where I got it from is here
add_filter('acf/validate_value/name='.'email', 'acf_unique_value_field', 10, 4);
function acf_unique_value_field($valid, $value, $field, $input) {
if (!$valid || (!isset($_POST['post_ID']) && !isset($_POST['post_id']))) {
return $valid;
}
if (isset($_POST['post_ID'])) {
$post_id = intval($_POST['post_ID']);
} else {
$post_id = intval($_POST['post_id']);
}
if (!$post_id) {
return $valid;
}
$post_type = get_post_type($post_id);
$field_name = $field['name'];
$args = array(
'post_type' => $post_type,
'post_status' => 'publish, draft, trash',
'post__not_in' => array($post_id),
'meta_query' => array(
array(
'key' => 'email',
'value' => $value
)
)
);
$query = new WP_Query($args);
if (count($query->posts)){
return 'This Value is not Unique. Please enter a unique '.$field['label'];
}
return true;
}
I probably oversee something pretty obvious. But I'm not able to find what I'm overseeing.
I think the problem might be that you aren't querying all existing posts. If your posts per page default is configured to 5, and the 6th post turns out to have the same mail address as the updated one, your check will not find any matching posts.
Try the following: in acf_unique_value_field(), fix your query like so:
$args = array(
'post_type' => $post_type,
'post_status' => 'publish, draft, trash',
'post__not_in' => array($post_id),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'email',
'value' => $value
)
)
);
$query = new WP_Query($args);
'posts_per_page' => -1, will query an unlimited number of posts.
Also, I've seen you're using get_post_meta to retrieve ACF field values. I advise against doing so. Use ACF's get_field instead (and update_field for updating if necessary).
i have created custom post type for testimonials in wordpress.i have added 89 testimonials and wants to display 2 which i wants to displany in home page .
so wanted to created shortcode which will display testimonials according to their Post ID.
can anyone please tell me the code for shortcode.
Below i am showing code i had written to create custom post type for testimonial . Pls tell me the code to create shortcode like this:-[testimonial posts_per_page="5" testimonial_id="123,145"]
function custom_post_testimonial_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name'=> _x( 'Testimonials', 'Post Type General Name', 'walker_theme' ),
'singular_name'=> _x( 'Testimonial', 'Post Type Singular Name', 'walker_theme' ),
'menu_name'=> __( 'Testimonials', 'walker_theme' ),
'parent_item_colon' => __( 'Testimonial', 'walker_theme' ),
'all_items' => __( 'All Testimonials', 'walker_theme' ),
'view_item' => __( 'View Testimonial', 'walker_theme' ),
'add_new_item' => __( 'Add New Testimonial','walker_theme' ),
'add_new' => __( 'Add New', 'walker_theme' ),
'edit_item' => __( 'Edit Testimonial','walker_theme' ),
'update_item' => __( 'Update Testimonial','walker_theme' ),
'search_items' => __( 'Search Testimonial', 'walker_theme' ),
'not_found' => __( 'Not Found', 'walker_theme' ),
'not_found_in_trash' => __( 'Not found in Trash','walker_theme' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'testimonials', 'walker_theme' ),
'description' => __( 'Home page testimonials', 'walker_theme' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'author','thumbnail', 'tags'),
// You can associate this CPT with a taxonomy or custom
taxonomy.
'taxonomies' => array( 'genres', 'post_tag' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
// Registering your Custom Post Type
register_post_type( 'testimonials', $args );
}
add_action( 'init', 'custom_post_testimonial_type', 0 );
Use the shortcode attributes with known attributes and fills in defaults when needed.
function testimonials($atts) {
$a = shortcode_atts( array(
'posts_per_page' => '',
'testimonial_id' => ''
), $atts );
$testimonials = '';
$post_in = esc_attr($a['testimonial_id']);
$posts_per_page = esc_attr($a['posts_per_page']);
$post_artay = explode(',', $post_in);
$args = array(
'post__in' => $post_artay,
'posts_per_page' => $posts_per_page,
'post_type' => 'testimonials',
'order_by' => 'post__in',
);
// the query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$testimonials.= '<div class="title">'.get_the_title().'</div>';
$testimonials.= '<div class="content">'.get_the_content().'</div>';
$testimonials.='<div class="date">'.get_the_date().'</div>';
$testimonials.='<div class="author">'.get_the_author().'</div>';
endwhile;
endif;
return $testimonials;
}
add_shortcode('testimonial', 'testimonials' );
use the shortcode like.
[testimonial posts_per_page="5" testimonial_id="29,23"]
Hi let's add this code in the theme's functions.php file.
add_shortcode( 'testimonial', 'testimonial_shortcode_callback' );
function testimonial_shortcode_callback( $atts ) {
ob_start();
extract( shortcode_atts( array(
'posts_per_page' => 5,
'testimonial_id' => '',
), $atts ) );
// define query parameters based on attributes
$options = array(
'post_type' => 'testimonials',
'posts_per_page' => $posts_per_page,
);
if ( ! empty( $testimonial_id ) ) {
$options['post__in'] = array_map( 'trim', explode( ',', $testimonial_id ) );
}
$testimonial_query = new WP_Query( $options );
// run the loop based on the query
if ( $testimonial_query->have_posts() ) :
?>
<ul class="testimonial-listing">
<?php
while ( $testimonial_query->have_posts() ) : $testimonial_query->the_post();
?>
<li id="testimonial-<?php the_ID(); ?>">
<?php the_content(); ?>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
<?php
$testimonial_output = ob_get_clean();
return $testimonial_output;
endif;
}
Than use this shortcode as a example [testimonial posts_per_page="2" testimonial_id="123,145"]
I am trying to get print names of custom taxonomy that i have created for products.
function create_product_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x('product_categories', 'taxonomy general name'),
'singular_name' => _x('Product', 'taxonomy singular name'),
'search_items' => __('Search Product Category'),
'all_items' => __('All Product Categorie(s)'),
'parent_item' => __('Parent Product Category'),
'parent_item_colon' => __('Parent Product Category:'),
'edit_item' => __('Edit Product Category'),
'update_item' => __('Update Product Category'),
'add_new_item' => __('Add New'),
'new_item_name' => __('New Product Name'),
'menu_name' => __('Product Categories'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'product_categories', 'with_front' => true));
register_taxonomy('product_categories', array('products'), $args);
i have added data through the wordpress admin panel. Now i want to Display the names of the categories in product.php file.
function getLatestProducts()
{
$args = array(
'post_status' => 'publish',
'post_type' => 'products',
'posts_per_page' => 12,
'order' => 'ASC'
);
$result = '<div class="col-sm-3">';
$loop = new WP_Query($args);
$i=0;
while ($loop->have_posts())
{
$loop->the_post();
$clink=get_permalink($post->ID);
$desc=get_the_excerpt();
$categories = get_terms( 'product_categories');
$desc = strip_tags(str_replace(array("<p>", "</p>"), "", $desc));
$the_imgurl = get_post_custom_values('_cus_n__image');
$theimage=$the_imgurl[0];
$the_locurl = get_post_custom_values('_cus_n__location');
$theloc=$the_locurl[0];
echo $categories;
$result .='<div class="product-warp">';
$result .='<div class="product"> <img src="/wp-content/themes/cake/images/pro1.jpg" title="" alt=""> </div>';
$result .='<div class="product-name">';
$result .='<h5>'.$categories.'</h5>';
$result .='</div>';
$result .='</div>';
$i++;
}
$result .= '</div>';
if($i > 0){
return $result;
} else {
return "";
}
}
it is just printing this arrayarrayarrayarrayarrayarray
Ok bro you can use get_terms function for this purpose. Here is the example:
First Part
<?php
$args = array(
'orderby' => 'name'
);
$terms = get_terms('product_categories', $args);
foreach($terms as $term) {
?>
<a href="<?php echo get_term_link($term->slug, 'product_categories') ?>">
<?php echo $term->name; ?>
</a>
<?php
}
?>
I only give you an example. You can paste my code where you want.
Second Part
Now use the WordPress Taxonomy Template for that when user click on one of your category and next page show all the related products of clicked category and also you must read this.
If you read taxonomy Template link then we go to next step.
Now you create a file taxonomy-product_categories.php in your theme root folder.
This create template for you taxonomy.
Now in this file here is the complete code:
<?php
get_header();
$slug = get_queried_object()->slug; // get clicked category slug
$name = get_queried_object()->name; // get clicked category name
$tax_post_args = array(
'post_type' => 'products', // your post type
'posts_per_page' => 999,
'orderby' => 'id',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_categories', // your taxonomy
'field' => 'slug',
'terms' => $slug
)
)
);
$tax_post_qry = new WP_Query($tax_post_args);
if($tax_post_qry->have_posts()) :
while($tax_post_qry->have_posts()) :
$tax_post_qry->the_post();
the_title();
the_content();
endwhile;
endif;
get_footer();
?>
Once again I told you that I give you only a code you can merge this code in your theme.
Hope this'll help you.
I have a custom post type and custom taxonomy set up using the code below in functions.php.
The URL writes correctly when clicking through on the archive pages on the front end and when clicking "View Post" in the admin edit screen. But when the post is returned in site search results, the custom taxonomy is literally missing. The URL on search results is http://www.example.com/foo//postname.
Any idea why the custom taxonomy in the URL would work in some situations but not in others?
add_action( 'init', 'create_foo_posttype' );
function create_foo_posttype() {
$tax_labels = array(
'name' => _x( 'Foo Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Foo Category', 'taxonomy singular name' ),
'all_items' => __( 'All Foo Categories' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Foo Category' ),
'update_item' => __( 'Update Foo Category' ),
'add_new_item' => __( 'Add New Foo Category' ),
'new_item_name' => __( 'New Foo Category Name' ),
);
register_taxonomy('foo_categories','foo',array(
'labels' => $tax_labels,
'hierarchical' => true,
'has_archive' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'foo' )
));
register_post_type( 'foo',
array(
'labels' => array(
'name' => __( 'Foo' ),
'singular_name' => __( 'Foo' )
),
'public' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 100,
'capability_type' => 'post',
'supports' => array( 'title', 'author', 'thumbnail', 'trackbacks', 'revisions' ),
'taxonomies' => array( 'foo_categories' ),
'has_archive' => true,
'rewrite' => array( 'slug' => 'foo/%foo_categories%')
)
);
flush_rewrite_rules();
}
add_action( 'init', 'cust_rewrite_init' );
function cust_rewrite_init() {
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}
add_filter( 'page_rewrite_rules', 'cust_rewrite_collect_page_rewrite_rules' );
function cust_rewrite_collect_page_rewrite_rules( $page_rewrite_rules )
{
$GLOBALS['cust_rewrite_page_rewrite_rules'] = $page_rewrite_rules;
return array();
}
add_filter( 'rewrite_rules_array', 'cust_rewrite_prepend_page_rewrite_rules' );
function cust_rewrite_prepend_page_rewrite_rules( $rewrite_rules )
{
return $GLOBALS['cust_rewrite_page_rewrite_rules'] + $rewrite_rules;
}
I found the problem. It wasn't in the code I posted originally. It was here:
add_filter('post_type_link', 'cust_permalink_structure', 1, 4);
function cust_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%foo_categories%' ) ) {
$post_type_term = get_the_terms( $post->ID, 'foo_categories' );
$post_link = str_replace( '%foo_categories%', $post_type_term[0]->slug, $post_link );
}
return $post_link;
}
Using $post_type_term[0]->slug; worked if a post had more than one tax value, but failed when the post had only one (which all of my posts do). To fix it, I used array_pop instead of the other array call and it works fine. The resolved code:
add_filter('post_type_link', 'cust_permalink_structure', 1, 4);
function cust_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%foo_categories%' ) ) {
$post_type_term = get_the_terms( $post->ID, 'foo_categories' );
$one_term = array_pop($post_type_term);
$post_link = str_replace( '%foo_categories%', $one_term->slug, $post_link );
}
return $post_link;
}
In file functions.php I created a custom category
$labels = array(
'name' => _x('Partners', 'post type general name'),
'singular_name' => _x('Partners', 'post type singular name'),
'add_new' => _x('Add New', 'partners'),
'add_new_item' => __("Add New Partners"),
'edit_item' => __("Edit Partners"),
'new_item' => __("New Partners"),
'view_item' => __("View Partners"),
'search_items' => __("Search Partners"),
'not_found' => __('No partners found'),
'not_found_in_trash' => __('No partners found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'menu_icon' => 'dashicons-groups',
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','thumbnail','excerpt', 'editor')
);
register_post_type('partners',$args);
In the Partner's template I wrote:
<?php
$args = array(
'posts_per_page' => -1, // Get all posts
'post_type' => 'partners', // Query for the default Post type
'order_by' => 'post_date' // Order by date posted
);
$last_five_posts = get_posts( $args );
foreach ( $last_five_posts as $post ) : setup_postdata( $post );
?>
And it works fine. The page show me all posts I edited in the custom category.
The problem came out with translation. I use WMPL plugin, I use two languages: italian and english. The Partners'page show me both languages:
Italian page:
post ORIGINAL1
post TRANSLATED1
post ORIGINAL2
post TRANSLATED2
...
English page:
post ORIGINAL1
post TRANSLATED1
post ORIGINAL2
post TRANSLATED2
...
I think the problem is on the code, I'm missing something.
Do you know how to fix it?
The Support answered me. In the Partner's template instead of using the get_posts () function, I had to use the WP_Query class:
<?php
$args = array(
'posts_per_page' => -1, // Get all posts
'post_type' => 'partners', // Query for the default Post type
'order_by' => 'post_date' // Order by date posted
);
$last_five_posts = new WP_Query( $args );
if( $last_five_posts->have_posts() ): while( $last_five_posts->have_posts() ) : $last_five_posts->the_post();
?>