php if statement not evaluating properly - php

This is my first time posting a question here, it has been a valuable resource before so I thought maybe someone would be able to answer my question.
It might be a simple solution, but as I said already, I'm a PHP noob. Forgive me.
Here is my code:
<?php wp_reset_postdata(); // reset the query ?>
<?php $authorposts = get_the_author_posts();
if ($authorposts < 1) {
echo " ";
}
else { ?>
<div class="single-sidebar"><!--expert_blog start-->
<div class="single-sidebar-middle">
<div class="single-sidebar-top">
<h3>More Posts by <?php the_author();?></h3>
</div>
<p><?php echo get_related_author_posts(); ?></p>
<div class="single-sidebar-bottom">
<div class="more_blog">+ Read more</div>
</div>
</div>
</div>
<div class="clear"></div>
<?php wp_reset_postdata(); // reset the query ?>
What this is supposed to do: get the number of posts by a particular author (of the post currently being viewed), and output it into a conditional statement saying if it is 1 or less than 1, display nothing. Otherwise, display the titles of each post.
What happens: The divs show up even when there is only one post attirbuted to the author, but nothing shows up in the list (im guessing because there is only one post and you are already viewing it).
Any help would be appreciated, it is late and my brain has melted. Please let me know if anything needs clarification.
Thanks in advance.

Instead of if ($authorposts < 1) it should be if ($authorposts <= 1)

Related

How to use one class in jquery to php/html foreach loop?

My title is quite confusing but my question is..
is have this script plugin wherein the plugin will create a bubble-like progress bar found here.
in the script I have $('.demo').waterbubble();
now have a foreach loop that is..
<?php foreach($bins as $binArray): ?>
<div class="row">
<?php foreach ($binArray as $bin):?>
<div class="col-xs-8">
<canvas class="demo"></canvas>
</div>
<?php endforeach;?>
</div>
<?php endforeach; ?>
so in this code it should iterate the canvas and display bubbles depending on the $bin variable. I tested with 6 iterations so it should show 6 bubbles, but instead it only shows one bubble and the others are not showing any bubbles. I inspect the code and the <canvas class="demo"></canvas> is iterated properly but not showing anything for some reason. Any one knows why is that? and is there any way to use this or to work around this? thank you very much!
I'm guessing it's an issue with the script. You might be better off trying to init each waterbubble with a unique ID.
<?php foreach($bins as $binArray): ?>
<div class="row">
<?php foreach ($binArray as $bin):?>
<div class="col-xs-8">
<canvas id="demo<?= $bin[number] ?>"></canvas>
</div>
<script>
$('#demo<?= $bin[number] ?>').waterbubble();
</script>
<?php endforeach;?>
</div>
<?php endforeach; ?>

PHP give a different class if there is more than an amount of blocks

I've been working on a website and it has 5 blocks. When the screen gets smaller I need to get 2 of the blocks under the other 3 and they get a different with so i gave them another class.
Im trying to get it into a CMS and i have to use this code down below.
Now is my question how do i make this happen with if i want the user to be able to edit the blocks. I've tried to make some edits in the script but so far no succes. I need to add a different class to block nr 4 and 5. What is a good way to do this?
<div class="colums">
<?php
$i=1;
//krsort($properties['blocks']);
foreach($properties['blocks'] as $block)
{
echo '<div class="colum1" id="pic'.$i.'" style="background-image:url(/uploaded/overige/'.$block['image2'].');">
<div class="content_colum">
<div class="wilpe_logo">
<img src="/uploaded/overige/'.$block['image'].'" alt="" />
</div>
<div class="text_holder">
'.$block['text'].'
<div class="button_two">
<a class="button_text" href="'.$cms->make_link($block['link']).'">'.$block['linktext'].'</a>
</div>
</div>
</div>
</div>';
$i++;
}
?>
</div>
Colum1 needs to be colum2 if there is 3 colum1's
I've found the answer in another question. I made the question smaller and more understandable and this is the answer that came out:
$i = 1;
foreach($properties['blocks'] as $block)
{
echo '<div class="' . ($i < 4 ? 'colum1' : 'colum2') . '" id="pic'.$i.'" style="background-image:url(/uploaded/overige/'.$block['image2'].');">
// --------------
Stackoverflow answer
Posted it here to share with everyone.

How can I display the value of a field in WordPress?PHP

I have this site:
link
I installed the plugin Types and tried this code to display the value field in a div.
<div class="selectat">
<div>
<?php $variable = do_shortcode("[types field='descriere' ]");?>
<?php echo $variable;?>
</div>
</div>
My field is called descriere..I put a picture to see more clearly.
Unfortunately this blank although I do not see anything on the site and basically my div is empty ..
It is wrong the way I wrote the code?
I must call another function?
You can help me solve this problem please?
Thanks in advance!
You dont need to use any shortcode you could use
<div class="selectat">
<div>
<?php $variable = the_field( 'descriere' );
<?php echo $variable;?>
</div>
</div>
But if you want to echo the code straight away you can just use get_field('') which echos the value straight to the page.
<div class="selectat">
<div>
<?php $variable = get_field( 'descriere' );
</div>
</div>

PHP isset not showing HTML with ID is set

I have the below section of code and I want to only show the html IF the ID is set in the url. It isn't working and I am not sure why! The same code works on other areas of the page just not here! I am editing some code that someone else built for me, I am a php noob so please bear with me!
Thanks for any help!!
<?php if(!isset($_GET['id'])) { ?>
<div class="control-group"> Show this text </div>
<? } ?>
You have your conditional the wrong way round.
<?php if(isset($_GET['id'])) { ?>
<div class="control-group"> Show this text </div>
<?php } ?>
You have to remove the not (!), obviously. As the logic you want is isset and not not isset.
Try this:
<?php if(isset($_GET['id'])) {
echo "<div class='control-group'> Show this text </div>";
}; ?>
Depending if you want to show the code or not place a ! in front of isset.

Dynamic PHP variables for jQuery use

I'd like to apply jQuery to classes that are generated dynamically as they are looped through PHP/MySQL.
I've got a non-dynamic working version on JSfiddle: jsfiddle.net/TXJA5/1/
In the example, cat_fruit, cat_vegetable and cat_cereal would be echoed within a PHP loop; something like:
<div class="category cat_<?php echo $category; ?>">
<p><?php echo $category_label; ?></p>
</div>
Then, in following columns, there will be divs containing items associated to the category:
<div class="column">
<div class="item cat_<?php echo $category; ?>">
<p><?php echo $item[0]; ?></p>
<p><?php echo $item[0]; ?></p>
</div>
</div>
So, why? Well, while the data is variable and "categories" exist in one column and "items" exist within other columns in another div, I need the associated divs to be identified by jQuery to make them the same height (using equalizeCols — see the fiddle; you'll get it.)
n.b. There's a reason this isn't a table, despite the obvious column/row relationships. I considered it (fretfully) and tried it, but it won't work for the needs of the actual project.
Thanks in advance for any help you can offer. This is my first question on SO after years of learning from it — this community is awesome. I've found a couple questions on here that may make mine look like a duplicate, but they couldn't quite help get me there!
<div class="column">
<div class="category" data-cat="<?php echo $category; ?>">
<p><?php echo $item[0]; ?></p>
<p><?php echo $item[0]; ?></p>
</div>
</div>
I guess you can add a data atribute (or an id) in the first column and then loop through those with jQuery:
$('.category').each(function(){
var category = $(this).attr('data-cat');
$(".cat_"+category).equalizeCols();
});
http://jsfiddle.net/TXJA5/2/
You can loop categories in the same way you do in the HTML part of the code:
<?php for(...){ ?>
var $els = $(".cat_<?php echo $category?>").equalizeCols();
<?php } ?>
If I understand your question and issue OK this is the solution. If this is not your issue please be more specific.

Categories