Ask your WordPress questions! Pay money and get answers fast! (more info)

The use of if statements and style with php WordPress

  • SOLVED

Ok, The code below resides in my index.php and echo's my custom fields for my custom "Author" taxonomies respectively. Custom fields hold the author bios, displayed at the top of my index page when viewing an author.

If you look in the code below, I am also echoing text "echo 'AKA:',$aka;" I need to find some way for the text not to be echoed if their is no data inserted into the custom fields and I also need to style the echoed text "'AKA:'" separate from the echoed fields ($aka)

I think there is a way to do this with if and while statements but I am not fluent enough with php to design something like this. And for the styling I just need to be pointed in the right direction for best practice. Thank you.






<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$term_id = $term->term_id;

$t_id = $term_id;

$term_meta = get_option( "taxonomy_term_$t_id" );

$photo= $term_meta['photo'];
$name= $term_meta['name'];
$aka = $term_meta['aka'];
$birth = $term_meta['birth'];
$birth_place= $term_meta['birth_place'];
$death= $term_meta['death'];
echo '<img src="',$photo,'">';
echo 'Name:',$name;
echo 'AKA:',$aka;
echo 'Birth:',$birth;
echo 'Birth Place:',$birth_place;
echo 'Death:',$death;
echo 'Description:' . term_description();

?>

Answers (4)

2013-12-01

phppoet answers:

use this simple code inplace of your code

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

$term_id = $term->term_id;



$t_id = $term_id;



$term_meta = get_option( "taxonomy_term_$t_id" );






if( $term_meta['photo'] ) { echo '<img src="',$term_meta['photo'],'">'; }

if ($term_meta['name']) {echo 'Name:',$term_meta['name']; }

if ($term_meta['aka']) { echo '<font size="02" color="green" style="">AKA</font>:',$term_meta['aka']; }

if ($term_meta['birth']) { echo 'Birth:',$term_meta['birth']; }

if ($term_meta['birth_place']) { echo 'Birth Place:',$term_meta['birth_place']; }

if ($term_meta['death']) { echo 'Death:',$term_meta['death']; }

echo 'Description:' . term_description();



?>


you can style your aka text like this echo '<font size="02" color="green" style="">AKA</font>:',$term_meta['aka'];


phppoet comments:

use this simple code inplace of your code

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

$term_id = $term->term_id;



$t_id = $term_id;



$term_meta = get_option( "taxonomy_term_$t_id" );






if( $term_meta['photo'] ) { echo '<img src="',$term_meta['photo'],'">'; }

if ($term_meta['name']) {echo 'Name:',$term_meta['name']; }

if ($term_meta['aka']) { echo 'AKA:',$term_meta['aka']; }

if ($term_meta['birth']) { echo 'Birth:',$term_meta['birth']; }

if ($term_meta['birth_place']) { echo 'Birth Place:',$term_meta['birth_place']; }

if ($term_meta['death']) { echo 'Death:',$term_meta['death']; }

echo 'Description:' . term_description();



?>


Shane Pennington comments:

That make a lot more sense. Is there any way to do the same with the description?


phppoet comments:

so you want to style the description ? right then use this

echo '<font size="02" color="green" style="">Description:' . term_description() . '</font>';

inplace of echo 'Description:' . term_description();


Shane Pennington comments:

No no I want to add an if statement to the description like the others.


phppoet comments:

to make display description conditionally you need to use this type of code

$term_description=term_description();

if ($term_description != '') { echo 'Description:' . term_description(); }


full code will be like this

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

$term_id = $term->term_id;
$t_id = $term_id;
$term_meta = get_option( "taxonomy_term_$t_id" );


if( $term_meta['photo'] ) { echo '<img src="',$term_meta['photo'],'">'; }
if ($term_meta['name']) {echo 'Name:',$term_meta['name']; }
if ($term_meta['aka']) { echo '<font size="02" color="green" style="">AKA</font>:',$term_meta['aka']; }
if ($term_meta['birth']) { echo 'Birth:',$term_meta['birth']; }
if ($term_meta['birth_place']) { echo 'Birth Place:',$term_meta['birth_place']; }
if ($term_meta['death']) { echo 'Death:',$term_meta['death']; }
$term_description=term_description();
if ($term_description != '') { echo 'Description:' . term_description(); }
?>


Shane Pennington comments:

Very good works like a charm. Now I will attempt to style them.
Duncan says: It's very bad practice and very very outdated ( 1999 ) to use font tags. Please don't do that.
What else should be used there?


phppoet comments:

yeah he is right . let me provide you a code with latest method . wait


phppoet comments:

here is the full code

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );



$term_id = $term->term_id;

$t_id = $term_id;

$term_meta = get_option( "taxonomy_term_$t_id" );





if( $term_meta['photo'] ) { echo '<img src="',$term_meta['photo'],'">'; }

if ($term_meta['name']) {echo '<span class="custom-text">Name:</span>','<span class="author-text">'.$term_meta['name'].'</span>'; }

if ($term_meta['aka']) { echo '<span class="custom-text">AKA:</span>','<span class="author-text">'.$term_meta['aka'].'</span>'; }

if ($term_meta['birth']) { echo '<span class="custom-text">Birth:</span>','<span class="author-text">'.$term_meta['birth'].'</span>'; }

if ($term_meta['birth_place']) { echo '<span class="custom-text">Birth Place:</span>','<span class="author-text">'.$term_meta['birth_place'].'</span>'; }

if ($term_meta['death']) { echo '<span class="custom-text">Death:</span>','<span class="author-text">'.$term_meta['death'].'</span>'; }

$term_description=term_description();

if ($term_description != '') { echo '<span class="custom-text">Description:</span>' '<span class="author-text">'. term_description() .'</span>'; }

?>

<style>
.custom-text {
//css code for custom text here
}
.author-text {
//css code for author text here
}
</style>


normal text can be styled by putting code into .custom-text while author information text can be styled using .author-text class .

regards


phppoet comments:

you can style your text like this
<style>

.custom-text {

text-decoration: none;
font-style: italic;

}

.author-text {

text-decoration: none;
font-style: bold;

}

</style>


Shane Pennington comments:

Thank you sir. I will have to continue this tomorrow morning.


Shane Pennington comments:

Couldn't sleep.
Ok I am getting this error with above code;

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/quotetwe/public_html/wp-content/themes/Nexus/index.php on line 58

Line 58:if ($term_description != '') { echo '<span class="custom-text">Description:</span>' '<span class="author-text">'. term_description() .'</span>'; }


Shane Pennington comments:

Ok it works, missed a , between spans. Your work much appreciated!

2013-12-01

Duncan O'Neill answers:

Hi Shane,

try this for your php;


<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

$term_id = $term->term_id;
$t_id = $term_id;

$term_meta = get_option( "taxonomy_term_$t_id" );

if ($term_meta && is_single()){
foreach ($term_meta as $key => $value){
if ($value!=""){
if ($key=="photo"){
echo "<img src='".$value."'>";
}
else {
echo "<span class='key'>$key:</span><span class='value'>$value</span>";
}
}

}

echo "<span class='key'>Description:<span class='value'>" . term_description()."</span>";
}
?>


( updated after reply below)

and then in your theme's style.css, add something like this;


span.key {color:red;} /* or whatever color best suits maybe {font-weight:bold;} */
span.value {margin-right:1em;} /* if its being laid out horizontally */


The styling could be improved if I could see where is was being used. Second edit just tidied up the formatting, added span class=value, and wrapped the description in <span>s. It would probably be better to use a ul - let me know if you want an example using that.


Shane Pennington comments:

That's interesting. Some how you called the custom field names to display. Thats neat but for example birth_place has an underscore because of the way I set up the custom fields. The photo no longer displays. Other than that it worked on the author that had custom field data.

It broke on all authors with no custom field data.
"Warning: Invalid argument supplied for foreach() in /home/quotetwe/public_html/wp-content/themes/Nexus/index.php on line 44"
Line 44: foreach ($term_meta as $key => $value){


It also broke the a-z index page.
"Fatal error: Allowed memory size of 41943040 bytes exhausted (tried to allocate 71 bytes) in /home/quotetwe/public_html/wp-content/themes/Nexus/az-index-page.php on line 79"
Line 79: $categories = array_merge($categories,array());

If we were to get this to work could I style and position the Name:,Birth:,Death ect. separate from the data were echoing from the fields?

I have left it if you would like to take a look; http://quotetweet.com


Shane Pennington comments:

This is the only author with field data.
http://quotetweet.com/authors/abraham-lincoln/


Duncan O'Neill comments:

It's very bad practice and very very outdated ( 1999 ) to use <font> tags. Please don't do that.


Shane Pennington comments:

ok thanks


Shane Pennington comments:

what else should be used there?

2013-12-01

Hariprasad Vijayan answers:

Hello,


Try this
echo '<img src="'$photo.'">';
instead of
echo '<img src="',$photo,'">';

It help to display image i think.


Shane Pennington comments:

Nope but thanks
Parse error: syntax error, unexpected T_ECHO in /home/quotetwe/public_html/wp-content/themes/Nexus/index.php on line 50
Line 50: echo echo '<img src="'$photo.'">';


Hariprasad Vijayan comments:

could you pleas show complete code of that page?


Hariprasad Vijayan comments:

* please


Shane Pennington comments:

Whoops, now I put it in there right
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/quotetwe/public_html/wp-content/themes/Nexus/index.php on line 50
Line 50:echo '<img src="'$photo.'">';


Shane Pennington comments:

<?php get_header(); ?>



<?php
if ( is_category() && 'on' === et_get_option( 'nexus_category_featured', 'on' ) )
get_template_part( 'includes/featured' );
?>

<div class="page-wrap container">
<div id="main-content">
<div class="main-content-wrap clearfix">
<div id="content">
<?php get_template_part( 'includes/breadcrumbs', 'index' ); ?>

<div id="left-area">

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );



$term_id = $term->term_id;







$t_id = $term_id;







$term_meta = get_option( "taxonomy_term_$t_id" );





foreach ($term_meta as $key => $value){

if ($value!=""){

if ($key=="photo"){

echo '<img src="',$photo,'">';

}

else {

echo "<span class='key'>$key:</span>$value";

}

}



}





echo 'Description:' . term_description();







?>
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$thumb = '';
$width = (int) apply_filters( 'et_index_image_width', 240 );
$height = (int) apply_filters( 'et_index_image_height', 240 );
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
$thumb = $thumbnail["thumb"];

?>
<span class="bqstartindex">&#8220;</span>
<div class="recent-post-index">

<?php if ( '' !== $thumb ) : ?>
<div class="et-main-image">
<a href="<?php the_permalink(); ?>">
<!--<?php print_thumbnail( $thumb, $thumbnail[""], $titletext, $width, $height ); ?>-->
</a>

<div class="meta-info">
<div class="meta-date">

</div>
</div>
</div>
<?php endif; ?>

<div class="et-description">


<?php if ( ( $review_rating = get_post_meta( get_the_ID(), '_et_author_rating', true ) ) && '' !== $review_rating ) : ?>
<span class="review-rating"><span style="width: <?php echo $review_rating * 33.5; ?>px;"></span></span>
<?php endif; ?>

<?php et_nexus_post_meta(); ?>
<span class="class1">
<a href="<?php the_permalink(); ?>">

<?php
the_content();
?>
</a>
</span>
<?php
echo '<div class="share-time-index">';
echo do_shortcode ('[shareaholic app="share_buttons" id="435909"]');
echo '</div>';
echo '<div class="term-list-index-author">';
echo '<span class="class2">';
echo get_the_term_list( $post->ID, 'authors', ' - ', ', ', ' ' );
echo get_the_term_list( $post->ID, 'movies', ' ,', ', ', ' ' );
echo '</span>';
echo '</div>';


?>


<style type="text/css">
.class1 A:link {
text-decoration: none;
color: #444444;
}
.class1 A:visited {
text-decoration: none;
color: #444444;
}
.class1 A:active {
text-decoration: none;
}
.class1 A:hover {
text-decoration: none;
color: #0098D3;
}
.class2 A:link {
text-decoration: none;
color: #7a7a7a;
font-style: italic;
font-weight: 300;
}
.class2 A:visited {
text-decoration: none;
color: #7a7a7a;
font-style: italic;
font-weight: 300;
}
.class2 A:active {
text-decoration: none;
font-style: italic;
font-weight: 300;
}
.class2 A:hover {
text-decoration: none;
font-style: italic;
color: #0098D3;
font-weight: 300;


</style>



</div> <!-- .et-description -->

<!--<a href="<?php the_permalink(); ?>" class="read-more"><span><?php esc_html_e( 'More>', 'Nexus' ); ?></span></a>-->
</div> <!-- .recent-post -->

<?php
endwhile;

if ( function_exists( 'wp_pagenavi' ) )
wp_pagenavi();
else
get_template_part( 'includes/navigation', 'index' );
else :
get_template_part( 'includes/no-results', 'index' );
endif;
?>
</div> <!-- end #left-area -->
</div> <!-- #content -->

<?php get_sidebar(); ?>
</div> <!-- .main-content-wrap -->

<?php get_template_part( 'includes/footer-banner', 'index' ); ?>
</div> <!-- #main-content -->

<?php get_footer(); ?>


Hariprasad Vijayan comments:

Try this in your php

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$term_id = $term->term_id;
$t_id = $term_id;
$term_meta = get_option( "taxonomy_term_$t_id" );
$photo= $term_meta['photo'];
$name= $term_meta['name'];
$aka = $term_meta['aka'];
$birth = $term_meta['birth'];
$birth_place= $term_meta['birth_place'];
$death= $term_meta['death'];
if(!empty($photo))
{
echo '<img src="'.$photo.'">';
}
if(!empty($name))
{
echo "<span class='custom-title'>Name:</span>".$name;
}
if(!empty($aka))
{
echo "<span class='custom-title'>AKA:</span>".$aka;
}
if(!empty($birth))
{
echo "<span class='custom-title'>Birth:</span>".$birth;
}
if(!empty($birth_place))
{
echo "<span class='custom-title'>Birth Place:</span>".$birth_place;
}
if(!empty($death))
{
echo "<span class='custom-title'>Death:</span>".$death;
}
echo "<span class='custom-title>Description:</span>" . term_description();

?>


Hariprasad Vijayan comments:

And add this in your CSS.

.custom-title {color:#0098d3;}


Hariprasad Vijayan comments:

Is that code works for you?


Shane Pennington comments:

I'm sorry but phppoets code worked best. Thank you.


Hariprasad Vijayan comments:

You can try this for checking description
if(!empty(term_description()))
{
echo "<span class='custom-title'>Description:</span>" . term_description();
}


Hariprasad Vijayan comments:

So the complete code would be

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$term_id = $term->term_id;
$t_id = $term_id;
$term_meta = get_option( "taxonomy_term_$t_id" );
$photo= $term_meta['photo'];
$name= $term_meta['name'];
$aka = $term_meta['aka'];
$birth = $term_meta['birth'];
$birth_place= $term_meta['birth_place'];
$death= $term_meta['death'];
if(!empty($photo))
{
echo '<img src="'.$photo.'">';
}
if(!empty($name))
{
echo "<span class='custom-title'>Name:</span>".$name;
}
if(!empty($aka))
{
echo "<span class='custom-title'>AKA:</span>".$aka;
}
if(!empty($birth))
{
echo "<span class='custom-title'>Birth:</span>".$birth;
}
if(!empty($birth_place))
{
echo "<span class='custom-title'>Birth Place:</span>".$birth_place;
}
if(!empty($death))
{
echo "<span class='custom-title'>Death:</span>".$death;
}
if(!empty(term_description()))
{
echo "<span class='custom-title'>Description:</span>" . term_description();
}
?>
<style type="text/css">
.custom-title {color:#0098d3;}
</style>


Shane Pennington comments:

Thank you for your time. I think phppoet had the best solution for my situation.


Hariprasad Vijayan comments:

You are welcome.

Cheers..

2013-12-01

Ryan S answers:

Okay since you want to check if there is a data being returned on each custom fields so that we need is check those fields


echo '<div class="more-info">';
// only display not empty field
if( $photo )
echo '<img src="'.$photo.'">';

if( $name )
echo '<label>Name:</label> '. $name;

if( $aka )
echo '<label>AKA:</label> '. $aka;

if( $birth )
echo '<label>Birth:</label> ' . $birth;

if( $birth_place )
echo '<label>Birth Place:</label> ' . $birth_place;

if( $death )
echo '<label>Death:</label> ' . $death;

if( term_description() )
echo '<label>Description:</label> ' . term_description();
echo '</div>';


it's a nice clean code for adding if statement to only display not empty field

so next is adding CSS, we added a DIV wrapper with more-info class in it so we can add our css code with ease

div.more-info label{ font-weight: bold; /*add your css code more here*/ }


adding inline CSS with PHP

echo '
<style type="text/css">
div.more-info label{ font-weight: bold; /*add your css code more here*/ }
</style>
';

// escaping with PHP tag
?>
<style type="text/css">
div.more-info label{ font-weight: bold; /*add your css code more here*/ }
</style>
<?php



Hope that helps



Shane Pennington comments:

Thank you for your time. I think phppoet had the best solution for my situation.