How do i print INSIDE the defined tag?
When it ouputs this code, the_title() gets printed OUTSIDE (before) the h1 tag..
My code is:
<?php
if ( '' != get_the_post_thumbnail() ) {
print '<p>HEY</p>';
}
else {
print '<div class="page-header row full"><h1 class="page-title" itemprop="headline">', the_title() ,'</h1></div>';
}
?>
i have already tried:
<?php
if ( '' != get_the_post_thumbnail() ) {
print '<p>HEY</p>';
}
else {
print '<div class="page-header row full"><h1 class="page-title" itemprop="headline">'. the_title() .'</h1></div>';
}
?>
What am i doing wrong?
Thanks
You could do like below:
<?php if ( '' != get_the_post_thumbnail() ): ?>
<p>HEY</p>
<?php else: ?>
<div class="page-header row full"><h1 class="page-title" itemprop="headline"><?php the_title(); ?></h1></div>
<?php endif; ?>
Or use get_the_title()(which returns the title value) instead.
Try print "....".get_the_title().".....";
The methods "the_xxxx" print the value, the methods "get_the_xxx" returns the value.
try echo instead of print
echo - can output one or more strings
print - can only output one string, and returns always 1
Related
I am trying to change a CSS class, based on the contents of a variable called $project_cats which contains a category name. While $project_cats appears to be valid throughout my script, it is not valid within one of my if statements, at least that is my assumption.
How can I access $project_cats from within my if statement?
<?php
if ( $query->have_posts() )
{
?>
<div id="myresource">
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<?php
$terms = get_the_terms($post->id,"project-type");
$project_cats = NULL;
if ( !empty($terms) ){
foreach ( $terms as $term ) {
$project_cats .= strtolower($term->name) . ' ';
}
}
//$project_cats does not appear to have content in the below if statement
if ($project_cats == "webinars") {
$iconclass = "iconsmind-Video-4";
}
?>
<div class="nectar-icon-list" data-icon-color="default" data-icon-style="border" data-icon-size="small" data-animate="">
<div class="nectar-icon-list-item">
<div class="list-icon-holder" data-icon_type="icon" style="background-color:#1963af;">
<i class="icon-default-style <?php echo $iconclass; ?>" data-color="default"></i>
</div>
<div class="content">
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
<?php echo $project_cats;?>
</div>
</div>
</div>
<?php
}
?>
</div>
<?php
}
else
{
echo "No Results Found.";
}
?>
I just realized what's happening, in the if statement the name of the category is having a space ' ' appended to it, hence my if statement was never TRUE. Fixed.
I currently have these two blocks of php that are pulling results:
<?php
$webtech = article_custom_field('web-tech');
if ( !empty($webtech) ) :
?>
<div class="tech-list">
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
</div>
<?php
endif;
?>
And
<?php
$url = article_custom_field('site-url');
elseif ( !empty($url) ) :
?>
<div class="site-url">Visit</div>
<?php
endif;
?>
I want to combine them to output one single block, like:
<div class="tech-list">
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
<div class="site-url">Visit</div>
</div>
It needs to meet the following:
If web-tech exists, output it. Don't output site-url if it doesn't exist.
If web-tech exists, output it. If site-url exists, output it.
If site-url exists, output it. Don't output web-tech if it doesn't exist.
The containing div should not be output at all if neither variable does not exist.
Am I missing an obvious way to do it? It seems trivial, but I can't get the if/else/elseif statements to line up.
You could store your outputs in a variable, like so:
<?php
$output = '';
$webtech = article_custom_field('web-tech');
if ( !empty($webtech) ) :
$output .= '<div class="tech-title">Technologies:</div>'
. '<ul class="tech-ul">' . $webtech . '</ul>';
endif;
$url = article_custom_field('site-url');
if(!empty($url)) :
$output .= '<div class="site-url">Visit</div>';
endif;
if($output != ''):
echo '<div class="tech-list">';
echo $output;
echo '</div>';
endif;
?>
This way, only when there is something set in your output variable, will it show anything.
Does this solve your problem?
It sounds like you need to check both variables before you output the container that they exist in.
<?php
$webtech = article_custom_field('web-tech');
$url = article_custom_field('site-url');
if ( !empty($webtech) || !empty($url))
{
?>
<div class="tech-list">
<?php
if ( !empty($webtech) )
{
?>
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
<?php
}
if ( !empty($url) )
{
?>
<div class="site-url">Visit</div>
<?php
}
?>
</div>
<?php
}
?>
if (a or b) then {
OpenTechTitle;
if (a) SetAtext;
if (b) SetBtext;
CloseTechTitle;
}
Is there any reason why this is happening due to the following code? All that gets displayed is the variable , i.e the image.
<?php $featured_image = the_post_thumbnail();?>
<?php if (is_page(7) || is_page(12))
echo '<div class="featured_image">' . $featured_image . '</div>'
?>
<?php
$featured_image = the_post_thumbnail();
if (is_page(7) || is_page(12)) {
echo '<div class="featured_image">' . $featured_image . '</div>';
}
?>
The function itself echos the img: http://codex.wordpress.org/Function_Reference/the_post_thumbnail
try:
<?php
if ( has_post_thumbnail() && (is_page(7) || is_page(12))) {
echo '<div class="featured_image">';
the_post_thumbnail();
echo '</div>';
}
?>
What you are seeing is the return value of the echo function.
Argh, this code is not pulling through my custom meta.
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
if (!empty($post_meta)) {
?>
<div class='client-testimonial'><?php echo $my_meta['testimonial']; ?></div>
<div class='client-name'><?php echo $my_meta['name']; ?></div>
<?php
}
?>
But the one below works, the only reason I am not using it is because it still shows the speach marks and dash when the fields are left empty in the admin panel
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
echo "<div class='client-testimonial'>". "'".$my_meta['testimonial']."'". "</div>";
echo "<div class='client-name'>". "-" .$my_meta['name']."</div>";
?>
Please help me on why the first code is not echoing the info. I am at the end of my tether!
You're checking if $post_meta is not empty, you don't have a variable named $post_meta
Change:
if (!empty($post_meta))
to
if (!empty($my_meta))
i think u have checked wrong variable.
<?php
$my_meta = get_post_meta($post->ID,'_my_meta', true);
if (isset($my_meta) && !empty($my_meta)) {
?>
<div class='client-testimonial'><?php echo $my_meta['testimonial']; ?></div>
<div class='client-name'><?php echo $my_meta['name']; ?></div>
<?php
}
?>
I'm not entirely sure why this isn't working. But this piece of code does not work.
<?php
foreach ( $gallery_ids as $gallery ) {
echo '<div class="tgallery" rel="'.$gallery['gid'].'"><?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?></div>';
}
?>
I was guessing that maybe I'm putting the wrong quotes in the wrong place.
All the parts seperately work, as in :
I can display the 'gid' value with echo $gallery['gid']
I can make the div tags appear with the appropriate rel
I can, by itself, make <?php echo do_shortcode("[nggallery id=3]"); ?> work.
I just can't make the entire thing appear together.
You're mixing interpolated php and html, placing "<?php echo" inside what's already php.
<div class="tgallery" rel="<?php echo $gallery['gid'];?>">
<?php echo do_shortcode('[nggallery id="'.$gallery['gid'].'"]'); ?>
</div>
Why you put <?php ?> inside your echo ?
<?php
foreach ( $gallery_ids as $gallery )
{
echo '<div class="tgallery" rel="'.$gallery['gid'].'">'.do_shortcode('[nggallery id='.$gallery['gid'].']').'</div>';
}
?>
The issue
Pick either string concatenation or opening/closing PHP for HTML. You cannot combine both as you have done above.
echo '<div class="tgallery" rel="'.$gallery['gid'].'">
<?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?>
</div>';
The second line of code above does not belong inside a string as code in between the <?php ... ?> will not be parsed by PHP when it is contained in a string.
Solutions
Concatenation
I have fixed your code to use concatenation below:
foreach ( $gallery_ids as $gallery ) {
$shortcode = do_shortcode("[nggallery id={$gallery['gid']}]");
echo '<div class="tgallery" rel="' . $gallery['gid'] . '">' . $shortcode . '</div>';
}
Opening and closing PHP
This is how you would do it using PHP "templating":
<?php foreach($gallery_ids as $gallery ): ?>
<div class="tgallery" rel="<?php echo $gallery['gid']; ?>">
<?php echo do_shortcode("[nggallery id={$gallery['gid']}]"); ?>
</div>
<?php endforeach; ?>
You are already "in" php, so your opening tag is causing the problem:
<?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?>
It should be something like:
echo '<div class="tgallery" rel="'.$gallery['gid'].'">' . do_shortcode('[nggallery id='.$gallery['gid'].']') . '</div>';
<?php
foreach ( $gallery_ids as $gallery ) {
echo "<div class=\"tgallery\" rel=\"{$gallery["gid"]}\">". do_shortcode("[nggallery id=\"{$gallery["gid"]}\"]") ."</div>";
}
?>
try this....
<?php
foreach ( $gallery_ids as $gallery ) {
echo '<div class="tgallery" rel="'.$gallery['gid'].'">'.do_shortcode("[nggallery id=".$gallery['gid']."]").'</div>';
}
?>
you have inside a php statment
Try Notepad then it will colour code your php code, so you can clearly see what quotes etc you have wrong