I am trying to figure out a way to remove permalink's from posts with the category 'nolink'. I have tried a few java script attempts - but have not managed to get it to work.
<?php
query_posts( 'tag=Client-list' );
while ( have_posts() ) : the_post();
echo '<ul class="client-thumb-wrap">';
echo '<a href="';
the_permalink();
echo '">';
echo '<li class="';
$category = get_the_category( $custompost );
echo $category[0]->cat_name ;
echo ' ';
echo $category[1]->cat_name ;
echo ' ';
echo $category[2]->cat_name ;
echo ' ';
echo $category[3]->cat_name ;
echo '">';
echo '<img src="';
the_field('client_logo');
echo '">';
echo '</li>';
echo '</ul>';
endwhile;
wp_reset_query();
?>
has_term allows you to check if a post has a specific term assigned (or not), try:
$href = ( has_term( 'nolink', 'category' ) ) ? '#' : get_permalink();
echo '<a href="' . $href . '">';
Related
Figured this would be pretty easy but I am running into an issue.
The website I am building, the client has a list of taxonomies that have an ACF Image Field and ACF Description field.
What they want to do is have a block where they can select certain ingredients from the Taxonomy Block, then have it render out formatted (on a page)(At this time it doesnt need to link to the actual category) but they want to do it this way so they dont need to update page by page when an ingredient changes description or image they can just change it in the taxonomy list.
Below is the code i am using to try and get it going from the docs, it wont render the name or original description it will render the slug but skips over the name but the slug is correct
I've been trying this with no luck, it just returns 3 li's which is correct but i can get a name or custom field to come through.
If i just the the_field('ingredients_selector'); I get the ID's just fine But for the life of me i can not get a term name or the ACF field attached to it/
$terms = get_field('ingredients_selector');
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul class="ingredients-list">';
foreach ( $terms as $term ) {
echo '<li class="ingredients-list__item">' . $term->name . '</li>'; ?>
<p>Description: <?php the_field('description', $term); ?></p>
<p>Image: <?php the_field('image', $term); ?></p>
<?php }
echo '</ul>';
}
?>
I've also tried this way, this gives me same reuslt but the slug with work, it will skip term name again but "view all" will link at least
<?php
$terms = get_field('ingredients_selector');
if( $terms ): ?>
<ul>
<?php foreach( $terms as $term ): ?>
<li>
<h2><?php echo esc_html( $term->name ); ?></h2>
<p>Term description: <?php the_field('description', $term); ?></p>
<p>Term Image: <?php the_field('image', $term); ?></p>
<p><?php echo esc_html( $term->description ); ?></p>
View all '<?php echo esc_html( $term->name ); ?>' posts
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Attached is my set up of the ACF fields
EDIT****
This was my solution
<?php
$tax = 'ingredients';
$terms = get_terms( $tax, $args = array(
'hide_empty' => false, // do not hide empty terms
));
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('image', 'ingredients_' . $term->term_id );
$description = get_field('description', 'ingredients_' . $term->term_id );
if( $term->count > 0 ) {
echo '<a href="' . esc_url( $term_link ) . '">';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
echo $term->name .'</a>';
echo $description;
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
Try this and replace term_name_ with your actual term slug:
<?php
$terms = get_field('ingredients_selector');
if( $terms ): ?>
<ul>
<?php foreach( $terms as $term ): ?>
<li>
<h2><?php echo esc_html( $term->name ); ?></h2>
<p>Term description: <?php the_field('description', 'term_name_'.$term->term_id); ?></p>
<p>Term Image: <?php the_field('image', 'term_name_'.$term->term_id); ?></p>
<p><?php echo esc_html( $term->description ); ?></p>
View all '<?php echo esc_html( $term->name ); ?>' posts
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
You can find more about this here.
This code working with image and title but post_per_page doesn’t work and trying
number' =>1
is work but if any taxonomy Less than one Or equal to one Info will not be showing.
<?php
$tax = 'studio';
$types = get_terms( array(
'post_per_page' => 1,
)
);
foreach($types as $type) { ?>
<?php
$term_link = get_term_link( $type );
$image = get_field('studio_avatar', 'studio_' . $type->term_id . '' );
if ( has_term( $type->term_id, 'studio')) {
echo '<a class="author author-avt inline" href="' . esc_url( $term_link ) . '">';
echo '<img class="si-user" src="' . $image['url'] . '" /> ';
echo '<span>'. $type->name . '</span>';
echo '</a>';
echo '<span class="posted-on inline">';
echo '<i class="si-clock"></i><time>'. $post_date = get_the_date("j F Y") .'</time>';
echo '</span>';
}
?>
<?php } ?>
I can't seem to pull a ACF image object for a custom taxonomy when used in this loop, which is made of echo blocks:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'product_type' );
$image = get_field('product_type_tax_image');
$hero_image = $image['sizes']['medium'];
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
var_dump($hero_image);
// We successfully got a link. Print it out.
echo '<a href="' . esc_url( $term_link ) . '" class="product-grid- block" style="' . $hero_image[0] . '">';
echo '<div class="product-grid-inner-txt-wrap">';
echo '<h4>' . $term->name . '</h4>';
echo '<p>' . $term->description . '</p>';
echo '</div>';
echo '</a>';
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
The var dump just returns NULL. I've looked at this and this but don't seem to help. What am I doing wrong?
Take a look at this article:
http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/
To pull a custom field from a Taxonomy Term, you need to add a second item to your get_field function call. Like this:
$image = get_field('product_type_tax_image', $term );
Also, it looks like you're trying to loop through $terms, but you're grabbing the custom field's value outside of your foreach loop, so that should get moved inside that loop, like so:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'product_type' );
foreach ( $terms as $term ) {
//get $term's image
$image = get_field('product_type_tax_image', $term );
$hero_image = $image['sizes']['medium'];
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
var_dump($hero_image);
// We successfully got a link. Print it out.
echo '<a href="' . esc_url( $term_link ) . '" class="product-grid- block" style="' . $hero_image[0] . '">';
echo '<div class="product-grid-inner-txt-wrap">';
echo '<h4>' . $term->name . '</h4>';
echo '<p>' . $term->description . '</p>';
echo '</div>';
echo '</a>';
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
In Wordpress 3.5.1 I want to create & use custom layout to display search result. I googled and found http://codex.wordpress.org/Creating_a_Search_Page but this is not exactly what I want.
Okay I solved my problem and now very happy :)
I write following script in search.php file
while ( have_posts() ) {
the_post();
//get_template_part( 'content', get_post_format() );
$permalink = get_permalink($post->ID);
$title = get_the_title($post->ID);
$feed = truncate( strip_tags( get_the_content($post->ID) ), 0, 300, "[...]");
echo '<div class="posts">';
echo '<div style="float:left">';
echo '<a class="featured-img">
'.the_post_thumbnail('thumbnail').'
</a>';
echo '</div>';
echo '<div class="posts-content">';
echo '<div class="heading">'.$title.'</div>';
echo '<div class="comments"> </div>'.
$feed
.'</div>';
echo '</div>';
}
Good Day. I am using a Gallery shortcode in my wordpress site to display images.
Problem is that I wasnt the gallery to show only images from a certain category, for example category id 35. How do I specify it in the shortcode?
Shortcode:
[custom_gallery style="1" source="**cat=%cat_id%**" link="image" description="0" size="200x200" limit="10"]
Now I have tried the following, but does not work -
cat=%35%
cat=%cat_id=35%
The shortcode code (because it is a custom shortcode):
/**
* Gallery posts shortcode
*/
function gallery_posts_func($atts, $content = null) {
extract(shortcode_atts(array(
"limit" => '5',
"cat" => '',
"thumb_width" => '',
"thumb_height" => '',
), $atts));
global $wp_query,$paged,$post;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$query .= '&posts_per_page='.$limit;
$query .= '&post_type=gallery';
$query .= '&taxonomy=gallery_cat';
$query .= '&gallery_cat='.$cat;
$wp_query->query($query);
ob_start();
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="gallery-holder fourthcol shortcode">
<?php
$gogo_gallery_video_url = get_post_meta($post->ID, 'gogo_gallery_video_url', true);
$gogo_gallery_links_to = get_post_meta($post->ID, 'gogo_gallery_links_to', true);
$gogo_gallery_title_links_to = get_post_meta($post->ID, 'gogo_gallery_title_links_to', true);
$gogo_gallery_custom_link = get_post_meta($post->ID, 'gogo_gallery_custom_link', true);
if ($gogo_gallery_title && $gogo_gallery_video_url && $gogo_gallery_title_links_to=="gallery_title_links_image") {
echo '<h5>';
echo '<a href="'.$gogo_gallery_video_url.'" rel="prettyPhoto[mixed]">';
echo ''.get_the_title().'';
echo '</a>';
echo '</h5>';
} elseif ($gogo_gallery_title && $gogo_gallery_title_links_to=="gallery_title_links_image") {
echo '<h5>';
echo '<a href="'.$thumbnail[0].'" rel="prettyPhoto[mixed]">';
echo ''.get_the_title().'';
echo '</a>';
echo '</h5>';
} elseif ($gogo_gallery_title && $gogo_gallery_title_links_to=="gallery_title_links_content") {
echo '<h5>';
echo '<a href="'.get_permalink().'">';
echo ''.get_the_title().'';
echo '</a>';
echo '</h5>';
} elseif ($gogo_gallery_title && $gogo_gallery_title_links_to=="gallery_title_links_link") {
echo '<h5>';
echo '<a href="'.$gogo_gallery_custom_link.'">';
echo ''.get_the_title().'';
echo '</a>';
echo '</h5>';
} elseif ($gogo_gallery_title) {
echo '<h5>';
echo ''.get_the_title().'';
echo '</h5>';
} else {
echo '';
}
?>
<div class="gallery-box">
<div class="gallery-image prettygallery">
<?php if (has_post_thumbnail()) { ?>
<?php
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
if ($gogo_gallery_video_url && $gogo_gallery_links_to=="gallery_links_image") {
echo '<a href="'.$gogo_gallery_video_url.'" rel="prettyPhoto[mixed]">';
echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&w='.$thumb_width.'&h='.$thumb_height.'&zc=1&q=100&s=1" alt="'.get_the_title().'" />';
echo '</a>';
} elseif ($gogo_gallery_links_to=="gallery_links_image") {
echo '<a href="'.$thumbnail[0].'" rel="prettyPhoto[mixed]">';
echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&w='.$thumb_width.'&h='.$thumb_height.'&zc=1&q=100&s=1" alt="'.get_the_title().'" />';
echo '</a>';
} elseif ($gogo_gallery_links_to=="gallery_links_content") {
echo '<a href="'.get_permalink().'">';
echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&w='.$thumb_width.'&h='.$thumb_height.'&zc=1&q=100&s=1" alt="'.get_the_title().'" />';
echo '</a>';
} elseif ($gogo_gallery_links_to=="gallery_links_link") {
echo '<a href="'.$gogo_gallery_custom_link.'">';
echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&w='.$thumb_width.'&h='.$thumb_height.'&zc=1&q=100&s=1" alt="'.get_the_title().'" />';
echo '</a>';
} else {
echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&w='.$thumb_width.'&h='.$thumb_height.'&zc=1&q=100&s=1" alt="'.get_the_title().'" />';
}
?>
<?php } ?>
</div>
<?php if ($gogo_gallery_short_desc) { ?><em><?php echo $gogo_gallery_short_desc; ?></em><?php } ?>
</div>
</div>
<?php endwhile; ?>
<?php $wp_query = null; $wp_query = $temp;
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode("gallery_posts", "gallery_posts_func");
What you are looking for are probably:
cat="35"