I have a custom post type with two taxonomies, project-type and project-name. I am trying to display a list of project-names with links when the project-type is photography. The code I have so far is
<?php
if(is_taxonomy('photography'))
{
$taxonomy = 'project-name';
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term)
{
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
}
?>
</ul>
If I remove the if then it displays all the project-names but I need it to show only the ones that also have the project-type 'photography'.
Any help would be much appreciated.
Check this...
$term = get_term_by('name', 'photography', 'project-type');
if($term != false ){
$taxonomy = 'project-name';
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term)
{
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>
<?php
}
I hope this is what you wanted...
Related
I do split the $post_content using <!--nextpage--> as separator, but maybe there is another method to obtain the number of pages of a single post because I'm doing it many times on large posts.
The WP_Post class doesn't have a member like $post_pages or something.
Use this for a link to previous posts with directional:
<?php previous_post_link(); ?>
Or this for a link to previous posts that without "<<":
<?php previous_post_link('<strong>%link</strong>'); ?>
The code needs to be inside the loop.
Or
<div class="prev-posts pull-left">
<?php
$prev_post = get_previous_post();
if($prev_post) {
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
echo "\t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" class=" "><strong><<< "'. $prev_title . '"</strong></a>' . "\n";
}
?>
</div>
<div class="next-posts pull-right">
<?
$next_post = get_next_post();
if($next_post) {
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" class=" "><strong>"'. $next_title . '" >>></strong></a>' . "\n";
}
?>
</div>
I am trying to modify a portfolio template in a Wordpress theme. I'd like to apply a specific css class to a specific category of the portfolio, using its ID.
I've tried many things thanks to the different answers already brought here about conditional css (How to apply a CSS class depending on a value contained in PHP variable?), php variables, or about the use of the " foreach", but I wasn't able to find a solution :
May be I didn't put my condition at the right place, or may be is it because of the use of the foreach, or may is there a misplaced tag??
The result remains the same, a full blank page.
I hope you'll have few time to have a look on this and give me a little help.
Here is the original code extract :
<?php $terms_array = array(); $id = $cat = null; ?>
<?php foreach ( $p_terms as $term ) { ?>
<?php $terms_array[$term->term_id] = array( 'name' => $term->name ); ?>
<?php } ?>
<?php if ( count( $terms_array ) > 0 ) { ?>
<div class="row">
<div class="col-md-12">
<ul id="ef-portfolio-filter" class="list-inline text-center">
<?php if ( count( $terms_array ) > 1 ) { ?>
<li class="cbp-filter-item-active"><a href="#" data-filter="*"><?php _e( 'All', EF_TDM ) ?>
<span class="cbp-filter-counter"></span>
</a>
</li>
<?php } ?>
<?php $cur = count( $terms_array ) == 1 ? ' class="cbp-filter-item-active"' : ''; ?>
<?php foreach ( $terms_array as $id => $cat ) { ?>
<?php echo '<li' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter"></span></a></li>'; ?>
<?php } ?>
</ul><!-- #ef-portfolio-filter -->
</div><!-- .col-md-12 -->
</div><!-- .row -->
<?php } ?>
<?php } ?>
And here is one of my tries. I just paste an extract as I didn't change anything above it and just introduce my condition after the foreach :
<?php $cur = count( $terms_array ) == 1 ? ' class="cbp-filter-item-active"' : ''; ?>
<?php foreach ( $terms_array as $id => $cat ) { ?>
<?php
if ($id = myspecific id)
{echo '<li class="cbp-filter-item-custom"' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter-custom"></span></a></li>';
}else{
echo '<li ' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter"></span></a></li>' } ?>
</ul><!-- #ef-portfolio-filter -->
I'm developing a News website in Wordpress, and I need to show in a bootstrap carousel the first three posts, my problem is that I need to add the "active" class only at the first of the three elements, but really don't know how to. Here's my code:
<?php
$args = array('numberposts' => '3');
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
echo '<div class="item active"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
}
?>
I've already tried a answer found on this site (this one):
$isFirst = true;
foreach ($recent_posts as $recent) {
echo '<div class="item' . $isFirst ? ' active' : '' . '"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ": <strong>" .$recent["post_title"] . '</strong></a></div>';
$isFirst = false;
?>
but it just printed me the "active" words.
Thanks for your help
You need to set $i so that you can count how many times you have gone through the loop and do some logic with it, like in my example below. Instead of having two lines of code that are nearly identical like I have done below though, you should be able to do the if conditional right around the class active. I didn't do that so you could clearly see the conditional and the count of the loops through the array.
<?php
$args = array('numberposts' => '3');
$recent_posts = wp_get_recent_posts($args);
$i = 0;
foreach ($recent_posts as $recent) {
if ($i == 0) {
echo '<div class="item active"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
} else {
echo '<div class="item"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
}
$i++;
}
?>
I have been trying to create a page on wordpress that displays all categories with images, title and links of all categories of a particular post_type.
I have added the following code to my functions.php file:
function show_categories($excl=''){
$categories = get_the_category($post->ID);
if(!empty($categories)){
$exclude=$excl;
$exclude = explode(",");
foreach ($categories as $cat) {
if(!in_array($cat->cat_ID)) {
echo '<div class="product-category">';
// echo '<p>' . $cat->category_description . '</p>';
echo '<a href="' . get_category_link( $cat->term_id ) . '" />';
echo '<img src="';
echo z_taxonomy_image_url($cat->term_id, 'products') . '" />';
echo '</a>';
echo '<h2><a href="' . get_category_link( $cat->term_id ) . '" class="cat-link"';
echo '/>' . $cat->cat_name . '</a></h2>';
echo '<a href="' . get_category_link( $cat->term_id ) . '" class="more-info" >Info</a>';
echo '</div>';
}
}
}
}
Now the problem is: It only displays the categories of the latest post. If the latest post is included to all categories, all categories will show up, if not it will only show the categories relevant to the latest post.
I call this function on the file archive-products.php like this:
<?php show_categories(); ?>
Any ideas?
This is the page: http://giannacamilotti.com/products/
I found a solution, the code below worked!
function show_categories($excl=''){
$args=array(
'post_type' => 'Products'
);
$categories = get_categories($args);
if(!empty($categories)){
$exclude=$excl;
$exclude = explode(",");
foreach ($categories as $cat) {
echo '<div class="product-category ' . $cat->cat_name . ' ">';
echo '<a href="' . get_category_link( $cat->term_id ) . '" />';
echo '<img src="';
echo z_taxonomy_image_url($cat->term_id, 'products') . '" />';
echo '</a>';
echo '<h2><a href="' . get_category_link( $cat->term_id ) . '" class="cat-link"';
echo '/>' . $cat->cat_name . '</a></h2>';
echo '<a href="' . get_category_link( $cat->term_id ) . '" class="more-info" >Info</a>';
echo '</div>';
}
}
}
Please let me know if anyone has a better and cleaner solution.
i am not able to display the custom fields in a wordpress home page.
This is the code (fully working except for the part that show the custom field)
<?php
$thumbnails = get_posts('numberposts=3&cat=3');
foreach ($thumbnails as $thumbnail) {
if ( has_post_thumbnail($thumbnail->ID)) {
echo '<div class="contest-home"><a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
echo get_the_post_thumbnail($thumbnail->ID, 'medium', array('class'=> "blur") );
echo '</a>';
echo '<h2><center>' . esc_attr( $thumbnail->post_title ) . '</center></h2>';
echo get_post_meta($post->ID, "RiassuntoHome", true);
echo '<p><b>Giorni restanti:</b> 30 giorni';
echo '<br><b>Premio:</b> 10.000 euro';
echo '</p><center><a class="st-btn slarge st-btn-green st-btn-slarge" href="' . get_permalink( $thumbnail->ID ) . '">Invia la Tua Idea</a></center></div>';
}
}
?>
<?php echo '<div class="clearfix"></div></div>' ?>
The object is $thumbnail, not $post:
echo get_post_meta($thumbnail->ID, "RiassuntoHome", true);