How would I pull an array of all the labels and links for every menu item in the $primary_nav variable?
You can just use php's print_r(). Or use a dedicated module like devel : it provides helpers which pretty print variables including arrays and objects. It also provides a clear cache button, can generate random nodes, etc.
I think devel's dprint_r() and dpm() are the most used functions and the most useful I guess for Drupal developers when it comes to print and debug variables.
In your case, $primary_nav is a processed variable : it is first prepared by functions from the registered theme, then it can be altered in process or preprocess hooks provided by themes and/or modules.
Knowing that, you can either print it right from the template you are working on, like page.tpl.php :
<?php if (!empty($primary_nav)): ?>
<!--<?php print render($primary_nav); ?>-->
<?php dpm($primary_nav); ?>
<?php endif; ?>
.. or debug it "earlier" in a process or preprocess function like :
function some_preprocess_page(&$variables) {
dpm($variables['primary_nav']);
}
Also, if you don't know what is going on or what variables are available in a template, combining php's get_defined_vars() with a pretty print function can be vey helpful : dpm(get_defined_vars());
I have this WP_Query:
<?php $query = new WP_Query(
array( "post_type" => "post",
"showposts" => "12"
) );
while ($query->have_posts()) : $query->the_post(); ?>
...
My goal is to use this function for displaying the posts in this order:
1 3 5 7 9 11 2 4 6 8 10 12
So that 6 rows with 2 columns each will be displayed like this:
1 2
3 4
5 6
7 8
9 10
11 12
The page logic that I have requires this specific order.
At the moment I'm achieving this using shortcodes, but that means 12 mySQL queries instead of just 1 and having the result displayed in this custom order.
I know that I could do this if I setup a custom field where to specify the order in which I want the result provided.
However, I don't want to complicate an editor's life, so I want this to be achieved by stating somewhere the order in which I want the WP_Query results displayed.
I had a look at post__in, but that would involve manually providing the posts IDs, so it is not an option since this is the home page and the post numbers will always be different.
Basically, no matter which are the latest posts (what ID they have), I want to display them like this:
1,3,5,7,9,11,2,4,6,8,10,12
Instead of the default order, which is:
1,2,3,4,5,6,7,8,9,10,11,12
There is a very simple and easy way to achieve this. You can take advantage of the built in loop counter, $wp_query->current_post. There is no need for custom queries etc. Inside your loop, do something like this
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php /* Start the Loop */ ?>
<div class="entry-content <?php if( $wp_query->current_post%2 == 1 ) echo ' left-post'; ?>">
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
<?php endwhile; ?>
This will add a class of left-post to every second post. You can now style accordingly (float your entry-content left and set the width to about 48%). To avoid wacky styling problems, simply add .left-post { clear: left; } to your stylesheet
It's really simple, actually.
Install a plugin called Simple Page Ordering: https://wordpress.org/plugins/simple-page-ordering/
And add the 'oderby' => 'menu_order' argument in your query.
Also, instead of using "showposts", use posts_per_page.
thanks.
I'm sorry to say that it's unlikely you'll achieve this using WP_Query. I believe you're likely approaching this in the wrong way.
Your results will need to be returned in a structured and uniform way, and you'll have to handle them appropriately, regardless of the method to return them. Doing it the way you're proposing can easily cause issues with pagination, and MySQL doesn't have an ordering algorithm to achieve it, so any query written will be slow. You'll limit yourself moving forward too.
I'm guessing you are doing something with columns on the web page, which is why you want the results in this order. If this is the case there are lots of ways to achieve it without having to reorder your results in this manner.
Without trying to sound offensive, it sounds like a case of over-complicating the problem, and it can be solved earlier on in the process rather than tinkering with the data source itself.
If you can send over what you're doing with the results themselves (maybe better in a different question?) then you might get better results.
I am trying to get data (data) from the following URL.
http://www.example.dk/page/?data=1
I use the following code for getting the data to the wordpress page:
<?php $_GET['data']; ?>
But I don't get the data to the page. Isn't it the normal way to get data from URL with php?
As Matt pointed out, you'll need to echo the the result. Like:
<?php echo $_GET['data']; ?>
If your server is setup to use PHP shorthand statements you might also be able to use
<?= $_GET['data'] ?>
I have created a loop to show some products on my wordpress site and they seem to be working fine they reel out the products however if I place one of custom fields after the loop it doesn't show. I know its not an issue with the custom field itself as it works fine if I put it above the loop. Does anyone know where I could be going wrong?
Here is my code:
http://pastebin.com/SVxYK0XP
Thanks
You are calling setup_postdata() within your loops, therefore overwriting the $post object.
When you are calling the_field('monoblock_valves_text'); after the foreach loop, it's trying to get that custom field out of the last post of the loop, while it clearly needs to get it from the actual post/page showing.
You need to store the old $post object before the loop, and restore it after the loop, as such:
$old_post = $post;
foreach($products_mono_posts as $post):
setup_postdata($post);
// Rest of code
endforeach;
$post = $old_post;
setup_postdata($post);
the_field('blahblahblah');
OK, So I have this.
<?php
$count_posts = wp_count_posts('attractions');
echo $count_posts->publish;
?>
I currently have the amount of attraction posts echoing out onto the page, however when I try to add another it break. How do I add say attractions and Restaurants together for example?
If I do
$count_posts = wp_count_posts('attractions', 'festivals');
echo $count_posts->publish;
it wont add the two together, so my question is how do I write it so it adds them together?
Your question is basic PHP.
$attractions_count = wp_count_posts('attractions');
$restaurants_count = wp_count_posts('restaurants');
$festivals_count = wp_count_posts('festivals');
$total = $attractions_count->publish + $restaurants_count->publish + $festivals_count->publish;
echo $total;
Always check the PHP Manual.
Lots of good tutorials.
WordPress Codex, essential.
Search this site before asking, probably you'll find many of your doubts already answered, PHP, Html, CSS and jQuery.
WordPress StackExchange is also full of good material.