retrieve query outside the wordpress loop with ajax.load in #content - php

I am working on an advanced search on wordpress based on custom taxonomies.
I'm stuck since 48h so I was hoping to have some help or thought...
Step 1 --- in the js file the query strings are created like that:
if (jQuery('#s').val() == ''){
URL = "/?genre=" + genre + '...other Stuff' #content';
}else{
URL = "/?s="+searchQueryString+"&genre=" + genre +'...other stuff' #content';
}
It nicelly load my custom loop in my #content div without changing the browser url or reloading the header, which is pretty good...so far. :-)
Step 2 --- then I wrote 2 functions in my function.php , one to load the loop with the GET[] elements on main page, using new WP_Query
and one that does the same thing for search queries:
add_action('pre_get_posts','SearchFilter');
Which is compiling my GET[] filters with the GET[s] in the content.php,
Still all good....
Step 3 --- (problem^^)---
I want to add a css class to desactivate the radio buttons located in my header.php, depending on the results in the loop.
Try-1 I thought I could create a php array to compile the terms found while the loop is happening, and then compare it with my buttons value.like that:
$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slugs');
$results = wp_get_post_terms(get_the_ID(),'category',$args);
foreach ($results as $result){
array_push($stack, $result);
}
But there is no way to retrieve the data from that array in the header afterwhile, or to create it from the header using things like global $post;since my url doesn't change.
it just shows the homepage query.
Try-2 I also thought I could encode it to json and then put some action in my js file. but so far it just return json unexpected character, and I got the feeling that even if I crack it, its not going to be the right way since its going to make the js file heavier.
May be I'm just missing something about the Global wp_query and I don't need to charge my script?
Excuse my english and the long question,
thanks a lot in advance if you have an idea,
DACO

I'm going to use wp_localize_script to export the array to my js script,
Thanks sorted

Related

why is WordPress printing my array instead of just constructing it?

I am trying to apply a custom view to all the results of a visit to a taxonomy page, which involves wrapping the whole lot of results in some DOM boilerplate and then invoking the function to display each result as an element within that boilerplate. My strategy of choice was to step through each result, populated dynamically depending which taxonomy term has led them here, and fill a bucket with those results, then display each one appropriately.
In other words, I want the WordPress loop to tell me WHICH things to display, then my own iteration to decide HOW to display them. This seemed like a simple strategy, but loops like the following appear to be always displaying my entire bucket, although I am nowhere telling it to actually print that bucket. Narrowing it down to a minimal example I have the following, which still is printing out $bucket. What is going on?
$bucket = array();
while ( have_posts() ) {
the_post();
$bucket[]=the_meta(); // this is all printed to the screen. Why?
}
the_meta(); // this is all printed to the screen. Why?
Wordpress has this concept of the_{name of function} and get_the_{name of function}. For example, the_title and get_the_title. the_title will echo out the title into the page but get_the_title will return the title instead.
the_{name of function} will echo out the result.
get_the_{name of function}, on the other hand, will return the result.
Read more on that
If you need to return the result instead of echoing it out into the page then use this version of the function: get_the_{name of function}.
So you could use the following code:
$bucket = array();
while ( have_posts() ) {
the_post();
$bucket[]=get_post_meta(get_the_ID());
}

will do_shortcode() allow me to run shortcode in content before a line in the template?

G'day folks, I'm attempting to integrate Ajax Availability Calendar into a WordPress site via PHP.
I have multiple calendars set up, and need to display a different one on each property page.
The problem I'm having is that I need to be able to specify the ID e.g. $_GET["id_item"]=2 of the calendar before the <?php require_once 'pathtofile' ?> statement which is in the template.
The result I need to achieve is...
<?php $_GET["id_item"]=2; require_once 'path\to\required.file'; ?>
If I put this code in the template with the appropriate ID, it works, but I need the ID number to change on each page, otherwise I'll always be displaying the calendar with ID number 2.
I currently have custom shortcode (below) with which I can specify the ID in the page content, but I am at a loss as to how to access that ID before the statement in the template.
So what I currently have is...
In Functions.php
function ajaxcalendar_shortcode($atts) {
$args = shortcode_atts(
array(
'id_item' => 1
),
$atts
);
return '<?php $_GET["id_item"]='.$id_item.'; ?>';
}
add_shortcode('cal_display', 'ajaxcalendar_shortcode');
with the shortcode [cal_display id_item = "2"] in the content.
Is it even possible to do this or should I try another approach?
My research suggests that I may be able to use do_shortcode() to achieve this, but I haven't figured out how to use it in this situation, or I may be misinterpreting it's purpose.
I've tried putting this in the page template file:
<?php $content = the_content(); echo do_shortcode($the_content); ?>
<?php require_once 'C:\xampp\htdocs\MooreRiver\availability\ac-includes\cal.inc.php'; ?>
But it continues to display the calendar with ID 1.
Any tips would be appreciated :)

Drupal: back-link in custom breadcrumb

On my Drupal7 have a views (results) width a exposed filter for list the nodes.
When click on a node then display a breadcrumb
ex.
Home >> results >> node-title
Thats good!
But i will make the breadcrumb 'results' a backlink.
When input ex. t then the url is:
http://www.site.com/results?title=t
I try the above url as variabele in the 'results' breadcrumb.
I hope you understand this.
Is this possible with a php snippet in custom breadcrumbs?
Yes this is possible :)
The tricky part here is having your code "remember" what the query was from the list of nodes. One option would be to add a $_GET parameter to all of the node links.
For clarity:
If you are on
http://www.site.com/results?title=t
A link to a given node on that list of results would be:
http://www.site.com/node/56?title=t
This can be done in views by modifying the output of the link. Shouldn't be too hard.
Then, to modify the breadcrumbs you need to add a function like this to template.php
function THEME_NAME_breadcrumb($variables) {
$breadcrumb = $variables['breadcrumb'];
// check to ensure this is the one you want to alter
// Custom rebuild process of breadcrumb with custom links.
if ($breadcrumb[1] == 'your_breadcrumb_id') {
// Keeping the trail/current page as non linked
$links[1] = l(t('results'), 'results', array('query' => array('title' => $_GET['title'])));
drupal_set_breadcrumb($links);
}
}
(Check my code for syntax, its untested)
Good luck!

Drupal node 2-page content split

My employer requires certain pages on the website have a two page feature.
What this means is that some default content show up on the node_view page as normal but the second part should show up when a link is clicked.
This will be easy if I could do this across multiple nodes but the requirement is for all the data to be stored in one node but displayed across two pages.
I hacked together the following code:
function mymodule_node_view($node, $view_mode, $langcode){
$path = current_path();
$path_alias = drupal_lookup_path('alias',$path);
$links = array( 'test' => array('title'=>'my_link', 'query'=>'', 'href'=>"{$path_alias}/nextpage") );
$node->content['my_module'] = array(
'#theme' => 'links__node__mymodule',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
}
That creates a hyperlink called my_link across the top of my content area - which is great.
The problem starts when I click the hyperlink. Supposing I am on http://example.org/homepage and I click the hyperlink, I expect to be redirected to http://example.org/homepage/nextpage. Also, I still want to maintain the view and edit tabs of the actual node I was on. However, Drupal correctly gives me a "page not found" error.
What's interesting is if I used http://example.org/node/1 and visited http://example.org/node/1/nextpage, I don't get the issues I described above but the url is less descriptive.
To solve this problem, I am sure I have to extend hook_menu but I don't know how to account for any number of taxonomy terms leading up to the actual node title. So, I can't predict how many % I will need before the node title and then my /nextpage. However, I want /nextpage to still have the view and edit tabs of it's parent page.
This is all unexplored territory for me.
Update
I found the following function which does a great job of returning the entire node path complete with taxonomies:
$path = current_path();
$path_alias = drupal_lookup_path('alias',$path);
What I don't know is how to take advantage of this in hook_menu to dynamically create /nextpage for my nodes.
Please remember, I don't really want /nextpage to be entirely independent of the original and actual Drupal node. When on /nextpage I want to be able to have access to the view, edit etc tabs of the node.
So, /nextpage effectively is just an extension of a Drupal node page.
There is a quick way to do that. Using views module.
In the fields section choose the field you wanna view. And in the arguments add the nid.
Then add the link to the node view you already created.
The final result http://mysite/views-page/[nid]
Hope this helps... Muhammad.
I would check the node_menu() function to get some reference on how it's implemented.
Not sure on your taxonomy requirements so this might be insufficient but I'll go with what I understand.
But off the top of my head I'd go for something like:
function mymodule_menu() {
$items['node/%node/nextpage'] = array(
'title' => 'Next page',
'type' => MENU_LOCAL_TASK, // Makes it a tab on node/%node-pages
'page callback' => 'mymodule_node_page_view', // Your page display function
'page arguments' => array(1), // First will be a node object, second will be whatever value is passed in the url
// You should rip access callback and access arguments from node_menu()
);
return $items;
}
That should do something like what you are asking for.
It is also possible, easier and definitely recommended to do this with Panels/Pages (see also Chaos Tools) or arguably Views as they are quite capable of all this and generally a better way to work with Drupal's strengths than custom code.
Updated
To clarify I've simplified the menu hook and you should be able to use the below page view function. I still believe you would make a better solution using Panels and overriding node_view and such.
The MENU_LOCAL_TASK part in the menu hook should turn this into another tab along with View and Edit.
function mymodule_node_page_view($node) {
die("It works: ".$node->title);
}
Hope that's more helpful.

Wordpress Custom Post Loop - How to not print information from an Array that I don't need?

Been using the plugin Custom Content Type Manager to create a custom post that displays location information.
We hold weekly games for each location - So what I'm trying to accomplish is in my custom post I have a set of checkboxes so you check if the venue is played on Monday, Tuesday or Wed...etc Then in my theme I'm going to have a 7 day calendar. And if a location is checked for that day then I want the title/links to the location printed there.
I'm giving you that background because I really dont think I'm going about this the correct way. Essentially I'm doing it in a loop, and I'm pulling all the checkbox options in an Array, and if the option is equal to Monday to that specific day, then it prints the locations title name etc.
I want this setup so a non-technical person (kinda like me lol) can just add a new location and pick "friday" for example and the code does the rest.
Essentially I got it working. 2 problems though
I'm running 7 loops to accomplish this - one for each day. I know this is stupid and there is probably a better solution.
It's printing the correct information - however its also reading/printing each of the other locations except its not putting up the info for them - I know this cause its creating empty DIVs for them.
NOTE: I'm having issues posting the whole code...?? I deleted all the php tags to present this
$weekly = new WP_Query( array( 'post_type' => 'locations', 'posts_per_page' => 5 ) );
while ( $weekly->have_posts() ) : $weekly->the_post();
<div class="weekly-venue-spacer">
$day_array = get_custom_field('weekly_day:to_array');
if (in_array('3', $day_array)) {
print_custom_field('venue_display_name');
echo "<br />";
print_custom_field('city_crossroads');
}
</div>
endwhile;
wp_reset_postdata();
the '3' in the in_array statement just means "Wednesday".
Here look at this image:
http://i40.tinypic.com/svnee0.jpg
an example of the empty DIVs being created - easily seen with padding applied to the div
Thanks for reading. Any solution to approach this differently would be great.
I am not sure I understood correctly what you want , but assuming I did - I think your whole approach is a bit wrong/complicated .
First of all, you do not need 7 loops .
I have noticed have a custom field - so in that custom field , instead of an array, just store the ONE day that you need , and then simply GET by checking the custom_field value ..
Second - why do you use checkbook and not a list ? is there an eventual event that can be in several different days ? because if every event is exclusive for one day - than it would be more easy to use a drop list or even radio buttons.
and for your direct question - I do not know how the value of the custom field is structured - but you are printing ALL of it ..
EDIT I : After reading comment and understanding better the problem -
While still thinking that the approach is a bit wrong , but not knowing exactly how you construct the data - I will address the IMMEDIATE problem :
The code creates empty DIVS simply because you tell it to .
you are using a WHILE condition in the code BEFORE outputting a div.
Since your query gets 5 posts - it will create 5 divs (some of which that do not meet the NEXT condition , will be of course empty).
your function now , put in human-words is working like this :
1. Get 5 post.
2. As long as I have posts (for each post), Open a div.
3. If you have Tuesday in array - print something
4. close div
5. if not finished all posts (in our case, 5) - go back to step 2.
It is obvious that the code will print empty div also for empty events..
So to get it right you simply move the opening div tag to BEFORE the WHILE condition .
that is if you do not need to check for existence of events in the query ..
The right way would be to use also the IF statement, just like the regular wordpress loop.
The general mechanism is this :
<?php if ($weekly->have_posts()) : ?>
//now we open a div
<?php while ( $weekly->have_posts() ) : $weekly->the_post();?>
// now we check for other conditions and print them if available.
<?php endwhile; ?>
// now we close the DIV
<?php endif; ?>

Categories