Displaying those Custom Meta Field with Value only in Wordpress - php

I have a Wordpress custom Meta Field and I want to display only those fields that have a value in it ( and not display those where the user has not put a value).
How I can go about doing this?

Just wrap your code to display in if condition:
<?php if (get_post_meta(get_the_ID(), "field_name", true)): ?>
<!-- here your html & meta echo or-->
<?php echo get_post_meta(get_the_ID(), "field_name", true) ?>
<?php endif; ?>

Related

Advanced Custom Fields repeater image caption

I am trying to display an image caption for a wordpress Advanced Custom Fields (ACF) repeater field, and haven't had any luck with the follow three options:
<?php if($middle_image['image']): ?>
<?php $midimage = wp_get_attachment_image_src($middle_image['image']); ?>
<?php $caption = $midimage->post_excerpt; ?>
<?php $captiontwo = $middle_image['image']['caption'] ?>
<?php $captionthree = $middle_image['image']->post_excerpt; ?>
<?php $alt = get_post_meta($middle_image['image'], '_wp_attachment_image_alt', true); ?>
<?php $main_image = wp_get_attachment_image_src($middle_image['image'], 'two-column-cropped' ); ?>
<div class="two-column-cropped"><img src="<?php echo $main_image[0]; ?>" alt="<?php echo $alt ?>" />
<?php if($caption) { ?>
<div class="image_caption"><?php echo $caption; ?></div>
<?php } ?>
</div>
<?php endif; ?>
Any suggestions would be appreciated. Thanks.
wp_get_attachment_src() does not fetch any data about an image besides the URL, height and width. You might want wp_get_attachment_metadata() however I think you would be better off changing your ACF field to return an Image Object (actually an array) instead of the Image ID (as I assume you have it now).
ACF can return any of three things for an image field: The image src URL, the Attachment ID (which can be passed to functions such as wp_get_attachment_image_src()), or the attachment information as an array.
You can check that you are receiving the correct response from ACF by using var_dump($middle_image) or var_dump($midimage)
I assume that $middle_image is your repeater field.
As long as ACF is configured correctly to return the image object (instead of the image URL or ID), you can simply remove this line:
<?php $midimage = wp_get_attachment_image_src($middle_image['image']); ?>
and then access the image caption using $middle_image['image']['caption']

Adding a custom attribute after the short description in magento

I'm quite new to magento and i'm struggling trying to add a custom attribute after the short description in products page
<?php if ($_product->getShortDescription()):?>
<div class="short-description">
<div class="std"<?php if($richSnippets['status'] == 1): ?> itemprop="description"<?php endif; ?>><h2><?php echo $this->__('Details:') ?></h2><?php echo $_helper>productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
</div>
<?php endif;?>
<?php $_item = $this->getProduct()->getId();
$_resource = $this->getProduct()->getResource();
$CustomAttribute = $_resource->getAttributeRawValue($_item, 'ratebeer', Mage::app()->getStore());
echo $CustomAttribute; ?>
Anyway the attribute does not appear after the short description...the same code works in another section of the same page so i don't really know what's going on
You should be able to just
echo $_product->getRatebeer();
But you may have to add the attribute to your flat tables first. You can do this by setting Include in product listing to Yes.
This is bad practice, but you could try this just to see if the attribute is set up correctly
echo $_product->load($_product->getId())->getRatebeer();

displaying wordpress pages not showing the correct format

I am using this PHP Code on my website to get wordpress page content from the database:
$page = get_page_by_id($records["value"], OBJECT, 'page');
query_posts('p='.$page->ID.'');
echo '<div id="page-title">'.$page->post_title.'</div>';
echo nl2br($page->post_content);
echo '<p> </p><hr /><p> </p>';
in wordpress, i have put the page with HTML and aligned images etc but when it displays on my website it just shows the images and text all under each other with the incorrect format (not as i put it in the text editor)
what have i done wrong here?
Im also wondering why you are doing like this? there is no need to call post title and content that way. simply make a page.php. The TwentyFourteen does have page template but in a folder named "page-templates". You can simply make a page.php by sample code below.
<?php get_header(); ?> //the header template
<?php while ( have_posts() ) : the_post(); ?> //start the loop
<?php the_title(); ?> //Page Title
<?php the_content(); ?> //The content
<?php endwhile; ?> //end loop
<?php get_sidebar(); ?> //sidebar template
<?php get_footer(); ?> // footer template
you can add div / span wherever you want.

Hide Div title if there's no content

I'm very newbie regarding PHP, I'm trying to configure my wordpress Loop.
I figured out how to display my customfield in my template, I manually added a title before my taxonomy and custom fields, but I'd like it doesn't show if the taxonomy is empty.
Here is the code:
<div class="customfields">
<h2 class="widgettitle">My Title</h2>
<?php echo do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
I would very much appreciate your help!
Thanks a ton,
T.
So code should be
<?php $taxonomy = do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<div class="customfields">
<?php if(!empty($taxonomy)) : ?>
<h2 class="widgettitle">My Title</h2>
<?php echo $taxonomy; ?>
<?php endif; ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
If you want to show content in HTML only if certain variables contain a value then you can create a simple if statement that uses PHP's isset function to determine whether or not the variables are set, and if true place some HTML on the page. You can read more about isset here.

Wordpress Text is not Shows line by line

I write a custom type for wordpress which is contain a textarea. I can save it and also i can access from single view. But i want to show them line by line if author save it line by line. But it's not showing them line by line, it shows all of them as a single line.
Here is my single.php's custom meta show codes;
<div id="yemek_resim">
<?php if($tz_image_display == 'true') : ?>
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) : /* if post has post thumbnail */ ?>
<?php the_post_thumbnail('single-large'); ?>
<?php endif; ?>
<?php endif; ?>
</div><!-- yemek_resim -->
<div id="yemek_malzemeleri">
<?php
$malzemeler = get_post_meta($post->ID, 'ofa_yemek_malzemeler', true);
echo esc_attr($malzemeler);
?>
</div><!-- yemek_malzemeleri -->
here yemek_malzemeler's content come from a textarea and author can write multiline content. But at site, they seem as a single line...
You should use the N2BR PHP function in the output file. Just write <?php echo n2br($string); ?> in the single.php file from Wordpress. Good luck ;)
I resolved the problem by using <pre> </pre> tags. thanks for your care.

Categories