Wordpress; image not appearing post loop - wordpress-theming

I have post about this before, but I didn't get any conclusive answer but I'm really hoping someone can help me. I have setup some custom post types, and with them, some custom fields using Wordpress 3's UI.
One of the fields I have set up is called banner_image, but in the loop, it doesn't output the image.
<?php echo get_post_meta($post->ID, 'banner_image', true); ?>
This simply outputs the ID number of the post. If I set the function to false, I get an array with this ID in and nothing else. How do I get the path to the image? I can't work this out and Googling reveals a sea of content not related to my problem, it's a real difficult one to search for so you're my only hope!
Many thanks,
Michael.
<?php
global $post;
$tmp_post = $post;
$args = array(
'post_status' => 'publish',
'post_type' => 'work',
'order' => 'DESC'
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php if( get_post_meta($post->ID, 'show_in_home_banner', true) == "yes" ) { ?>
<li class="slide">
<div class="slide-image">
<a href="<?php echo get_page_link($post->ID) ?>">
<?php echo get_post_meta($post->ID, 'banner_image', true); ?>
</a>
</div>
<div class="slide-content">
<h3 class="slide-header"><?php echo get_post_meta($post->ID, 'sub_title', true); ?></h3>
<p class="slide-title"><strong><?php echo the_title(); ?></strong></p>
</div>
</li>
<?php } ?>
<?php endforeach; ?>

Try this
<?php echo get_post_meta($post->ID, 'banner_image', $single); ?>

Apparently, the custom field 'banner_image' doesn't have the right value. I guess it doesn't save the correct value first. You may want to install the Simple WP FirePHP plugin (http://wordpress.org/extend/plugins/simple-wp-firephp/) and check the value with the fb() function.

Related

Image Slider flashes wrong image at first

I am using a wordpress theme, Pitch Pro, and it has a built in slider. My issue is that sometimes the slider will flash the wrong image for a second. I noticed that it does not do it normally but only if I go from a page such as support to the home page. I have tried changed and setting the order. Changing the max amount of slides, currently 6, but nothing seems to fix it.
The site currently is jrummy16.com/test if anyone has any idea on a fix.
Not sure if this could relate to it anyway but the server I am using has issues and to fix it hostgator told me to add define( 'CONCATENATE_SCRIPTS', false ); to my config.php file. It fixed the issue but I do not recall the slider having this issue before adding that.
I have no idea on how to even start troubleshooting this. So any help would be greatly appreciated.
EDIT
All of the files were found inside the Theme, Pitch Pro.
I opened up my home.php file and I find this at the top for slider.
<?php
$slider_template = apply_filters('pitch_slider_template', array('slider', null));
get_template_part( $slider_template[0], $slider_template[1] );
?>
I then went and opened up slider.php in the same folder. It has this code,
<?php
$slides = new WP_Query(array(
'numberposts' => siteorigin_setting('slider_max_slides'),
'nopaging' => true,
'post_type' => 'slide',
'orderby' => 'menu_order',
'order' => 'ASC'
));
if($slides->have_posts()){
?>
<div id="slider">
<div class="container">
<div class="slides nivoSlider">
<?php while ($slides->have_posts()) : $slides->the_post(); if(has_post_thumbnail()) : ?>
<?php if(get_post_meta(get_the_ID(), 'slide_destination', true)) : $destination = get_post_meta(get_the_ID(), 'slide_destination', true) ?>
<?php echo '<a href="'.esc_url(get_permalink($destination)).'" title="'.esc_attr(get_the_title($destination)).'">' ?>
<?php elseif(get_post_meta(get_the_ID(), 'slide_destination_url', true)) : $destination = get_post_meta(get_the_ID(), 'slide_destination_url', true) ?>
<?php echo '<a href="'.esc_url($destination).'">' ?>
<?php endif; ?>
<?php echo get_the_post_thumbnail(get_the_ID(), 'slide') ?>
<?php if(!empty($destination)) echo '</a>' ?>
<?php endif; endwhile; ?>
</div>
<?php $slides->rewind_posts(); ?>
<div class="indicators-wrapper">
<ul class="indicators">
<?php while ($slides->have_posts()) : $slides->the_post(); if(has_post_thumbnail()) : ?>
<li class="indicator <?php if($slides->current_post == 0) echo 'active' ?> indicator-group-<?php echo $slides->post_count ?>">
<div class="indicator-container">
<div class="pointer"></div>
<h4><?php the_title() ?></h4>
<?php the_excerpt() ?>
</div>
</li>
<?php endif; endwhile; ?>
</ul>
</div>
</div>
</div>
<?php
wp_reset_postdata();
}
I am not sure if this will help or not but I hope that it does.
I think the reason is that before slider loads completely, the page will show last image in the list of slides and in your case it is this one:
<img width="705" height="344" src="http://jrummy16.com/test/wp-content/uploads/BA-slider.jpg" class="attachment-slide wp-post-image" alt="BA-slider">
Here is a similar problem and it has few of the solutions which you may use it as well.
UPDATE:
I found this article which suggests to modify your CSS. I would paste the code here but the article uses image to show CSS code:)

Random Color CSS - Prevent Using Twice

I asked a question previously - HERE - about how I could set my border-color to a random color. I have this working.
As you'll see, I have a possible 7 colors in my array. How can I edit my code so that once a color has been chosen, it won't be reused. There are 3 divs on my page that the color will be used on, so I'd like 3 different colors from the possible 7.
Full code of how I'm using it:
<div id="featured-posts" class="container_12">
<?php
global $post;
$myposts = get_posts('numberposts=3&category=12');
foreach($myposts as $post) :
?>
<?php global $post; ?>
<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
?>
<?php
$colors = array("#000000", "#949c51", "#571c1e", "#f36533", "#782a80", "#f6a41d", "#ed1b24");
$randomColor = $colors[array_rand($colors)];
?>
<a href="<?php the_permalink(); ?>">
<div class="grid_4 featured-home" style="background: url(<?php echo $src[0]; ?> ) !important;">
<div class="featured-details" style="border-color: <?php echo $randomColor; ?>;">
<h4 style="color: <?php echo $randomColor; ?>;"><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?> <q style="color: <?php echo $randomColor; ?>; font-weight:bold !important;">Read more ></q></p>
</div>
<div class="featured-lower" style="border-color: <?php echo $randomColor; ?>;"></div>
</div></a>
<?php endforeach; ?>
</div>
You could shuffle and then pop the last element off the array each time:
Define your colours array once (this would need to be removed from your loop):
$colors = array("#000000", "#949c51", "#571c1e", "#f36533", "#782a80", "#f6a41d", "#ed1b24");
Then each time you use randomColor you shuffle it and remove the last element of the array to use:
shuffle($colors);
$randomColor = array_pop($colors);
You have to add a function where a random color is choosen from the array, deleted from array and returned. Then you have to call this function on your inline-style settings.
But you should really avoid inline-styles. You could also do something like this with SASS in a far more cleaner way.

How to loop through the custom post type once you have added to an existing query

I am a bit stuck with a project and I would appreciate if someone could help me with that one.
I've searched for some references everywhere (firstly of course here) and I only could find bits and pieces of what I need.
I need to end up with the following structure:
<!--LOOP 1 - WORD PRESS PARENT PAGES-->
<div id="post-parent1-name">...</div>
<div id="post-parent2-name">
<h2><?php the_title(); ?></h2>
<div class='post-content'><?php the_content(); ?></div>
<!--LOOP 2 - WORD PRESS CHILDREN PAGES-->
<div id="post-child1-name">...</div>
<div id="post-child2-name">
<h3><?php the_title(); ?></h3>
<!--LOOP 3 - WORD PRESS CUSTOM POST TYPE-->
<div id="custom-post-type-name">
<h4><?php the_title(); ?></h4>
<div class='post-content'><?php the_content(); ?></div>
</div>
</div>
<div id="post-child3-name">...</div>
</div>
<div id="post-parent3-name">...</div>
<div id="post-parent4-name">...</div>
I've used the following filter in my functions.php to add the custom post type to the query:
<?php
function insert_post( $query ) {
if ( $post->post_name == 'my_parent_post_name' && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'my_custom_post_type') );
return $query;
}
add_filter( 'pre_get_posts', 'insert_post' );
?>
I've managed to get the first and the second one to work fine however I could not figure out how to get the third one to work (which is my custom post type) in order to bring me the title, content and thumbnail respectively.
Sorry guys, I know that looks a bit confusing however I hope that makes sense.
Cheers,
Doug.
After several hours trying to find the answer for that problem I finally achieved my goal using:
<?php global $wp_query; ?>
Function Reference/query posts
I am still not quite sure about the usage of the action add_action( 'pre_get_posts' , 'ucc_pre_get_posts_filter' );, although I've manage to add a new array to my pre-existing query without affecting it:
Pre-existing query:
<?php query_posts(array('post_type' => 'page', 'order'=>'ASC', 'post_parent' => 0)); ?>
First Loop here
Inclusion of a new array using $wp_query:
<?php
$args = array_merge( $wp_query->query_vars, array( 'post_type' => 'my_custom_post_type', 'posts_per_page' => 5 ) );
query_posts( $args );
?>
Third Loop here
Cheers guys!

Wrapping code in a function prevents it from working

I want to wrap my code in a function (and then put it in functions.php) so that I can call it elsewhere but my code fails as soon as I wrap it in a function.
I think this may be a scope issue, do I have to pass the the post number somehow to the function? If I get rid of the function that's wrapped around the query, the code works fine.
I'm guessing that the code is irrelevant really (although I may be wrong) - it's more to do with the fact that it's a loop and a function.
<?php function getGallery2() { ?>
<!-- 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
<?php query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- 2. echo the test field -->
<?php $link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
<?php $alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
<img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
<?php endwhile;?>
<?php wp_reset_query(); ?>
<?php } ?>
<?php getGallery2(); ?>
You would have it something like this I think (not tested):
<?php function getGallery2() { ?>
$global post;
$link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
$alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
<img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
<?php } ?>
Then call the function within any loop on any PHP page. Make sense? i.e. don't loop within the function. I don't understand why you don't just use a php include? i.e.
require('get-gallery.php');
Hope that helps :D
$post is not in the functions scope.
You can add global $post; to the top of the function or you can include it as a parameter like this:
function getGallery2($post){
// code
}
echo getGallery2($post)
Code inside a function can only see variables that were created within the same function or in global scope. Meaning the $post object is undefined.
//
On a slightly off topic note, you have lots of HTML comments within PHP. You could easily tidy things p by making it all PHP.
EDIT:
function getGallery2(){
global $post;
// 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page');
while ( have_posts() ) : the_post();
// 2. echo the test field -->
$link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true);
$alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true);
echo '<img src="'.$link.'" alt="echo $alt " />';
endwhile;
wp_reset_query();
}
getGallery2();

Loop for custom post types filtered by a taxonomy in url

I've created a custom post type and a custom taxonomy to go with it.
I can't figure out how to program the loop on the taxonomy-{taxonomy}.php template to make it only show the posts that correspond with the current taxonomy archive page someone is on. <domain>taxonomy/term
I have created a loop that does filter based on taxonomy, however, its not dynamic, the loop works for whichever term of the custom taxonomy I program it to.
I would love a way to have it know which taxonomy archive (based on url?) the user is on, and only show posts from that taxonomy without having to create a template for each taxonomy term.
Current Loop:
<?php
$args = array( 'post_type' => 'event', 'type' => 'party', 'post_status' => 'future', 'posts_per_page' => 50, 'order' => 'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="event-item">
<div class="event-meta gold">
<div class="event-date"><?php the_time('M d'); ?></div>
<div class="event-time"><?php the_time('g:i A'); ?></div>
</div>
<div class="event-title">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?>
</a>
</div>
<div class="entrytext">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
Summary: having party be replaced with something that can change it based on which page a user is on would be spectacular!
Try using get_query_var('tag'), where 'tag' is the name of your custom taxonomy and add there result of that query to your $args.
I use it to create a tags archive page that changes display based on what tag I'm currently in.