Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Add some code snippet IF Custom-Field from a Post exists... WordPress

  • SOLVED

Hello everybody! So this is the code I am asking about:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">

<h2><?php the_title(); ?></h2><?php $subtitle = get_post_meta($post->ID, "Subtitle", true); echo $subtitle; ?>

<div class="entry">

<!-- container for the slides -->
<div class="images">

<div><?php $image1 = get_post_meta($post->ID, "Image1", true); echo $image2; ?></div>
<div><?php $image2 = get_post_meta($post->ID, "Image2", true); echo $image2; ?></div>
<div><?php $image3 = get_post_meta($post->ID, "Image3", true); echo $image3; ?></div>
<div><?php $image4 = get_post_meta($post->ID, "Image4", true); echo $image4; ?></div>
<div><?php $image5 = get_post_meta($post->ID, "Image5", true); echo $image5; ?></div>
<div><?php $image6 = get_post_meta($post->ID, "Image6", true); echo $image6; ?></div>

</div><!-- slider ENDE -->

<div id="frontcover_controller">
<!-- "previous slide" button -->
<a>prv</a>

<!-- "next slide" button -->
<a>nxt</a>

<div class="slidetabs">

<a href="#"></a>
<a href="#"></a>
<a href="#"></a>
<a href="#"></a>
<a href="#"></a>
<a href="#"></a>

</div>
</div><!-- frontcover_controller ENDE -->

<script type="text/javascript">
// What is $(document).ready ? See: http://flowplayer.org/tools/documentation/basics.html#document_ready
$(function() {

$(".slidetabs").tabs(".images > div", {

// enable "cross-fading" effect
effect: 'fade',
fadeOutSpeed: "slow",

// start from the beginning after the last tab
rotate: true

// use the slideshow plugin. It accepts its own configuration
}).slideshow({autoplay: true, autopause: true, clickable: false, interval: 10000});
});
</script>


As you can see I am adding exactly 6 Times the "Image1-Image6" Custom-Fields in my Query. When I ad these fields to a post the content of the fields will just appear at the place. But if I do NOT ad these fields in a post the just do not appear! Super cool!

As you can also see I have exactly 6 times the "<a href="#"></a>"-thing there. Each of these "<a href="#"></a>"-snippets is for one of the 6 "Image1-Image6" things... the problem I've got is, that if do not fill in on of the 6 Image1-Image6 fields I also just need as many "<a href="#"></a>"-Snippets as I've got Image1-Image6 fields.

If I make a post with just the fields Image1, Image2, Image3 and Image4 I also just need 4 times the "<a href="#"></a>"-Snippets.

So I hope you guys can tell me a way how to solve this. It's basically the idea of a
<strong>"if image# is there, then echo "<a href="#"></a>" .Thing</strong>

Thank a lot!

Answers (2)

2011-03-29

Denzel Chia answers:

Hi,

I am give you an example for one of your custom field image.



$image1 = get_post_meta($post->ID, "Image1", true);
if(!empty($image1)){

echo '<a href="#"></a>';
}



Just use the above for all your images.

Thanks

2011-03-29

MDan answers:

You can try something like this, which is more complete:


<?php $value = get_post_meta($post->ID, 'activities'(activities is the custom-field key for this example), true);

if($value == 'gaming') {
echo '<a href="http://yourdomain.tld/gaming/">Gaming Stuff</a>';
} elseif($value == 'sleeping') {
echo '<a href="http://yourdomain.tld/sleeping/">Nap Supplies</a>';
} elseif($value == 'eating') {
echo '<a href="http://yourdomain.tld/eating/">Dieting Advice</a>';
} else {
echo '<a href="http://yourdomain.tld/">Home Page</a>';
}

?>


Different links are output on the page,depending on the value of the activities key,


Thanks

Daniel