i am trying to get a count of top level categories , i am using wordpress 2016 theme , its a static front page and i am trying to retrieve the total count of categories and then dynamically create table with those categories
i tried to search with WP_Query and wp_list_categories it does list names but not the total count , i tried to use count function in php but output is always 0(zero) even though i have few categories / posts etc already created to test this functionality.
please advise or assist with the code
Thanks
Try Below Code :
<?php
$args = array(
'parent' => 0,
'hide_empty' => 0
);
$categories = get_categories( $args );
echo count( $categories );
?>
You can also wrap it in a function to re-use it else where in your site.
Hope this will help you!
Related
I have following structure
Services
-Page_1
-Page_2
--Page_3
--Page_4
I use Wp-query to get pages. This is my args:
<?php $args = array(
'post_type' => 'page',
'post_parent' => '45'
); ?>
45 is ID of Services page.
But I only get first level Page_1 and Page_2. How do I get all pages? I'm using Advanced Custom Fields plugin so using get_pages() is not a good option, is it?
As mentioned in your comments, the easiest solutions is to actually use get_pages(). You can still grab meta data of posts if you are able to get the the ID of the page (which you can with get_pages()) so you should still be able to ACF's custom fields.
Here's an example of get_pages():
$args = array(
'child_of' => 45,
'post_type' => 'page',
);
$pages = get_pages($args);
The main difference between get_pages() and WP_Query that we want to focus on here is the 'child-of'=>45 paramater vs the 'post_parent'=>45.
The child-of argument will grab ALL children throughout the hierarchy, i.e. children and children's children etc.
In contrast, the post-parent argument of WP_Query will only grab direct children of the page.
Using in conjunction with ACF
If you need to grab custom fields from ACF, you will still be able to use get_post_meta().
If your custom field includes an array of values, you will need to unserialize it first and then loop through the values like this:
$meta = get_post_meta( $post->ID, 'acf_meta_key', true ); //grab the post meta using WordPress core function
$array = unserialize( $meta ); //unserialize the field into an array
foreach ( $array as $value ) { //loop through the array
echo $value .'<br>';
}
I'm having a bad time trying to make wordpress querys excluding categories unless asked.
I mean, if not specifically asked (ex: $args= array('cat'=>'-35')) , the loop excludes this category.
what I got so far:
function exclude_categories( $wp_query ) {
$excluded_cats = array( '-35' , '-36' );
set_query_var( 'category__not_in', $excluded );
}
add_action( 'pre_get_posts', 'exclude_categories' );
This is doing ok excluding posts with this category but then if I try to ask for posts with this category it wont display any.
Any sugestions?
Can you show the whole code with when you ask for posts including this category?
Basically, you can set these parameters to inlucde or exclude categories in the query.
'category__not_in'=> array(1,2),
'category__in' => array(3,3)
However in your example you have already pre-set set_query_var( 'category__not_in', $excluded );
categories so even if you use category__in the query won't use it because of the usage of category__not_in. You need to set this parameter to empty value.
So, I have a custom field called "geo_location".
And under that field, there are total 5 different values (1 to 5).
Now, there are total 50 posts that have one of the five values.
For example, post#1 has geo_location="1" and post#2 has geo_location="3" etc.
Is there a way to show all the posts under the same 'geo_location' value?
So for example, I want to show all the posts under the same geo_location="1".
how can I achieve this?
Thank you.
You need to use WP_Query to create a database query using your custom meta-data.
This is done like this:
$args = array(
'meta_key' => 'geo_location',
'meta_value' => '1'
);
$query = new WP_Query( $args );
Then you can output the result using the loop in the usual way.
My Wordpress site has multiple custom categories and posts types all which need to be displayed on the home page blogroll. But I need the first post on the homepage always to be the most recent post from a specific category.
For example the pst at the top of the page will always be the latest "giraffe" category; and below it; sorted default/chronologically are more "walrus" mixed with "seagull" and "nachos"
What would be the best way to accomplish this?
Without running through the Loop twice, sticky posts are your best option.
Otherwise:
...
$args = array(
'orderby' => 'post_date',
'category'=> 'giraffe',
'numberposts'=> 1,
);
$latest_giraffe_post = get_posts( $args );
...
then do what you're already doing for the rest
Some background info --
In wordpress I have my portfolio as a parent page with each item of work a child page.
I also have a blog.
I want to display the most recent 6 items on the homepage whether they are from my portfolio or my blog.
I have a loop that displays the posts and on another page I have a loop that displays the child pages of a specific parent page. I only need to display the_title and the_post_thumbnail.
Is it possible to loop through both the posts and the child pages and display them in order of date (recent first).
So the final display would be a mixture of posts and child pages.
Could it be done by having a separate loop for pages and one for posts, then somehow adding the results to an array and then pull them out in date order (recent first).
Any help would be greatful.
Thanks
Use a custom query.
Example
$latest_content = new WP_Query(
array(
'posts_per_page' => 6,
'post_type' => 'any',
'post_status' => 'publish'
)
);
if ( $latest_content->have_posts() )
{
while ( $latest_content->have_posts() )
{
$post = $latest_content->next_post();
// Do anything you know about the loop.
print $post->guid . '<br>';
}
}
rewind_posts();
I have not tried it myself, but I think you can use the get_posts template tag with post_type => 'any'.
Like so (default ordering is date descending):
<?php
$args = array('post_type' => 'any', 'numberposts' => 6);
$posts_and_pages = get_posts($args);
?>