If get field with multiple values - php

I have an if statement involving a get field. However I would like it have an option of 2 different values.
This is what I currently have
<?php
if(get_field('more_links')):
?>
I would it like go if have more_links or exp_link or both do this. I know the below wont work but just to help show what I mean.
<?php
if(get_field('more_links, exp_link')):
?>

by default you need to call this function more than just once, if you want more than you param. You can call this like if(get_field('exp_link') && get_field('more_links')):, or you can create a wrapper that receives an array and call get_field foreach value on the parameter

Related

How to add a query argument when clicking a link, and then retrieve it in Hook callback

I am trying to create an archive page that shows only posts of a custom type made by a specific user. Without overriding the the Author archive page of said user, nor the posts archive page. Basically I wanna leave the default wordpress archives intact.
so far I fugured I would do something like
<a href="<?php echo add_query_arg('type', get_post_type(), get_author_posts_url(get_the_author_meta("ID")));?>">
posted by <?php the_author() ?>
</a>
this basically generates a link that goes like
www.example.com/author/john/?type=custom_type
now I am trying to retrieve the query argument that I added in the pre_get_posts hook callback function that I use in my functions.php file like so:
add_action("pre_get_posts",function($query){
///code to retrieve parameters goes here
}
the problem is that the only parameter I am getting in the $query paramter is the author username. As for the post type (aka the string that comes after "?type="). That part of the url does not show up in the query vars. I tried inspecting every property of the $query parameter that is being passed to the callback but I cant find that post type. I can only find the author.
Anyone know what I am doing wrong or how to fix it?
OK I realized what I was doing wrong
You cannot use a made up key name when creating a key value pair in query vars. In other words if you want to assign a value to a queryvar, the key of the var has to be one of the wordpress query var predefined names. The full list can be found here https://codex.wordpress.org/WordPress_Query_Vars
When using a made up name like "type" in my case, you can not retrieve that value using the get_query_var method because the key value pair is not added to the query vars in the first place. Instead you have to manually retrieve it from the super global variable $_GET[<variable_name>] so in my case I had to look in $_GET["type"]

Multiple URL parameters?

I am using Dreamweaver and would like to add multiple parameters to my my link. Right now I have one parameter which works fine, but I would like to add multiple parameters to the same link, how do I do that?
The code to the link is as follows:
<a href="Patient.php?Patient=<?php echo
$row_Patienter['Patient']; ?>">Visa</a>
This does links to another page's Recordset.
My question is:
How do I add multiple parameters to the same link? How would it look like?
You can separate parameters by adding a & at the end of each parameter.
Something like:
http://www.domain.com?parameter1=value1&parameter2=value2
But in your case I would recommend to do it like this:
<?php
echo "<a href='patien.php?parameter1=".$value1."&parameter2=".$value2."'>Link</a>";
?>
You add extra parameters by starting with ? and adding & each time you start a new parameter. Remember in HTML to use &
Visa
<a href="bar.php?foo=<php echo $foo; ?>&bar=<?php echo $bar; ?>">
or whatever the values are. Better use echo, not sure what you wrote works

filter an array using ajax in php

I read similar questions here, but didn't find my answer. Actually i have an array of data which i am passing to my view file from my controller. There i am iterating through it to show data in the table like below:
AbcController(abcAction) :
$this->view->array = $array;
and it's corresponding view file : abc.phtml :
<?php foreach($this->array as $object){ ?> *** table to display data*** <?php }?>
I am able to sort through postback but i want to sort this data through ajax call. i had done sorting logic in my controller. Ajax call should return this sorted array which i will show in table as i am showing above in phtml file. What i had learned in ajax, i am not able to implement it. Can anyone help me out in this.
Thanks
json_decode and json_encode. Is what I believe you are asking for.

How to paginate wordpress page + need help debuging + need help with POST/GET requests

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.

Value of variable changes once out of the click function in jQuery

this is the continuation of my previous question. Since I just logged in that day without an Open Id, I don't know how to login to that account and edit further. So I created a new Open Id enabled account and posted this as a new question.
Now I have the code like this. Inside the onclick event, the value is stored in the $selectedId correctly. But When I try to pass that value in the url, I do not get the correct value. The last value in the for loop is passed.
<script type="text/javascript">
$(document).ready(function(){
<?php foreach ($Forms as $r): ?>
$("<li><a id='<?=$r['Form']['id'];?>' data-attr='Formentries' href='#'><?=$r['Form']['name']?></a></li>").appendTo("#headers").click(function () {
<?php $selectedFormId=$r['Form']['id'];?>
alert("selId: "+<?php echo $selectedFormId;?>); //here the selected id is alerted
});
alert("outside the loop"+<?php echo $selectedFormId;?>); //here the last value in the loop is alerted
});
Once out of the click function, the value of $selectedFormId changes to the last value in the array. Can someone help me with this?
Actually what I am trying to achieve is, I list a set of Forms as links, and when I select the links I want its id to be saved in a php variable. I want it particularly be saved in a php variable coz after I select a Form I have an option to export the entries in the form through another link
<a href="localhost/FormBuilder/reports/export/<?php echo $selectedFormId;?>" class="thickbox button" title= "Export" >Export</a> .
So I want the id in there,so that I could pass it to the export function in the controller.
I also get the selected id in a javascript variable as
formid=$(this).attr("id");
but I do not know how to pass this value to the export function in the controller.
I don't know if i've understood your question very well but the $selectedFormId value is set inside the loop, so everytime the loop is executed the variable is set and when the loop finishes $selectedFormId gets the last processed value. I think you should set it outside the loop.
as you are declaring it inside the block so it is not available outside;
either you declare it before the $(document).ready block or finish its usage it inside this block itself.
As far as I can see you are setting $selectedFormId inside each loop in the iteration, so the assignment code will only be set when the click event is fired.
Therefore, logically, when you exit the loop, $selectedFormId will be the last item in the array. Are you meaning to put a condition around the assignment? Don't forget, the server side code will execute regardless! It won't care about client side conditions or closures!
if (something){
<?php $selectedFormId=$r['Form']['id'];?>
}
And, again echoing the above comments, you should really be trying to achieve code separation. The above really is tag soup!
You are mixing client and server-side code -- the code you wrote seems like you're expecting a JavaScript function on the client-side to magically set a PHP variable on the server-side, when in reality that type of operation is not possible.
What you should maybe do is have your click event set a variable on the client-side. Then set an onclick on your export link to construct a URL and redirect to it based on the locally stored variable value.
That's probably not the best solution, but it would be one option.

Categories