Recursive query to display pages hierarchically (including their content) - wordpress

I try to achieve the following layout with Wordpress 3.2.1:
---> ID 4 (Record 2, 3rd Attempt)
--> ID 3 (Record 2, 2nd Attempt)
-> ID 2 (Record 2, 1st Attempt)
--> ID 5 (Record 1, 2nd Attempt)
-> ID 1 (Record 1, 1st Attempt)
Every line represents a page and on the Backend the page hierarchy looks like that:
-> ID 1
--> ID 5
-> ID 2
--> ID 3
---> ID 4
I'm sorry I prepared a nice illustration but I'm not yet allowed to post images. So, to explain it a bit further: I try to build a list of records and as soon as one of the records gets broken, it will shift to the right and on top of it the newest record attempt appears.
Ok, so first question: How do I have to set up query_posts to recursively get all children? The following code only returns page 3 but not 4:
<?php query_posts(array('post_parent' => 2, 'post_type' => 'page')); ?>
And second question: Does anyone know a better/simpler/sexier (Wordpress) solution for what I try to achieve?

try
$child_pages = get_pages('child_of=2');
you can look it up in the WP Codex here: http://codex.wordpress.org/Function_Reference/get_pages

Related

Loop that is removing duplicate posts isn't posting the right amount of posts

I have two loops, the first is posting the four most recent posts while excluding one category. The second is supposed to post all of the most recent posts except the ones that are in the first loop.
What ends up happening is that the first loop posts the four most recent posts (which is what I'm looking for, so yay!), but the second loop will run through the loop twice, and if a posts is in the first loop, it will post one less post than it should.
For example, when the second loop runs, it finds a post from category 10, and it displays it as it should, then it finds a post from category 2, which is in the first loop, it doesn't post that poss, but then it stops before finding two postable posts.
<div id="new-article-wrapper">
<?php
$new_articles = new WP_Query('cat=-10&posts_per_page=4');
//Array to save post IDs
$ids = array();
if ($new_articles->have_posts()) :
while ($new_articles->have_posts()) : $new_articles->the_post();
get_template_part('partials/loop', 'new_articles_1');
//Save post IDs into array
$ids[]= $post->ID;
endwhile;
else :
endif;
wp_reset_postdata();?>
<?php
$new_articles_all = new WP_Query('posts_per_page=2');
if ($new_articles_all->have_posts()) :
while ($new_articles_all->have_posts()) : $new_articles_all->the_post();
//Check for duplicates
if (!in_array($post->ID, $ids)) {
get_template_part('partials/loop', 'new_articles_2');
}
endwhile;
else :
endif;
wp_reset_postdata();?>
</div><!-- id="new_articles" -->
What I'd want the second loop to do is to go through the loop again when it finds a duplicate post, but without incriminating so no matter how many duplicate posts are found, the loop always outputs two posts.
You are only asking for 2 posts
$new_articles_all = new WP_Query('posts_per_page=2');
If one of those posts is in your ID list then only one is left to be displayed. To be sure you need to ask for at least size of new_articles + 2 .
And if you want to make sure the second loop only displays 2 posts, then have a counter variable and break out of the while loop when it gets to 2.

Mechanize Traverse & Parse

I'm trying to walk through a hitlist of pages found by Mechanize. Upon a search, which is working just fine, I get a hitlist with 10 records per page. A bottom navigation system takes me to the record count > 10. Display 10 per page. So 53 records = 6 "group pages" as I'm calling them.
What I want to do is use the top search results page to do the following:
Grab the html behind every record on this list. I can do that through an iteration.
Follow through the '[Next]' link at the bottom and repeat both 1 and 2 until there is no more 2. Essentially this will get every record.
I'm having an issue with moving off the first page into the second page. I'm grabbing the html behind the first 10 records, but then the system bonks on me. Not sure why. I thought I was iterating through the group pages, but it isn't advancing past the first group page.
counter = 1
puts "Counter: #{counter}"
while agent.page.links_with(:text => '[Next]').count == 1
page = agent.page.link_with(:text => '[Next]').click
puts "Counter/Next: #{counter} / #{agent.page.links_with(:text => '[Next]').count}"
agent.page.links_with(text: '[complete profile]').each do |link|
a = link.click # link.click goes to each company page
r = a.body.to_s
r = r.gsub(/^\s+\n/, "")
# enter company into db
class Company < ActiveRecord::Base
end
company = Company.new
company.cdate = DateTime.now
company.status = 'new'
company.requestID = req_id
company.html = r
company.save
end
counter += 1
end
Any insight appreciated. I know I'm close.
Cheers
Solution: move the click method behind page:
page = agent.click(page.link_with(:text => "[Next]"))

wordpress : identify user way to an item

is it possible to track the route that the user took to get to a page ?
say that i have a post that have 2 different taxonomies
taxonomy 1 = make
taxonomy 2 = color
i want to create a different layout for each route
this is my current hierarchy:
Mercedes -> blue -> car - the user gets template 1 (single-car.php)
blue -> Mercedes -> car - the user gets template 1 (single-car.php)
this is what i want
Mercedes -> blue -> car - the user will get template 1 (single-car_by_make.php)
blue -> Mercedes -> car - the user will get template 2 (single-car_by_color.php)
is that possible without using extra query-strings ?
I am not writing actual code. But I just giving idea how to do it.
use the the following logic in the your single-car.php file
if(taxonomy == make )
get_template_part( 'single-car_by_make' );
if(taxonomy == color)
get_template_part( 'single-car_by_color' );
I hope It will help you.

Parsing images via nokogiri and xpath

I currently have a piece of code which will grab a product title, description, and price and for that it works great. However, I also need it to get the image URL which is where my dilemma is. I tried using a xpath inside the loop I have at the bottom and it lists out ALL the images that are equal to 220 on EVERY product which I dont want at all. So basically I get something like this....
product 1 Title here
product 1 Description here
product 1 price here
http://www.test.com/product1.jpg
http://www.test.com/product2.jpg
http://www.test.com/product3.jpg
http://www.test.com/product4.jpg
product 2 Title here
product 2 Description here
product 2 price here
http://www.test.com/product1.jpg
http://www.test.com/product2.jpg
http://www.test.com/product3.jpg
http://www.test.com/product4.jpg
Where as I obviously want product 1 to just have http://www.test.com/product1.jpg and product 2 to have http://www.test.com/product2.jpg etc, etc. The images are just in a div tag with no class or ID hence why I didnt just easily put them into a css selector. Im really new to ruby/nokogiri so any help would be great.
require 'nokogiri'
require 'open-uri'
url = "http://thewebsitehere"
data = Nokogiri::HTML(open(url))
products = data.css('.item')
products.each do |product|
puts product.at_css('.vproduct_list_title').text.strip
puts product.at_css('.vproduct_list_descr').text.strip
puts product.at_css('.price-value').text.strip
puts product.xpath('//img[#width = 220]/#src').map {|a| a.value }
end
Try changing:
puts product.xpath('//img[#width = 220]/#src').map {|a| a.value }
to:
puts product.xpath('.//img[#width = 220]/#src').map {|a| a.value }
The point of the '.' there is to say you want all images that are children of the current node (e.g. so you're not peeking at product 2's images).
File#basename will return only the filename:
File.basename('http://www.test.com/product4.jpg')
#=> "product4.jpg"
So you probably want something like this:
puts product.xpath('//img[#width = 220]/#src').map {|a| File.basename(a.value) }

Get next,previous post in 2 level categories in wordpress

I have 2 level categories like this:
paintings
- car
- cat
- dog
other
- other1
- other2
...
Now I've many posts in each sub-category.
My goal is get the next post and previous post from the current post in current main category.
For example : I've a post name : "a good car" in car category. Then I've a post name : "A big cat " in cat category . Then I've a post name : "a small dog " in dog category.
Now in the "a big cat" post , I'm trying to get the 'a small dog' post as next post and the 'a good car' as prev post.
I'm used :
codex.wordpress.org/Function_Reference/get_next_post
and :
codex.wordpress.org/Function_Reference/get_previous_post
but they still can't get the right post for me.
If I try with :
get_previous_post( false, '' )
I will get the previous post but not in painting category.
Also If I try with:
get_previous_post( true, '' )
I will get only the previous post in the current category , not the painting category.
Anyone can help me ? Thank you!
First, I would be sure that your "cat" paintings belong to both the "cat" and "paintings" categories. By default, I believe, the WordPress PREV/NEXT functions will work the way you're explaining.
i would expect this to work for you, provided your items are included in both the sub and main categories:
<?php previous_post_link('%link', 'Previous in category', TRUE); ?>
More details:
http://codex.wordpress.org/Template_Tags/previous_post_link and
http://codex.wordpress.org/Template_Tags/next_post_link

Resources