How can I display certain image in a post if that post contains a specific custom value.
For example, I have two posts, one has a custom field named top_post and its value is true. The other one doesn't have that value at all. I want the one with this value set to true to have image displayed (an image that shows it's a top post) and the one without the value to show nothing.
I know that the function would be something like "if top_post value is 'true' then display img(url)" but I don't know how to write that function as I'm just a PHP beginner.
Any help appreciated.
EDIT: I tried finding the function code on various coding blogs but didn't succeed... Also, I have no function coding experiences and that's why I was asking for a function code.
To expand on Atiaxi's answer, and perhaps make it a bit more readable for you as a beginner in php:
if ($top_post == true)
{
echo "<img src='top_post_image.png'>";
} else {
echo "<img src='blank.png'>"; //you could also leave this line out if you want 'nothing'
}
You're on the right track, I think what you need to keep in mind is that you're using PHP to generate the HTML on the page. So if you want an image to show up if a certain condition is true, you only generate the HTML for that image if the condition is true:
if(top_post)
echo "<img src='top_post_image.png'>"
Related
i have over one hundred html files that all roughly have the same structure. the html files represent each page of a comic.
naturally, i've realized that it would be more efficient to apply the structure of these pages to one file, and allow that file to dynamically create all of the comic pages.
my goals are so that, one; when a user accesses a comic page, the page number that the link contained can be passed through as a number value to determine the image file and the url.
two; when a user clicks "next" or "previous", the number value that defines the page is either added to or subtracted by one to load in the new url & image file.
and three; for the "next" and "previous" buttons to define themselves/decide whether to appear or not based on a filecheck i've already written.
<?php
if ($_GET["img"]) {
$imgnum = $_GET["img"];
} else { $imgnum = 1; }
?>
<html>
<div class="block">
<?php echo '<img class="page" src="' . $imgnum . '.png">'; ?>
</div>
</html>
this code does the trick for changing the image file and url based on the value of $imgnum but when it comes to passing values into the "next" and "prev" buttons, i have no idea what the hell im doing. i tried doing THIS:
<?php $prevValue = $imgnum - 1?>
<h2 class="arrow">Previous</h2>
clearly, passing a variable into that key does not work. is there any way to preserve the dynamism of the buttons with this php method? these buttons need variables to work on their own. do i have to switch to jquery? i honestly above all else need advice on where to start. i barely understand what's going on in this php script itself.
<?php
if ($_GET["img"]) {
$imgnum = $_GET["img"];
} else { $imgnum = 1; }
?>
What the above does.....
If (the URL string contains the [img] value ) {
Get the value of [?img=] from the URL string...
i.e. index.php?img=4 .. (it is getting the 4)
and set the [$imgnum] variable to that value
} else {
But if the URL does NOT contain the [img=] value,
then set the variable value to 1 [$imgnum = 1;]
}
It's difficult to tell exactly what you want, but the below will add the value properly to the button.
<?php $preValue = $imgnum - 1; ?>
<h2 class="arrow">Previous</h2>
PHP lines end with a semicolon... and you have to echo variables for them to be output to the HTML.
I have no clue what jQuery and javascript have to do with your question. But yes.. this could all be handled by an ajax request and you wouldn't need to reload the page every time a button is clicked.
However, if you don't understand what the PHP is already doing, explaining how to do this via AJAX will be exceptionally confusing to you. And much more of your code structure would need to be seen.
You're creating the right code but you just forgot to echo it.
You need to change your line to;
<h2 class="arrow">Previous</h2>
or you can use the shorter version if your php.ini configuration allows to;
<h2 class="arrow">Previous</h2>
Note:
You should be aware of SQL injection and secure your get variables by;
$imgnum = (int)$_GET["img"];
As you are taking the $_GET value as a variable, this time, only integer values will work so hackers cannot inject a code.
i'm trying to highlight current value in this loop menu:
<?php
for ($i=count($anos)-1;$i>=0;$i--)
echo '<li>'.$anos[$i]['ano'].'</li>';
?>
I'm trying to fix some problems in a web page but my knowledge in programming is rudimentary at best. I've managed to fix a lot of issues by googling, but i'm having trouble getting a solution to this.
This loop gets the years (anos) from the DB and displays all of them in a menu, clicking on a year brings a page with the image gallery.
This is the page: http://marialealdacosta.com/obra.php
Thanks for the help.
You can use $_GET to use the passed value along with URL. In your case it's ano.
Use this value to compare with loop's value and if they are similar change the style of the element.
So your code look something like this,
<?php
$ano="";
if(array_key_exists("ano",$_GET)) {
/*
Check if ano value is passed along with URL
before accessing it. Other wise it will give error.
*/
$ano=$_GET["ano"];
}
for ($i=count($anos)-1;$i>=0;$i--) {
$styles="";
if($anos[$i]['ano']==$ano) { // this will check the current ano.
$styles="background:red;"; // change appropriate styles here...
}
echo '<li style="'.$styles.'">'.$anos[$i]['ano'].'</li>';
}
$_GET is an associative reserved array of variables passed to the
current script via the URL parameters.
folks.
I'm starting with CakePHP and after reviewing this tutorial (
http://book.cakephp.org/view/1543/Simple-Acl-controlled-Application
) and also after having used the "cake bake" command to generate my
models,
controllers and views , everything is fine, but when I visit the
Post's add view (views \ posts
\ add.php), I find that instead of showing a input text for the
username, it shows a select with all the usernames.
this is the line in the Post's add view that show the select.
echo $this->Form->input('user_id');
PostsController :
function add() {
// Some other code
$users = $this->Post->User->find('list');
$this->set(compact('users'));
}
Although I know how to display only the username of the currently
logged-in user, I don't know how to control the content to show in
$this->Form->input() because if I use a variable that is not part of
the "Post" model , it's shown , but as the label for the input.
Have you any idea how to solve this??
P.S. I've been trying to find this information on the cookbook , but I haven't been able to find anything specific to my situation :(
If you want to add a string in as the value of the input, try this :
echo $form->input('my label text',array('value'=>'the value string','type'=>'hidden'));
That would make the input hidden, and set the value.
Or remove the type=>hidden and it will have a value, you can also add things like disabled and such in there, or to modify the div it creates, use div'=>array() and put your div options in that array... hope this helps.
i would like to help you on this thing.
first of all when you want a text box then you must define the type of the input field.
echo $form->input('user_id', array('type'=>'text', 'label'=false));
i hope this will help you.
This is the page, its a wordpress powered site:
http://bit.ly/9oJXWV
You select some value, it makes POST to same page and based on value you selected it makes a list pages.
Now before you jump into my code i just want to say that im a newbie and that my main problem here were database queries so i didnt focus on other small stuff(like bunch if's at start inline css and stuff like that).
So this is my template:
http://pastebin.com/HQvMq3Db
This is a function from functions.php which im using in template:
http://pastebin.com/fWKqqzQv
This page works the way i want it and i just finnished putting all the code together but have one issue. Once i get that sorted out i will make the code a lot nicer... :)
So the issue is that if you look at pages that are listed once you make a selection and submit, on a lot of them some values are missing even thought those values are there(open any page from that list which is missing some value and you can pretty much see the same stuff but now it display's all the data).
So that is the part i need help with debugging. I really have no idea how to tackle this.
Second part of this question is simple: how do i paginate this page? Any link, tip, tutorial would be good.
Also one more thing, how can i have links for example like this:
.../hostels/?grad=Beograd
and when user opens up that page he does not have to click to select the town, it would already list all pages from "Beograd"? I guess that is GET request right? Can i do something like that with POST? O_o Not sure what to do here, like i said im newbie.
Thanks for reading, looking forward to answers and comments.
Cheers!
You can set up your functions to enable pagination in WP without have to do any custom logic.
See: http://codex.wordpress.org/Template_Tags/query_posts#Pagination_Parameters
and when user opens up that page he does not have to click to select the town, it would already list all pages from "Beograd"? I guess that is GET request right? Can i do something like that with POST?
yes. yes. no.
GET requests retrieve the variables from the url. so you just run a link with GET variables, the php would suscessfuly display your info. but if you are using POST, the variables are retrieved from "the background", passed by the previous page. so you cannot just run a link, the page must be called from a previous page (trough a form) or the page won't have access to the variables.
1) I fixed pagination simply by implementing &paged='.get_query_var('paged') to my query. Now it looks like this:
$hostels = new WP_Query('post_type=page&meta_key=Grad&meta_value='.$grad.'&posts_per_page=60&orderby=title&order=ASC&paged='.get_query_var('paged'));
#js1568 i gave him +1 for his answer, but he didnt answer my entire question.
Now i can go through pages like so:
/acommodation/hostels/?city=beograd - this is page 1
/acommodation/hostels/page/2/?city=beograd - this is page 2
/acommodation/hostels/page/3/?city=beograd - this is page 3
etc...
2) The issue with missing info from some pages is fixed by putting this below the end of inner loop:
wp_reset_query();
and also i created some custom function which will get all meta values for given post id:
function custom_get_meta_values($id){
$first_array = get_post_custom_keys($id);
foreach ($first_array as $key => $value) :
$second_array[$value] = get_post_meta($id, $value, FALSE);
foreach($second_array as $second_key => $second_value) :
$result[$second_key] = $second_value[0];
endforeach;
endforeach;
return $result;
}
In my inner loop i call that function like this:
$result = custom_get_meta_values($post->ID);
Then i just echo what i need like so:
echo $result['Mail'];
Just put the name of meta field in that $result array and echo it.
3) I replaced POST with GET request so now i can have links like this:
/acommodation/hostels/?city=beograd
which when opened will show every hostel from 'beograd'. I only have 4 possible values for cities so if value of 'city' that i capture from GET request is not one of those 4 values, i do nothing, just show that form. If it is i take that value and show the list from that city.
As per Will instructions, i will mark this answer as accepted.
I have a form that posts to a page. I want to display an image if the value of the item posted equals "paypal".
I need to write something that says;
if $_POST['method'] equals "paypal" then show paypal.gif
if $_POST['method'] equals "mastercard" then show mastercard.gif
I hope I made a bit of sense, new to php trying to learn the best I can
If the methods have the same values as the file names, you can use this:
<img src="<?=$_POST['method']?>.gif" />