Trouble getting taxonomy image with Taxonomy Meta plugin in Wordpress - wordpress

I am using the Taxonomy Meta plugin and have followed all instrauctions but feel like something is wrong with what I am doing. I just want to pull the image that is assigned to each category is a custom taxonomy and display it on a page template..
the github repo of the plugin can be found here: https://github.com/rilwis/taxonomy-meta
please help, I get everything to show but the image and when I look in the source the image path isnt there just an empty img tag.
I can get an image to print using this:
$meta = get_option('additional');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta['5']) ? $meta['5'] : array();
$images = $meta['community-image'];
echo '<ul>';
foreach ($images as $categories) {
// get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$src = wp_get_attachment_image_src($categories, 'thumbnail');
$src = $src[0];
$args=array(
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => 'properties_community'
);
$categories=get_categories($args);
// show image
foreach($categories as $category) {
echo '<li><a href="' . home_url() . '/?property_communities='. $category->slug .'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '><img src="'.$src.'"/>' . $category->name.'</a> </li> ';
}
}
echo '</ul>';
but I cannot figure out how to make it where the image matches the category being displayed.. I just put the number 5 in there to see if it worked which it does but if I try using $category->term_id it doesnt work and I am a bit lost from here..
UPDATE:
I am now able to pull the images assigned to each category and print the names but I am getting this error "Warning: Invalid argument supplied for foreach() in /home/../themes/../my-template.php on line 39"
Here is my code
$args = array( 'taxonomy' => 'properties_community' );
$terms = get_terms('properties_community', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="my_term-archive">';
foreach ($terms as $term) {
$meta = get_option('additional');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta[$term->term_id]) ? $meta[$term->term_id] : array();
$images = $meta['community-image'];
foreach ($images as $att) {
// get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$src = wp_get_attachment_image_src($att, 'medium');
$src = $src[0];
// show image
echo '<img src="'.$src.'" />' . $term->name . '';
}}
}
Anyone see why I may be getting this error? I dont see anything wrong with the foreach :/
Line 39 would be this: foreach ($images as $att) {

Got it
$args = array( 'taxonomy' => 'properties_community' );
$terms = get_terms('properties_community', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="my_term-archive">';
foreach ($terms as $term) {
$meta = get_option('additional');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta[$term->term_id]) ? $meta[$term->term_id] : array();
$images = $meta['community-image'];
if (empty($images)) {
echo '<p style="text-align:center;">' . $term->name . '</p>';
} else {
foreach ($images as $att) {
// get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$src = wp_get_attachment_image_src($att, 'medium');
$src = $src[0];
// show image
echo '<img src="'.$src.'" /><p style="text-align:center;">' . $term->name . '</p>';
}}
}
}

Related

ob_start() combined with wp_query

I'm making my first plugin and I have a problem with displaying my shortcode.
It shows in the top all the time but I have read some about ob_start(); and trying to use it but the shortcode just returns nothing.
Im using the code below - it seems to have something to do with my posts. Anyone know why how to solve this problem?
My WP_Query:
$query = new WP_Query( array(
'category__in' => $categories,
'posts_per_page' => $whpost_stored_meta_shortcode['whpost_number_of_posts'][0]
));
The code i use to display it:
ob_start();
echo '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
echo '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
the_title();
echo '</div>';
endwhile;
echo '</div>';
return ob_get_clean();
The complete code for the shortcode function:
<?php
function cpt_content_func( $atts ) {
// Get the ID we putted in into [wh-postgrid id="THIS_ID"]
extract( shortcode_atts( array(
'id' => null
), $atts ) );
// Get stored meta data (For the categories - needed to be formatted in
// a certain way)
$categories = get_post_meta( $id, 'whpost_cats', true );
// Get meta data for settings and such.
$whpost_stored_meta_shortcode = get_post_meta( $id );
// Get the correct categories and use the settings we got.
$query = new WP_Query( array(
'category__in' => $categories,
'posts_per_page' => $whpost_stored_meta_shortcode['whpost_number_of_posts'][0]
));
// Set the styles
switch ( $whpost_stored_meta_shortcode['whpost_posts_per_line'][0] ) {
case 1:
$post_width = '100%';
$post_max_height = '';
break;
case 2:
$post_width = '50%';
$post_max_height = '';
break;
case 3:
$post_width = '33.333333%';
$post_max_height = '';
break;
case 4:
$post_width = '25%';
$post_max_height = '';
break;
default:
$post_width = '50%';
}
// Display the front-end
ob_start();
echo '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
echo '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
the_title();
echo '</div>';
endwhile;
echo '</div>';
return ob_get_clean();
}
add_shortcode('wh-postgrid','cpt_content_func');
I think problem with your Query, you can add this line to check
if (!$query->have_posts()) {
return 'Empty result';
} else {
return ob_get_clean();
}
The function must return the output.
Change your code like this and it should work:
$output = '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$postId = get_the_ID();
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( $postId ), 'large' );
$output .= '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
$output .= get_the_title( $postId );
$output .= '</div>';
endwhile;
$output .= '</div>';
return $output;

How to display thumbnails in Wordpress for related posts by author?

I have a related posts by author function using this code:
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 10 ) );
$output = '<div class="block">';
foreach ( $authors_posts as $authors_post ) {
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
return $output; }
To output I use the following code inside the loop of my single.php file:
<?php echo get_related_author_posts(); ?>
Currently the function it displays only post titles as links.
How should look this wp code in order to display thumbnails for this related posts by author function?
Try:
foreach ( $authors_posts as $authors_post ) {
$output .= get_the_post_thumbnail($authors_post->ID);
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
check get_the_post_thumbnail for more information like image size and attributes like extra classes.
Let me know if you need more help.

Returning an image url from its attachment ID using a custom post type

I'm using a custom post type plugin that returns an uploaded file's attachment ID instead of its url.
I've been able to get the image to display using wp_get_attachment_image_src as outlined in the codex here http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src but my problem is getting it to play nicely with the code on the template page used to call the information from the custom post type.
Stripping it down to the basics, this is what calls the custom post type info from the template page:
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
echo '<img src="' . $slide['slide'] . '" />';
}
?>
I'm having difficulty reconciling this with what the codex provides:
<?php
$attachment_id = 8; // attachment ID
$image_attributes = wp_get_attachment_image_src( $attachment_id ); // returns an array
?>
<img src="<?php echo $image_attributes[0]; ?>">
It seems like something like the following should work, but I'm obviously missing something with the php syntax
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
echo '<img src="<?php echo $image_attributes[0]; ?>" />';
}
?>
Any thoughts would be appreciated, thanks.
I think this is what you want
if ( $post->post_type == 'slideshowplatform' && $post->post_status == 'publish' )
{
$attachments = get_posts(array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
));
if ($attachments) {
foreach ($attachments as $attachment) {
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo $thumbimg;
//$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
//echo '<img src="' . $image_attributes[0] . '" />';
}
}
}

wp_list_categories - with custom taxonomy

I'm back struggling with some more coding :(
I have written a custom PHP page to loop through a custom post type. Now what I want is to list the custom posts categories, and have each category link to the same page but pass the category to the query_posts() function.
My code is currently
<div id="left" style="float:left;width:200px">
<b>Categories</b><br><br>
<?php wp_list_categories( $args ); ?>
</div> <!-- end of left container -->
<div id="middle" style="float:left;width:700px">
<?php
$args = array(
'post_type'=> 'e-books',
'showposts' => '-1',
'eCats' => $cat,
'order' => 'ASC'
);
query_posts( $args );
where
$cat = $_GET['cat']
What I want is each link from the wp_list_categories() to be something like.
http://example.com/products.php?cat=fiction
Is this possible? I can't get my head around the Walker class :( I've got this far
class MyWalker extends Walker_Category {
function start_el(&$output, $category, $depth, $args) {
extract($args);
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( 'list_cats', $cat_name, $category );
$link = '<a href="products.php?cat="';
$link .= $cat_name;
$link .= '>';
$link .= $cat_name . '</a>';
if ( 'list' == $args['style'] ) {
$output .= "\t<li";
$class = 'cat-item cat-item-' . $category->term_id;
if ( !empty($current_category) ) {
$_current_category = get_term( $current_category, $category->taxonomy );
if ( $category->term_id == $current_category )
$class .= ' current-cat';
elseif ( $category->term_id == $_current_category->parent )
$class .= ' current-cat-parent';
}
$output .= ' class="' . $class . '"';
$output .= ">$link\n";
} else {
$output .= "\t$link<br />\n";
}
}
}
but the links aren't completing - they are showing as
?cat=
For listing category with the custom taxonomy :
You can you following code:
$catnames = get_terms( 'custom taxonomy name');
Then use foreach loop to disply the category.

Getting the post ID outside outside of the Wordpress loop

So I have a snippet of code that grabs the categories and their coinciding posts and lists them outside of the loop (Below). I've been trying to get the post to link to #post-[ID] instead of the permalink - but I keep failing. Can anyone help?
<ul id="sidebar">
<?php
foreach( get_categories('orderby=ID&order=desc') as $cat ) :
if( !$cat->parent ) {
echo '<li class="title"><h2>' . $cat->name . '</h2>';
echo '<ul>';
process_cat_tree( $cat->term_id );
}
endforeach;
wp_reset_query(); //to reset all trouble done to the original query
//
function process_cat_tree( $cat) {
$args = array('category__in' => array( $cat ), 'numberposts' => -1);
$cat_posts = get_posts( $args );
$id = $post->ID;
global $post;
if( $cat_posts ) :
foreach( $cat_posts as $menuPost ) :
echo '<li';
if ( $menuPost->ID == $post->ID ) { echo ' class="active"'; }
echo '>';
echo '' . $menuPost->post_title . '';
echo '</li>';
endforeach;
endif;
echo '</ul></li>';
}
?>
The above code is outputting UL/LI tags like this:
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
Admittedly, I don't exactly understand what you mean by "linking to #post-[ID]", but going with the question title:
If you use get_permalink() when echoing the link, you will get the permalink - that's just what that function does.
Use get_the_ID() instead, if you want the post-ID returned, or the_ID() if you want it displayed (the_ID() is the same as echo get_the_ID()).
Edited from here:
If you're otherwise happy with the above code, changing
echo '' . $menuPost->post_title . '';
to
echo '' . $menuPost->post_title . '';
ought to do it.
However, I'd go about it like so:
echo '<ul>';
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<li class="title"><h2>' . $category->name . '</h2><ul>';
// query posts of that category:
$query_args = array(
'cat' => $category->cat_ID,
'posts_per_page' => -1
);
$your_query = new WP_Query( $query_args );
// loop through them:
while ( $your_query->have_posts() ) : $your_query->the_post();
echo '<li><a href="#post-';
the_ID();
echo '">';
the_title();
echo '</a></li>';
endwhile;
echo '</ul></li>';
// Reset Post Data
wp_reset_postdata();
}
echo '</ul>';

Resources