PHP If & Else Statement in Wordpress - php

I'm trying to figure out how to do a if else statement in Wordpress. I want it to state if $data[0]['media_upload'] is empty then show the blog title. Else (if there is something there) show the logo in Wordpress. I'm wondering if I have the wrong syntax.
Right now I have the following which is showing the blog title and no errors but I do have a image uploaded and its not showing for some reason. Any ideas would be great!
<?php
if (empty($data[0]['media_upload'])) {
echo'<h1 class="site-title"><a href="';
esc_url( home_url( '/' ) );
echo'" title="';
esc_attr( get_bloginfo( 'name', 'display' ) );
echo'" rel="home">';
bloginfo( 'name' );
echo'</a></h1>';
}
else{
echo '<a href="';
bloginfo('siteurl');
echo '">';
echo'<img src="';
global $data;
$data['media_upload'];
echo'" /></a>';
}
?>

Try changing:
$data['media_upload'];
in your else to: $data[0]['media_upload'];
like you have in your if statement (or the other way around). We don't really know what $data is so it's hard to help you.
Edit: Since I can't comment yet, I'll comment here.
Try using the rubber duck technique; explain to us what is happening with the $data object in your code. Maybe you'll figure it out yourself.

it should be
echo'<img src="';
global $data;
$data[0]['media_upload'];
^ // you forgot 0th index
echo'" /></a>';

Related

php variable inside href inside if statement

I am trying to make a thumbnail image clickable so that when clicked, it shows the full size image, but I keep getting a PHP error and I'm not sure why. This is a Wordpress site. I suspect it has something to do with the variable that's inside the URL, but I need some guidance. I've searched stack for almost 8 hours now...Here is my code:
if ( has_post_thumbnail() ) {
echo '<a href="'get_the_post_thumbnail_url($post_id, 'full');'">';
the_post_thumbnail('cb-thumb-600-crop');
echo '</a>';
} else {
I think what's wrong on your code is this:
echo '<a href="'get_the_post_thumbnail_url($post_id, 'full');'">';
should be:
echo '<a href="' . get_the_post_thumbnail_url($post_id, 'full') . '">';
Try This:
if ( has_post_thumbnail() ) {
$anything=get_the_post_thumbnail_url($post_id, 'full');
$anythings=the_post_thumbnail('cb-thumb-600-crop');
echo ''.$anythings.'';
} else {
The problem is on this line:
echo '<a href="'get_the_post_thumbnail_url($post_id, 'full');'">';
You forgot the dots between the single quotes near "get_" and ");"
It should look something like this:
echo '<a href="' . get_the_post_thumbnail_url($post_id, 'full') . '">';
Good luck and keep coding! :)

Fixing WordPress Header

I was provided this bit of code to add to my functions.php file by the support team of the company from whom I bought the theme.
I wanted my logo and site name side by side and they gave me this code:
function igthemes_site_logo() {
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
$url = home_url( '/' );
echo '<a href="' . esc_url( $url ) .'">';
echo "<img class='logo' src= " . $image[0] . " style='float:left; padding:15px;'>";
echo '<div style="float:left; padding:15px 0;">' . get_bloginfo( 'title' ) . '</div>';
echo '</a>';
}
However, that made it look like this.Two logos and titles
What do I change to just show one logo and one title?
it seems like that php function (igthemes_site_logo()) is called twice. That would be in the PHP code, not in the code you posted which only contains the function itself.

How to display array url values from custom field ( file upload )?

I'm am currently working on an Album Gallery and I am using this CMB2 plugin
https://github.com/WebDevStudios/CMB2
to create my custom fields. What I am using right now is the file_list field which allows me to upload multiple/bulk images.
But the problem is that I am not sure how to display each item. I would like to get the url of each item so I can use each as an image source.
What I am trying to achive is something like:
<li><img src="<?php echo $file_list; ?>" alt="" /></li>
Meaning, each item would be wrap in 'li img' and file link would be added as a src.
Your help would be much appreciated.
I hope this helps...
<?php $meta_values = get_post_meta(get_the_ID(), '_yourprefix_demo_file_list', true);
foreach($meta_values as $meta_value) {
echo '<li><img src="'. $meta_value . '"/></li>';
}
?>
actually I've made it worked with this:
<?php
$count=0;
if (is_array($album_files))
{
foreach ($album_files as $files)
{
$count++;
if (1 == $count) {
echo
'<figure>',
'<img ','src="', $files , '" ','>',
'</figure>';
}
}
}
?>
Hopefully this can help others as well.

How to echo Wordpress default thumbnail via php inside php if else

Hey guys i have this code
<?php $jthumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ($jthumb> "0")
{
echo $jthumb;
}
else
{
echo "http://placehold.it/350x250&text= Jedcore";
}
?>);background-attachment:fixed;">
What i wanted to do is to replace the
http://placehold.it/350x250&text= Jedcore with an image inside my wordpress theme like
<?php bloginfo('template_url'); ?>/images/theimage.png
But i just cant replace it liek that i will prompt an error,
I'm no expert with php :)
try this:
echo '<img src="'. bloginfo('template_url') . '/images/img.jpg" alt="">';
try after this
change
echo "http://placehold.it/350x250&text= Jedcore";
to
echo get_bloginfo('template_url').'images/theimage.png';

PHP Mixing html and code

(Preamble: Am new to PHP, coming from a C# background where I am used to very clean code. Am currently working on my own Wordpress site which has a purchased theme.)
I have seen this type of code in a WordPress theme:
<img src="<?php echo esc_url( $logo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" id="logo"/>
I find this very hard to read compared to the refactored:
<?php
echo '<a href="';
echo esc_url( home_url( '/' ) );
echo "><img src=";
echo esc_url( $logo );
echo " alt=";
echo esc_attr( get_bloginfo( 'name' ) );
echo '" id="logo"/></a>'
?>
But this is the easiest by far:
<?php
get_anchor($url, $imgsource, $alt, $id);
?>
get_anchor being a custom function that echos an anchor configured according to the parameters.
But surely I am not the first to think of this. Are there any existing libs that have a set of functions that return properly formatted html like in this example? Is there something I am missing?
I've written a function that returns a HTML tag based on the pure PHP output:
function tag($name, $attrs, $content) {
$res = '';
$res .= '<' . $name;
foreach($attrs as $key => $val)
$res .= ' ' . $key . '="' . $val . '"';
$res .= isset($content) ? '>' . $content . '</'.$name.'>' : ' />';
return $res;
}
$name is the tagname (e.g. a)
$attrs is a key, value array with attributes (e.g. array('href','http://google.com/'))
$content is the content / body of the tag (an other element or text)
Example basic use:
echo tag('a', array('href' => 'http://google.com/'),'Google');
Example nested use with multiple children:
echo tag('ul',array(),
tag('li',array(),'one') .
tag('li',array(),'two') .
tag('li',array(),'three')
);
I believe what you are looking for are templates like Smarty. They are the cleanest way to display information as code and view are completely separated.
However Wordpress do not use them, I don't know why actually, probably because most PHP programmers are not used to it.
Most of the PHP frameworks provide such libraries to out put html through parameterized functions, most of them are part of view layer if the framework follows MVC pattern.
but if you are not using any of the framework then you may use these libraries from here
PHP Pear Packages
And for building forms in particular see
HTML_QuickForm2

Categories