Proper way to write this php conditional statement - php

I am not well versed in php as I'm a newbie in it. I'm trying to use these lines of code but getting error. What is the proper way to write this:
<?php
if ( function_exists( 'wpsabox_author_box' ) ) {
echo wpsabox_author_box();
} else {
echo (
'<div class="postauthor">
<div class="authorprofilepix">'
get_avatar( get_the_author_id() , 80 );
'</div>
<div class="authorprofile">
<h4>' the_author(); '</h4>
<p>' the_author_description(); '</p>
</div>
<div class="clearfix"></div>
</div><!--end postauthor-->');
}
?>
Thanks in anticipation!

You should add dots between the strings and function calls inside the echo call.
e.g.
echo ('string' . function() . ' string ');

use this way :
<?php if (function_exists( 'wpsabox_author_box' ) ) {
echo wpsabox_author_box();
} else { ?>
<div class="postauthor">
<div class="authorprofilepix">'
<?php echo get_avatar( get_the_author_id() , 80 ); ?>
</div>
<div class="authorprofile">
<h4><?php echo the_author(); ?></h4>
<p><?php echo the_author_description(); ?></p>
</div>
<div class="clearfix"></div>
</div><!--end postauthor-->
<?php }
?>

Here's how your code should look in order to work:
<?php
if ( function_exists( 'wpsabox_author_box' ) ) {
echo wpsabox_author_box();
} else {
echo '<div class="postauthor">
<div class="authorprofilepix">' . get_avatar(get_the_author_id(), 80 ); . '</div>
<div class="authorprofile">
<h4>' . the_author() . '</h4>
<p>' . the_author_description() . '</p>
</div>
<div class="clearfix"></div>
</div><!--end postauthor-->';
}
?>
Here's the prefered solution:
<?php if (function_exists( 'wpsabox_author_box' ) ) {
echo wpsabox_author_box();
} else { ?>
<div class="postauthor">
<div class="authorprofilepix">
<?php echo get_avatar( get_the_author_id() , 80 ); ?>
</div>
<div class="authorprofile">
<h4><?php the_author(); ?></h4>
<p><?php the_author_description(); ?></p>
</div>
<div class="clearfix"></div>
</div><!--end postauthor-->
<?php }
?>
As you can see it's slightly correctd Ravi's solution. I can't say it's better but I much prefer it to the other cause it's much more clear.
One more thing. Don't use the_author_description() function, use the_author_meta('description') instead.

Use commas to avoid string concatenation. Also, use get_the_author_meta() as both the get_the_author_id() and the_author_description() have been deprecated.
if ( function_exists( 'wpsabox_author_box' ) ) {
echo wpsabox_author_box();
} else {
echo '<div class="postauthor">
<div class="authorprofilepix">',
get_avatar( get_the_author_meta('ID') , 80 ),
'</div>
<div class="authorprofile">
<h4>', get_the_author(), '</h4>
<p>', get_the_author_meta('description'), '</p>
</div>
<div class="clearfix"></div>
</div><!--end postauthor-->';
}

Related

PHP code in Sidebar Not Working Correctly

I am having some issues with my dynamic php sidebar and wanted to see if anyone finds any issues with my code. Right now, the aside and other sidebar classes are closing sooner than the end of the code. I was thinking there might be something else wrong with my code that I'm not aware of.
I have a php widget plugin installed, and the below code is written in the sidebar:
<div class="sidebar-container">
<button class="accordion" style="h5">Education</button>
<div class="accordion-content">
<p><?php echo '<span class="sidebar">' . get_field('education') . '</span>'; ?></p>
</div>
<?php if( get_field('licensescertifications') ) : ?><button class="accordion">Licenses/Certifications</button>
<div class="accordion-content">
<p><?php echo '<span class="sidebar">' . the_field('licensescertifications') . '</span>'; ?></p>
<?php endif; ?>
</div>
<?php if( get_field('honors') ) : ?><button class="accordion">Honors</button>
<div class="accordion-content">
<p><?php echo '<span class="sidebar">' . the_field('honors') . '</span>'; ?></p>
<?php endif; ?>
</div>
<?php if( get_field('publications') ) : ?><button class="accordion">Publications</button>
<div class="accordion-content">
<p><?php echo '<span class="sidebar">' . the_field('publications') . '</span>'; ?></p>
<?php endif; ?>
</div>
<?php if( get_field('affiliations') ) : ?><button class="accordion">Affiliations</button>
<div class="accordion-content">
<p><?php echo '<span class="sidebar">' . the_field('affiliations') . '</span>'; ?></p>
<?php endif; ?>
</div>
<?php if( get_field('languages') ) : ?><button class="accordion">Languages</button>
<div class="accordion-content">
<p><?php the_field('languages'); ?></p>
<?php endif; ?>
</div>
</div>
Maybe you should replace position of "endif" in each block? And replace "the_field" with "get_field" in "echo" line
<?php if( get_field('licensescertifications') ) : ?>
<button class="accordion">Licenses/Certifications</button>
<div class="accordion-content">
<p><?php echo '<span class="sidebar">' . get_field('licensescertifications') . '</span>'; ?></p>
</div>
<?php endif; ?>
Or shortly:
<?php if( $field_data = get_field('licensescertifications') ) : ?>
<button class="accordion">Licenses/Certifications</button>
<div class="accordion-content">
<p><?php echo '<span class="sidebar">' . $field_data . '</span>'; ?></p>
</div>
<?php endif; ?>

If Statement Condition If Element Exists Using PHP

I want to check if an element exist then show the div, if it doesn't then hide the div. I'm lost as to exactly how to place it. If I add for both divs combined or each div has to have an IF.
<?php if() ?> //if there is no content or image don't show this entire div
<div class="row-fullsize archive-header">
<div class="small-12 large-6 columns">
<?php $category_header_src = woocommerce_get_header_image_url();
if( $category_header_src != "" ) {
echo '<div class="woocommerce_category_header_image"><img src="'.$category_header_src.'" /> //This is waiting for an image to be uploaded. If it is uploaded show the entire div. Even if nothing is in the other div.
</div>';
}
?>
</div>
<div class="small-12 large-6 columns"> //This is the other div. With a H1 Heading and paragraph. It doesn't need a condition but needs to be a part of the entire row-fullsize.
<div class="hd-woocom-description">
<div class="hd-woo-content">
<h1><?php echo $productCatMetaTitle = get_term_meta( get_queried_object_id(), 'wh_meta_title',
true); ?></h1>
<p><?php echo $productCatMetaDesc = get_term_meta( get_queried_object_id(),
'wh_meta_desc', true); ?></p>
</div>
</div>
</div>
</div>
<?php endif; ?> // end it here??
If I understand your question correctly, this is what you wanted to achieve? Assuming your code works properly just that the if statement is wrong/incorrect.
<div class="row-fullsize archive-header">
<?php $category_header_src = woocommerce_get_header_image_url(); ?>
<?php if( $category_header_src ) : ?>
<div class="small-12 large-6 columns">
<?php echo '<div class="woocommerce_category_header_image"><img src="' . $category_header_src . '" /></div>'; ?>
</div>
<?php endif; ?>
<div class="small-12 large-6 columns">
<div class="hd-woocom-description">
<div class="hd-woo-content">
<h1><?php echo get_term_meta( get_queried_object_id(), 'wh_meta_title', true); ?></h1>
<p><?php echo get_term_meta( get_queried_object_id(), 'wh_meta_desc', true); ?></p>
</div>
</div>
</div>
</div>
Thanks everybody for assisting me with this. I ended up coding it a different way. It's not clean but it works until I can figure out a cleaner way to do it. Appreciate all the help.
<?php $category_header_src = woocommerce_get_header_image_url();
if( $category_header_src != "" ) {
echo '<div class="row-fullsize archive-header">';
echo '<div class="small-12 large-6 columns">';
echo '<div class="woocommerce_category_header_image"><img
src="'.$category_header_src.'" /></div>';
echo '</div>';
echo '<div class="small-12 large-6 columns">';
echo '<div class="hd-woocom-description">';
echo '<div class="hd-woo-content">';
echo '<h1>',esc_attr ( get_term_meta( get_queried_object_id(),
'wh_meta_title', true )),'</h1>';
echo '<p>', esc_attr( get_term_meta( get_queried_object_id(),
'wh_meta_desc', true )),'</p>';
echo '<p><?php echo $productCatMetaDesc = get_term_meta(
get_queried_object_id(), wh_meta_desc, true); ?></p>';
echo '</div></div></div></div>';
}
?>

Error in wordpress function.php file

I am trying to add a particular shortcode to my theme, but i get an error when i paste the below code in my themes function.php. I already tried including once in the function.php but that did not work.
I think the problem is because of the <?php prefix and ?> suffix it has which is prohibited in wordpress fucntion.php. I had removed all <?php and ?> but it still generates an error.
// Add Shortcode
function acardio_big_grid_1() {
<div class="col-md-12 featt big-grid">
<div class="container-fluid no-padding">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'post', 'posts_per_page' => 5, 'paged' => $paged);
$query = new WP_Query($args);
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<div class="big-pane col-xs-6">
<div class="the-big-pane image">
<div class="img-responsive"> <?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail('big-grid-one-image'); ?>
<?php else : ?>
<img src="
<?php if (function_exists('get_option_tree') ) : if (get_option_tree('defualt_fatured_uploader')) : ?>
<?php get_option_tree( 'defualt_fatured_uploader', '', 'true' ); ?>
<?php else : ?>
<?php echo get_template_directory_uri (); ?>/img/nothumb-featured.png
<?php endif; endif; ?>
" />
<?php endif; ?></div>
</div>
<div class="big-pane overlay">
<div class="the-big-pane-content">
<div class="the-cont-cat">
<span class="the-content-cat-bt"> <?php $category = get_the_category(); if ($category)
{ echo wp_kses_post('<a href="' . get_category_link( $category[0]->term_id ) . '" class="tiptipBlog" title="' . sprintf( esc_html__( "View all posts in %s", "acardio" ), $category[0]->name ) . '" ' . '>' . $category[0]->name.'</a> ');} ?></span> <span class="the-content-cat-bt"> <?php taqyeem_get_score(); ?> </span>
</div>
<h1><?php echo wp_trim_words( get_the_title(), 15 ); ?></h1>
<span class="the-content-post-data"><span class="the-auth-post-image-content" style="padding-top: 0px !important;"> <?php echo get_avatar( get_the_author_meta( 'user_email' ), 100 ); ?> </span> By <?php the_author_posts_link(); ?> <span class="the-content-post-date"> <i class="fa fa-clock-o the-content "></i><?php the_time('jS M, Y') ?></span> </span>
</div>
</div>
</div>
<?php elseif ($count == 2) : ?>
<div class="small-pane-holder1 col-xs-6">
<div class="small-pane col-xs-6 one">
<div class="the-small-pane-image">
<?php the_post_thumbnail();?>
</div>
<div class="small-pane-overlay">
<div class="small-pan-content">
<div class="the-cont-cat">
<span class="the-content-cat-bt"> <?php $category = get_the_category(); if ($category)
{ echo wp_kses_post('<a href="' . get_category_link( $category[0]->term_id ) . '" class="tiptipBlog" title="' . sprintf( esc_html__( "View all posts in %s", "acardio" ), $category[0]->name ) . '" ' . '>' . $category[0]->name.'</a> ');} ?></span> <span class="the-content-cat-bt"> <?php taqyeem_get_score(); ?> </span>
</div>
<h1 class="small-pane-title"><?php echo wp_trim_words( get_the_title(), 10 ); ?></h1>
</div>
</div>
</div>
<?php elseif ($count == 3) : ?>
<div class="small-pane col-xs-6 two">
<div class="the-small-pane-image">
<?php the_post_thumbnail();?>
</div>
<div class="small-pane-overlay">
<div class="small-pan-content">
<div class="the-cont-cat">
<span class="the-content-cat-bt"> <?php $category = get_the_category(); if ($category)
{ echo wp_kses_post('<a href="' . get_category_link( $category[0]->term_id ) . '" class="tiptipBlog" title="' . sprintf( esc_html__( "View all posts in %s", "acardio" ), $category[0]->name ) . '" ' . '>' . $category[0]->name.'</a> ');} ?></span> <span class="the-content-cat-bt"> <?php taqyeem_get_score(); ?> </span>
</div>
<h1 class="small-pane-title"><?php echo wp_trim_words( get_the_title(), 10 ); ?></h1>
</div>
</div>
</div>
<?php elseif ($count == 4) : ?>
<div class="small-pane col-xs-6 three">
<div class="the-small-pane-image">
<?php the_post_thumbnail();?>
</div>
<div class="small-pane-overlay">
<div class="small-pan-content">
<div class="the-cont-cat">
<span class="the-content-cat-bt"> <?php $category = get_the_category(); if ($category)
{ echo wp_kses_post('<a href="' . get_category_link( $category[0]->term_id ) . '" class="tiptipBlog" title="' . sprintf( esc_html__( "View all posts in %s", "acardio" ), $category[0]->name ) . '" ' . '>' . $category[0]->name.'</a> ');} ?></span> <span class="the-content-cat-bt"> <?php taqyeem_get_score(); ?> </span>
</div>
<h1 class="small-pane-title"><?php echo wp_trim_words( get_the_title(), 10 ); ?></h1>
</div>
</div>
</div>
<?php elseif ($count == 5) : ?>
<div class="small-pane col-xs-6 four">
<div class="the-small-pane-image">
<?php the_post_thumbnail();?>
</div>
<div class="small-pane-overlay">
<div class="small-pan-content">
<div class="the-cont-cat">
<span class="the-content-cat-bt"> <?php $category = get_the_category(); if ($category)
{ echo wp_kses_post('<a href="' . get_category_link( $category[0]->term_id ) . '" class="tiptipBlog" title="' . sprintf( esc_html__( "View all posts in %s", "acardio" ), $category[0]->name ) . '" ' . '>' . $category[0]->name.'</a> ');} ?></span> <span class="the-content-cat-bt"> <?php taqyeem_get_score(); ?> </span>
</div>
<h1 class="small-pane-title"><?php echo wp_trim_words( get_the_title(), 10 ); ?></h1>
</div>
</div>
</div>
</div>
<?php else : ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
}
add_shortcode( 'acardio-big-grid-1', 'acardio_big_grid_1' );
Right near the top you have a syntax error - an issue with your PHP tags. You need a closing php tag before the HTML, and an opening PHP tag after. The top of the code should read like so:
function acardio_big_grid_1() {
?>
<div class="col-md-12 featt big-grid">
<div class="container-fluid no-padding">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'post', 'posts_per_page' => 5, 'paged' => $paged);
You have a similar problem near the end of the code sample, but this time missing a opening php tag:
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<?php
}
add_shortcode( 'acardio-big-grid-1', 'acardio_big_grid_1' );
No guarantees that that is all your issues, but that would be one key issue in the code sample you provided.

Wordpress PHP Variable with multiple values

I'm building an archive page in WordPress that is pulling content from certain page IDs. Is there a way to create a variable that would go in place of 266 in the code below so I don't have to recreate this code for each ID?
<div class="col-md-4">
<div class="ih-item square effect6 from_top_and_bottom">
<a href="<?php echo get_the_permalink( 266 ); ?>">
<div class="img">
<img src="<?php the_field('photo', 266); ?>" />
</div>
<div class="info">
<h4><?php echo get_the_title( 266 ); ?></h4>
</div>
</a>
</div>
</div>
Wrap you code in a function and place that function to functions.php
and just call that function instead of recreating code again.
E.g.
functions.php
<?php
function your_function_name( $page_id ) {
if ( ! $page_id ) {
return;
}
$content = '<div class="col-md-4">
<div class="ih-item square effect6 from_top_and_bottom">
<a href="' . get_the_permalink( $page_id ) . '">
<div class="img">
<img src="' . the_field('photo', $page_id) . '" />
</div>
<div class="info">
<h4>' . get_the_title( $page_id ) . '</h4>
</div>
</a>
</div>
</div>';
return $content
}
?>
archive.php
<?php echo your_function_name( 266 ); ?>

Comment object empty in WP_Comment_Query

Here's my PHP code:
<?php
$comments_query = new WP_Comment_Query();
$comments = $comments_query->query( array( 'number' => 5 ) );
if ($comments)
{
foreach ( $comments as $comment ) {
?>
<div class="comment" id="comment-<?php $comment->comment_ID ?>">
<div class="avatar">
<?php get_avatar( $comment->comment_author_email, 48 ); ?>
</div>
<div class="text>
<a class="author" href="<?php get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; ?>">
<?php get_comment_author( $comment->comment_ID ) ?>
</a>
<?php strip_tags( substr( apply_filters( 'get_comment_text', $comment->comment_content ), 0, 80 ) ) . '...'; ?></p>
</div>
</div>
<?php
}
} else {
echo 'none.';
}
?>
Here is the resulting HTML of one comment:
<div class="comment" id="comment-">
<div class="avatar">
</div>
<div class="text>
<a class=" author"="" href="">
<p></p>
</div>
</div>
I have comments so the 'none.' is not printed. However, as you can see, the comment object is empty. What am I missing/what's going on?
I'm running WP 4.2.2.
Thanks,
Joshua
got it, something stupid, but even though I was calling the methods, I wasn't echoing the results.

Categories