I'm facing an issue with my website.
I would like to display all my custom post type named "Projets", and for each item, I want to get several term names to put in my data element.
Displaying all my post is not an issue, it's working well. I manage to display one term name using "get_terms()", but I don't know how to display several terms and put them in the right place.
I have 3 different custom taxonomy : city, typo and statut.
There is my code :
<?php
$args = array(
'post_type' => 'projets',
'posts_per_page'=>'99',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li class="content_item active" data-city="CITY_NAME_HERE" data-typo="TYPO_NAME_HERE" data-statut="STATUT_NAME_HERE">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('custom-size', ['class' => 'content_item_img', 'title' => 'Image du projet']); ?>
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile;
wp_reset_postdata();
?>
Thanks for the help!
Try this:
Inside the loop, you get the taxonomy like this (eq. "city"):
$cities = get_terms( array(
'taxonomy' => 'city',
'hide_empty' => false,
) );
You get an array of all the terms of the Taxo.
Where you need them, you have to loop them, here to give you an idea.
if(!empty($cities)){
foreach($cities as $city){
echo $city->name.', ';
}
}else{
echo 'No Cities.';
}
Comma Separated custom categories inside the loop.
<?php echo get_the_term_list( $post->ID, 'cutom_category', '', ', ', '' ) ?>
Related
If anyone out there is familiar with Event Organiser templates. I could use some help. I am trying to limit it to 5 per page and add custom pagination. I can't seem to edit the loop in eo-loop-events.php file to add a custom query to limit it to 5 per page.
I've tried the shortcode and looked at their documentation all afternoon and am getting nowhere. I figure there is probably an easy way to do this and I am missing it or I can't do this with the free version.
Any help would be appreciated.
I just had to implement pagination for my wp events organiser template. This is inside widget-event-list.php but will probably work in other templates. This is copy pasted from a working example within the template.
This utilizes the WP_Query loop instead of the $eo_event_loop() that the template uses by default. The WP_Query loop has arguments to get "event" data only.
Fortunately, the event organiser functions (like eo_get_event_datetime_format()) still work.
In this example, I am only outputting the title in the <h4> tag.
This solution was inspired by this answer to another question.
<h1>Paginated Query Area</h1>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'event', // Your post type name
'posts_per_page' => 3,
'paged' => $paged,
);
$loop = new WP_Query( $args );
?>
<?php if ( $loop->have_posts() ): ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
//Generate HTML classes for this event
$eo_event_classes = eo_get_event_classes();
//For non-all-day events, include time format
$format = eo_get_event_datetime_format();
?>
<h4 class="event-title"><?php the_title(); ?></h4>
<?php endwhile; ?>
<div class="upcoming-pagination">
<?php // Pagination display
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
</div>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
What exactly i want to know is how do i get my categories like in the picture. (The food types thumbnails.) I have a WordPress site with the Maya Shop theme which is based on Woo Commerce. I tried every which way i could from the menu and i hadn't managed to do it.
Also tried to fiddle a little bit with the Short code to no avail. I am new to this an i want to keep it as simple as possible. Do i have to write php code for some files or can i do it simpler than that?
That's not good answer. get_category_link() is not appropriate function to use for custom taxonomy. Function get_term_link() is what we need here.
<?php
$prod_categories = get_terms( 'product_cat', array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1
));
foreach( $prod_categories as $prod_cat ) :
$cat_thumb_id = get_woocommerce_term_meta( $prod_cat->term_id, 'thumbnail_id', true );
$cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
$term_link = get_term_link( $prod_cat, 'product_cat' );
?>
<img src="<?php echo $cat_thumb_url; ?>" alt="<?php echo $prod_cat->name; ?>" />
<?php endforeach; wp_reset_query(); ?>
I assuming that your theme doesn't have the code in place already to show the categories and their thumbnails on the homepage? If that is the case you will need to determine what template is being used and then most likely use some variation of the following code to build the display. Note: you will need to style and build any additional components to match your display exactly.
<ul>
<?php
$prod_categories = get_terms( 'product_cat', array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1
));
foreach( $prod_categories as $prod_cat ) :
$cat_thumb_id = get_woocommerce_term_meta( $prod_cat->term_id, 'thumbnail_id', true );
$cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
?>
<li><img src="<?php echo $cat_thumb_url; ?>" alt="<?php echo $prod_cat->name; ?>" /></li>
<?php endforeach; wp_reset_query(); ?></ul>
I am adding post thumbnail jquery slider to header part but it's resulting weird issue. It is somehow not ending while or stop query and so if I will go to single post or page it is keep displaying loop instead of page or post content.
I have tried two different query but none of them stopping to this weird issue.
First Tried
<?php
query_posts( 'post_status=publish&orderby=rand' );
while (have_posts()) : the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; ?>
Than Second Tried
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('post_status=publish&orderby=rand');
// The Loop
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; endif; wp_reset_query();?>
None of these stopping to display loop ( all post like index page) in to single post or page.
I will go to single post or page it is keep displaying loop instead of page or post content...
That is because you are not passing it any parameters that would limit the query for single posts. Your query ( $wp_query->query('post_status=publish&orderby=rand'); ) pulls all posts, all the time, and in random order. For single post display you need to pass it a post or page parameter. You probably need to use get_query_var() to check for 'p', 'page_id', or both. Something like this:
$pid = get_query_var('p');
if (!empty($pid)) {
$qry = 'p='.$pid;
} else {
$qry = 'post_status=publish&orderby=rand';
}
$wp_query->query($qry);
There are other possible solutions as well, like is_single().
Also, WordPress uses the variable $wp_query so you should really pick another one instead of clobbering that one.
I found the solution :)
I have added if (have_posts()) before while and end loop with wp_reset_query() and all good now. :)
So here is final code if anyone having same problem..
<?php
query_posts( 'post_status=publish&orderby=rand' );
if ( have_posts()): while (have_posts()) : the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; endif; wp_reset_query(); ?>
I am using a custom post type and custom categories. I need to fetch the category ID of a custom post type in a page. I tried using various methods but I am unable to get the category ID.
<?php
$args = array(
'posts_per_page'=> -1,
'post_type' => 'professional',
'orderby'=> 'id',
'order'=> 'asc',
);
query_posts($args);
$my_posts = new WP_Query($args);
while ($my_posts->have_posts()) : $my_posts->the_post(); // loop for posts
function getCurrentCatID(){
global $wp_query;
global $post;
$cats = get_queried_object()->term_id;
$cat_ID = $cats[0]->cat_ID;
return $cat_ID;
}
?>
<li>
<div class="caption">
<h3><?php the_title(); ?></h3>
<span class="industry"><?php getCurrentCatID($cat_ID); ?></span>
</div>
</li>
I am not able to get the cat ID in custom post types.
Try This.
$cat_l=get_the_term_list( $post->ID, 'custom_categories', '', ' , ', '' );
echo $cat_l;
I have add custom post type field name 'Movies'. Now I have done these things and its working great, but the problem is, (i.e When I'm click on any movie, its showing me only one movie post, (i.e I'm click on avatar movie its showing me avatar movie post, but when I'm click on stargate movie its showing me avatar movie post. Please help its a big issue) anyone who can help me to make this code exactly which I want.
in my functions.php I have add this code:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'Movies',
array(
'labels' => array(
'name' => __( 'movie' ),
'singular_name' => __( 'movie' )
),
'public' => true,
'has_archive' => true,
)
);
}
Then in my template file add this where I want to show post:
<?php
$args = array( 'post_type' => 'movies', 'posts_per_page' => 1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1>
<a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
I'm new for this thing, so please explain me as much as you can, where I paste it or what I do?
I can't see how you structured your links to those movie-posts, but you should give them a variable, so the page where the movie-post shows up knows what to show!
e.g $moviename
and in your template you modify the $args array to:
$args = array( 'post_type' => 'movies', 'posts_per_page' => 1, 'name' => $_GET['movie'] );
Should work, at least that explains why it always displays the same movie-post:
Your query hast no information what movie to display, at the moment it just takes the movie-posts table and displays the first one - because of the posts_per_page limit to 1.
Hope that makes sense...