Oke, first of I downloaded this plugin. This enables me to give my custom taxonomy terms an image. This works great. But the way my theme is setup and the way the plugin wants to work, isn't for me.
I only want the images to show in the place where I say it should. That's why I used terms (categories). Because then I can use this code:
<?php
//list terms in a given taxonomy
$taxonomy = 'our_team';
$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>
That only shows the names of the team members when you activated them (checked the box) in the post, and not in other places.
Now I would like to be able to use a similar code but for the featured images. The plugin uses this code to get the thumbnails.
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="my-custom-class-name">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'post_id' => 15,
'taxonomy' => 'our_team',
) );
Does anyone know how to "merge" these two codes together and generate an image only if I checked the box in the custom post type. Or knows another way to do this?
** UPDATE **
Ok, getting somewhere. The code is now only displayed where I want it. With the help of Rohit Kishore! Thanks! Here is the working code:
<div class="teamleden over-ons-ul">
<?php $terms = get_the_terms( $post->ID , 'our_team' );
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="our-team">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'taxonomy' => 'our_team',
) );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'our_team' );
if( is_wp_error( $term_link ) )
continue;
}
?>
</div>
The print code had to be before the foreach part...
Related
I have two different post types where I want to have a relationship. Authors can list one business(post type - "listings") and many special offers (post type - "special_offers"). In each single special offer page, I need to display which business offers that specific offer. Currently I'm using this code, and it outputs the Business name correctly. I need the business logo to be there as well (featured image)
function get_author_business() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post_type' => 'listings', 'post__not_in' => array( $post->ID ), 'posts_per_page' => -1 ) );
foreach ( $authors_posts as $authors_post ) {
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
return $output;
}
This might help (i'm assuming you are trying to get the image tag inside your output html)
function get_author_business() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post_type' => 'listings', 'post__not_in' => array( $post->ID ), 'posts_per_page' => -1 ) );
foreach ( $authors_posts as $authors_post ) {
$business_img = wp_get_attachment_image_src( get_post_thumbnail_id( $authors_post->ID ), 'thumbnail' );
$output .= '<img src="'.$business_img[0].'">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
return $output;
}
You can use the following code :
<?php
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
$attachment_url=wp_get_attachment_image_src($post_thumbnail_id,'full');
/* full -- is image size */
?>
<img src="<?php echo $attachment_url; ?>">
hope that will work for you.
I have attempted random filters and other hook modifiers to try to get my echoed results on the Page content area NOT the header, and I have failed miserably. I am looking for any suggestions or hints as to how post the echoed results to the content area of the Page and not the header area. To clarify: My results are appearing on the Page I assigned them to BUT they are populating in the header of the page?? Not the main content body area.
add_action('pre_get_posts', 'cv_testimonials_list');
function cv_testimonials_list($query) {
if ($query->is_page('9595') && $query->is_main_query()) {
gravity_form(1, false, false, false, '', false);
$args = array (
'post_type' => 'testimonial',
'post_status' => 'published',
'pagination' => true,
'posts_per_page' => '10',
'order' => 'DESC',
'orderby' => 'date',);
// The Query
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
echo "<p><strong>" . the_content() . "</strong></p>";
echo "<p>" . the_title() . "</p>";
echo "<p><a target='_blank' href='http://" . get_post_meta( get_the_ID(), 'testimonials-website-url') . "'>" . get_post_meta( get_the_ID(), 'testimonials-website-url', true ) . "</a></p>";
//print "<pre>";
//print_r($custom_fields);
//print "</pre>";
endwhile;
return;
}
}
It's because you're using functions that echo their values, not return them. The values are being echoed when they're evaluated, not where they're needed.
Try using get_the_content()and get_the_title() instead.
EDIT
Sorry, just noticed where you're echoing your stuff. You don't need to run your query in pre_get_posts, just modify it, eg
$query->set( 'post_type', 'testimonial' );
WordPress will then run the query for you in the normal scheme of things.
Not sure what gravity_form is doing, but I doubt pre_get_posts is the right place for it. If what you're really trying to do is use that code as your page content, I'd move all your code out of the hook, into a custom page template, and assign page 9595 to that page template.
EDIT 2
Create a page template like this in your theme, then in your admin screen, assign it to page 9595 (and delete the code from pre_get_posts):
<?php
/**
* Template Name: Testimonial List
*/
$args = array (
'post_type' => 'testimonial',
'post_status' => 'published',
'pagination' => true,
'posts_per_page' => '10',
'order' => 'DESC',
'orderby' => 'date'
);
$query = new WP_Query( $args );
get_header(); ?>
<div id="content" class="widecolumn"><?php
gravity_form(1, false, false, false, '', false);
while ( $query->have_posts() ) : $query->the_post();
echo "<p><strong>" . get_the_content() . "</strong></p>";
echo "<p>" . get_the_title() . "</p>";
echo "<p><a target='_blank' href='http://" . get_post_meta( get_the_ID(), 'testimonials-website-url') . "'>" . get_post_meta( get_the_ID(), 'testimonials-website-url', true ) . "</a></p>";
endwhile;
?>
</div>
<?php get_footer(); ?>
In this case, you're not trying to change the main WordPress query, you're just not using it - you're using your own query in the template and looping over that.
I wrote some code that displays all attachment images in a UL to sidescroll on a custom post type. client now wants video returned too. Right now, I'm using a regex to parse all the images from the post but it's not grabbing video. Also, I hate depending on regex as it is impossible to also grab url etc with present implementation.
Is there a way to grab all attachments associated with a post (not including the custom-field-thumbnail), and then populate the attachments in a element on the page?
This would call all your attachment types - just add extra statements for other mime types (png, gif etc.)
<?php
$thumb_ID = get_post_thumbnail_id( $post->ID );
$attachments = get_posts(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC',
'exclude' => $thumb_ID,
));
if ( $attachments ) {
foreach( $attachments as $attachment ) {
$attachment_mime = wp_check_filetype(wp_get_attachment_url($attachment->ID) );
$title = apply_filters( 'the_title' , $attachment->post_title );
if ( $attachment_mime['type'] == 'image/jpeg') {echo '<li><img src="' . wp_get_attachment_url( $attachment->ID ) . '" alt="'.$title.'" /></li>'; }
elseif( $attachment_mime['type'] == 'video/mp4'){echo '<li><video class="video-js vjs-default-skin" controls data-setup="{}"><source src="'.wp_get_attachment_url( $attachment->ID ).'" type="video/mp4" /></video></li>';}
}
}?>
I'm trying to get the id of the latest post in each category, and use that id to get the meta info and thumbnail and display it next to the corresponding category. I'm just not sure how to do it.
I've been trying this code, but it isn't working for me:
<?php
$args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($args);
foreach($categories as $category) : ?>
<?php $randpost = get_posts(
array(
'numberposts' => 1,
'category' => array( get_query_var($category->id)),
));
$randpostid = ($randpost->ID);
?>
<?php echo '<h2 class="newsitem"><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h2> '; ?>
<?php echo '<p>'. $category->count . ' nummer</p>'; ?>
<strong>Stad:</strong>
<?php $city = get_post_meta($randpostid, 'city', true); ?>
<?php echo $city ?>
<?php endforeach; ?>
What am I doing wrong?
Everything you have looks correct, except for one line. You need to change:
'category' => array( get_query_var($category->id)),
To:
'category' => $category->cat_ID
Category objects do not have an 'id' property, but rather a 'cat_ID' property.
ADDITIONALLY: If for whatever reason that doesn't solve your problem, the only other thing I can think of would be to change this line:
$randpostid = ($randpost->ID);
To:
$randpostid = ($randpost[0]->ID);
get_posts() returns an array, but I'm not sure if it is in array format when single posts are returned. Either way, the first code change is a must, and the second is probably needed.
If you're just after displaying information from the most recent post you could probably do this in a much more simple fashion. Something like this in your page template should work (untested):
EDIT
Answer edited in light of OP comment:
<?php
$cat_args = array('orderby' => 'name','order' => 'ASC'); //for parameters see http://codex.wordpress.org/Function_Reference/get_categories
$categories=get_categories($cat_args);
foreach($categories as $category) { // for each category we as for the most recent post
$post_args = array('numberposts' => 1, 'category' => $category, 'orderby' => 'post_date', 'order' => 'DESC', );
$posts_array = get_posts( $post_args );
foreach($lastposts as $post) : setup_postdata($post); //Use setup_postdata to access parts of the object using regular WP template tags ?>
<?php post_id = get_the_ID(); // or you could just use $post->ID ?>
<!--Do your stuff here-->
<?php endforeach; ?>
<?php } ?>
When using wp_get_post_terms() I can produce a list of taxonomy terms associated with a post. However, I only want to show the taxonomy terms that have been selected for that post. Using the aforementioned function and get_terms() will successfully find the taxonomy terms, but it will show all of the terms. Not only the ones that have been selected. In the $args array for the functions I've looked for a 'selected' filter, but I found none and when I tried it, it didn't work.
Am I trying to do something that can't be done? I'm sure it's something that is starring me right in the face. I just want to ask the pro's before I make major changes to the way I'm doing things.
wp_get_post_terms only returns terms that have been selected for that post, it doesn't return all taxonomy terms.
http://codex.wordpress.org/Function_Reference/wp_get_post_terms
<?php
$the_selected = $_GET['cat'];
$args = array( 'post_type' => 'portfolio_item', 'posts_per_page' => 11, 'orderby' => 'id', 'order' => 'DESC', 'themes_categories' => "$the_selected");
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
This works well for me. I simply send the taxonomy slug to browser, and itterate through them with the code above.
I send by this:
<li>Filter By:</li>
<?php
$categories=get_categories($args);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '?cat=' . $category->slug.'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </li> ';
}
?>
You can try out this code, it worked for me. I have a taxonomy named 'stores' and i wanted to display 2 selected taxonomy from it. so i used a include feature.
<?php
$taxonomy = 'stores';
$args1=array(
'include'=> array(12,30)
);
$terms = get_terms('stores',$args1 );
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
<?php echo get_the_term_list( $post->ID, 'your_taxonamy'); ?>
And if you want it without the term linking you can use this
<?php $terms_as_text = get_the_term_list( $post->ID,'your_taxonamy'); if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) ,''; ?>