I have this bit of code:
<div class="wrap sc-cat-container">
<?php
$customPostTaxonomies = get_object_taxonomies('short_courses');
if(count($customPostTaxonomies) > 0)
{
foreach($customPostTaxonomies as $tax)
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'title_li' => '',
'hide_empty' => FALSE
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '
<div class="one-half sc-cat-items">
<h2>
' . $category->name . '
</h2>
</div>';
}
}
}
?>
</div>
This displays a list of categories for my custom post type 'Short Courses' Here's how it looks currently: http://staging.seedcreativeacademy.co.uk/short-courses/
This is great and works fine but I would also like to add images for my categories, I it to look like this:
I have Advanced Custom Fields, where I have created an image field so I can now assign an image to each category. So far so good!
The main problem comes when I try and display the image inside the code above...
Here's the code that would display the image in any normal circumstance:
<img src="<?php the_field('course_type_image'); ?>">
But I guess I need to add it to the above code... so I came up with this but it isn't working as I don't t think I can put a php tag inside another PHP tag!?
<div class="wrap sc-cat-container">
<?php
$customPostTaxonomies = get_object_taxonomies('short_courses');
if(count($customPostTaxonomies) > 0)
{
foreach($customPostTaxonomies as $tax)
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'title_li' => '',
'hide_empty' => FALSE
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '
<div class="one-half sc-cat-items">
<img src="<?php the_field('course_type_image'); ?>">
<h2>
' . $category->name . '
</h2>
</div>';
}
}
}
?>
</div>
So I'm a bit confused...
You are attempting to echo a string, but within your string, you are calling a function that itself echos rather then returns, a string:
echo '
<div class="one-half sc-cat-items">
<img src="<?php the_field('course_type_image'); ?>">
<h2>
<a href="' . get_category_link( $category->term_id )
. '">' . $category->name . '</a>
</h2>
</div>';
Therefore, when trying to call the function the_field() within your string to be echo'd, it is attempting to echo again.
You need to return the value of the field instead, with the get_field() function.
Also, remove the php tags in the img source line, since you are already within an opening <?php tag:
'<img src="' . get_field('course_type_image') . '">'
Related
Still trying to figure it out how could I filter the custom taxonomy and display it. the JS is for firing the URL generated is working fine. the problem is the results page items are not showing. I have this code so far:
<?php
$args = array(
'orderby' => 'slug',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => false
);
$terms = get_terms([
'taxonomy' => 'pdf_cat',
'hide_empty' => false,
]);
foreach( $terms as $term ){
echo '<option class="ctg" value="'. get_term_link($term->term_id) .' ">' . $term->name . '</option>';
}
//NEXT CODE IS GET THE CATEGORY ID AFTER CLICKED TO DISPLAY
<?php
$terms = get_terms([
'taxonomy' => 'pdf_cat',
'hide_empty' => false,
]); // get the category from query
$terms = $term[0];
$term_name = get_term_name($term->name); // get the category name
$term_id = get_term_ID($term->term_id); // get the catehory ID
?>
<?php
$paged = get_query_var('paged', 1);
$args = array( // asigned the functions inside the array
'paged' => $paged,
'post_type' => 'pdf',
'taxonomy' => $term_id,
);
$query = new WP_Query($args); // get the functions inside the assigned name
global $query_string; //Post the specify numbers per page
query_posts( $query_string . "&posts_per_page=12&paged=".$paged );
while ( have_posts() ) : the_post()
?>
and here is the js to fire the dropdown menu link when clicked:
<script type="text/javascript">
$("#form").change(function(){
var url = $(this).val();
location.assign(url);
});
</script>
//NEXT CODE IS TO THE DISPLAY
<div class="pdf-box">
<?php
$file = get_field('file');
if( $file ): ?>
<a href="<?php echo $file['url']; ?>" target="_blank"><?php echo $file['file']; ?>
<div class="service-list-col1">
<i class="fa-file-o"></i>
</div>
<div class="service-list-col2">
<p><?php the_time('Y.m.d'); ?></p>
<h3><?php the_title(); ?></h3>
</div>
</a>
<?php endif; ?>
</div>
I am trying to place a link to the Artist of a video in a custom post type in WordPress. Everything is working but the term_id does not iterate.
<?php
$args = array(
'post_type' => 'video',
'posts_per_page' => 4,
'taxonomy' => $term->name,
'number' => $term->term_id,
);
$q = new WP_Query( $args);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
//Your template tags and markup like:
?>
<div class="card">
<div class="bg-img"><img alt="Norway" style="width:100%" src="<?php the_post_thumbnail( 'thumbnail' );?></div>
<div class="content">
<h4 style="color: green;"><?php the_title( ); ?></h4>
<h5 class="artist-name"><?php
$terms = get_terms( $args );
foreach( $terms as $term ){
echo '<div style="color: black;">' . esc_html( $term->name ) . " ". $term->term_id . '</div>';
} ?>
As per your code, it seems that you are using the posts arguments for "get_terms" which is not correct and that's why you are not getting the correct term's data.
You can check the query parameter of terms over here to get the idea.
I have created some custom categories for products and it has sub categories and the sub categories has further sub categories. now first i display the main categories. If i display there sub then all the sub categories related to that category and its sub category are displayed.I want to show them step by step.That is if the user click on main category then it goes to its sub category page. If user click one of its sub category then it should go to sub categories and if has no sub category then displayed the products. Code is this
$products = get_term_children($term_id[0], 'product-cat');
if(count($products) > 0){
$count = 0;
$sorted_products = array();
foreach ($products as $product) {
$sorted_products = get_term($product, 'product-cat');
$prod_meta = get_option("taxonomy_term_".$term->term_id);
//echo "<pre>"; print_r($sorted_products);
foreach ($sorted_products as $product) { ?>
<div class="col-md-3 col-sm-4 col-xs-12">
<a href="<?php echo $product['link']; ?>">
<a href="<?php echo $product['link']; ?>" class="hvr-grow">
<img class="center-block img-responsive" src="<?php echo $product['img'] ? $product['img'] : '/wp-content/themes/ruskin/images/dummy-product.jpg'; ?>" alt="<?php echo $product['name']; ?>">
<h3><?php echo $product['name']; ?></h3>
else{
# Define the WP Query post arguments.
$args = array(
'post_status' => 'publish',
'post_type' => 'products',
'posts_per_page' => -1,
//'meta_query' => array('relation' => 'AND', array('key' => '_cus__featured', 'value' => '1', 'compare' => '='),),
'meta_key' => '_cus__sort_order',
//'meta_value' => 'meta_value',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'tax_query' => array(
array('taxonomy' => 'product-cat',
'field' => 'slug',
'terms' => $cats
)));
$loop = new WP_Query($args);
$total = $loop->found_posts;
$sliders='';
// Generatet the slider conteents
while ($loop->have_posts()) {
$loop->the_post();
$listingimg = get_post_custom_values('_cus__listing_img');
$listingimg = "/wp-content/themes/bodyo/images/no-slider-img.jpg";
$img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'main_slide_img');
$img = "/wp-content/themes/bodyo/images/no-slider-img.jpg";
$sliders .= '<a href="'. get_the_permalink() .'" class="hvr-grow">';
$sliders .= '<img src="'.$listingimg.'" class="center-block img-responsive" alt="'. get_the_title() .'" />';
$sliders .= '</a>';
$sliders .= '</div>';
$sliders .= '<a href="'. get_the_permalink() .'">';
$sliders .= '<h3>'. get_the_title() .'</h3>';
$sliders .= '<p>'. get_the_excerpt() .'</p>';
$sliders .= 'read more';
$counter++;
}
It over-rites the previous sort order. That is if from dashboard we give 2 to three categories in sort order then it displays just the last one. First two are overwritten.
Something like this should do it.
$args = array(
'child_of' => $term_id[0],
'taxonomy' => 'product-cat',
'hierarchical' => true,
'depth' => 1,
);
$categories = get_categories($args);
Keep in mind that get_categories() is a wrapper of get_terms().
You can find all the accepted values here but the one you're looking for is depth.
This question seems to have been asked in some varieties, But mine is quite specific.
I need to display the most recent post of all categories of a custom post type called "products". I need to display the category instead of the title and link to that cat's archive.
The problem is, What I am getting instead is only one post of that general post type. Any thoughts?
Here is my starting point:
<?php
$query = new WP_Query( array(
'posts_per_page' => 12,
'post_type' => 'products',
'post_status'=>'publish',
'orderby'=>'post_date',
'order' => 'DESC',
'cat' => $category->term_id,
'count'=>1,
));
if ( have_posts() ) : while ( $query->have_posts()) : $query->the_post(); ?>
<div class="col-sm-6 col-md-4">
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail('product', array('class' => 'img-responsive center-block')); ?><h3><?php the_title(); ?></h3>
<?php else : ?>
<!--<img class="img-responsive center-block" src="<?php echo THEME_DIR; ?>/img/default-thumb.png" alt="<?php the_title(); ?>" />--><img class="img-responsive " data-src="holder.js/100%174/auto" alt="Generic placeholder image"><h3><?php the_title(); ?></h3>
<?php endif; ?>
</div><!-- col-sm-6 -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div><!-- end row -->
<ul class="pager">
<li class="previous"><?php previous_posts_link(); ?></li>
<li class="next"><?php next_posts_link(); ?></li>
</ul>
<?php else : ?>
<h1>Not Found</h1>
<p>Click Here to return to the Home Page</p>
<?php endif; ?>
The custom post type was created via a 3rd plug into Super CPT Plugin
Here is the code:
add_action( 'after_setup_theme', 'setup_data_structures' );
function setup_data_structures() {
if ( ! class_exists( 'Super_Custom_Post_Type' ) )
return;
$products = new Super_Custom_Post_Type( 'products', 'Product', 'Products' );
# Test Icon. Should be a square grid.
$products->set_icon( 'suitcase' );
# Taxonomy test, should be like categories
$product_category = new Super_Custom_Taxonomy( 'product-category', 'Product Category', 'Product Categories', 'category' );
# Connect both of the above taxonomies with the post type
connect_types_and_taxes( $products, array($product_category) );
# Add a meta box with every field type
$products->add_meta_box( array(
'id' => 'product-fields',
'context' => 'normal',
'fields' => array(
'client-name' => array('column' => true ),
'product-description' => array( 'type' => 'wysiwyg' )
)
) );
$products->add_to_columns( 'product-category' );
This should help you out. The follow code displays 5 of each category. Just switch the 5 to how many ever you wish to display from each category.
<?php
$cat_args = array(
'orderby' => 'name',
'post_type' => 'products',
'order' => 'ASC',
'child_of' => 0
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<dl>';
echo '<dt> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></dt>';
$post_args = array(
'numberposts' => 5,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<dd><?php the_title(); ?></dd>
<?php
}
echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
echo '</dl>';
}
?>
<?php
$cats = array('3d-mail', 'boxes', 'folders', 'binders');
$exclude_posts = array();
foreach( $cats as $cat )
{
// build query argument
$query_args = array(
'category_name' => $cat,
'showposts' => 1,
'post_type' => 'products',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
);
// exclude post that already have been fetched
// this would be useful if multiple category is assigned for same post
if( !empty($exclude_posts) )
$query_args['post__not_in'] = $exclude_posts;
// do query
$query = new WP_Query( $query_args );
// check if query have any post
if ( $query->have_posts() ) {
// start loop
while ( $query->have_posts() ) {
// set post global
$query->the_post();
// add current post id to exclusion array
$exclude_posts[] = get_the_ID();
<?php the_content(); ?>
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
?>
I wanted create a widget for the sidebar of the theme that i'm working on recently...But I can't find a way to get links of categories.
This is the code of my widget:
<section class="sidebar-categories">
<div class="inner">
<h3><label>categories</label></h3>
<ul>
<?php
$args = array(
'taxonomy' => 'category',
'parent' => 0, // get top level categories
'orderby' => 'name',
'order' => 'ASC',
'number' => 2,
'hierarchical' => 1,
'pad_counts' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ){
echo '<li>'. $category->name . '<span>'. $category->count .'</span></li>';
}
?>
</ul>
</div><!-- /inner -->
</section><!-- /sidebar-categories -->
Everything is fine...Markup is exacly what I want...But I don't know what to put in <a href=""> to get the links of categories...
ANY HELP WOULD BE APPRECIATED...
Use
echo get_category_link( $category->term_id );
To get the link of the given category term.
Documentation for the function is here: https://codex.wordpress.org/Function_Reference/get_category_link
Try this,
foreach ( $categories as $category ){
$category_link = get_category_link( $category->cat_ID );
echo '<li>'. $category->name . '<span>'. $category->count .'</span></li>';
}
Change your line
echo '<li>'. $category->name . '<span>'. $category->count .'</span></li>';
..into
$category_id = get_cat_ID( $category->name );
echo '<li>'. $category->name . '<span>'. $category->count .'</span></li>';