so I have this Issue that I cannot solve no matter what I do , thanks in advance for help :
What I am trying to do is that when I am on the post page , I give a class to the parent to its parent
I am using the following code to get the post id and its category id:
get_header();
$theCategory = get_the_category();
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 52,
'hide_empty' => 0
));
$thiscat = get_queried_object_id();
$catobject = get_category($thiscat,false); // Get the Category object by the id of current category
$catobject2 = get_category($catobject,false);
$parentcat = $catobject2->category_parent; // the id of the parent category
?>
<div class="container">
<?php echo category_description( get_query_var( 'cat' ) ); ?>
</div>
<?php var_dump($thiscat) ?>
After this , if the post is in the right categoty I am trying to add a class of active here :
<?php if( $categories ): ?>
<ul class="main-categ">
<?php foreach ( $categories as $kCat => $vCat ): ?>
<li class="main-item <?php echo $thiscat->category_parent == $vCat->cat_ID ? 'active' : ''; ?>"><?php
$subCategories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => $vCat->cat_ID,
'hide_empty' => 0
));?>
<a class="main-link"><?php echo $vCat->name; ?></a>
<?php if( $subCategories ): ?>
<ul class="subcateg">
<?php foreach ( $subCategories as $kSub => $vSub ): ?>
<li class="<?php echo get_query_var( 'cat' ) == $vSub->cat_ID ? 'activ':'';?>"><i class="fa fa-angle-double-right"></i> <?php echo $vSub->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Related
<?php
$categories = get_categories(
array('
hide_empty' => TRUE
)
);
foreach($categories as $category) { ?>
<?php
$args=array(
'cat' => $category->term_id,
'post_type' => 'ourservices',
//'posts_per_page' => 10,
//'category_name' => 'our-services', // replace it with your category slug
'orderby' => 'name',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
?>
<h2 class><?php echo $category->name; ?></h2>
<ul class="list-group list-group-flush">
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li class="list-group-item"><?php the_title(); ?></li>
<?php }?>
</ul><br>
<?php }
?>
<?php }
wp_reset_postdata();
?>
I tried with 'parent'=>0 but working with required result!
You could use an if statement to check whether your parent category is the one you need to exclude.
So your code would be something like this:
$categories = get_categories(
array(
'hide_empty' => TRUE
)
);
foreach ($categories as $category) { ?>
<?php
$args = array(
'cat' => $category->term_id,
'post_type' => 'ourservices',
'orderby' => 'name',
'order' => 'ASC',
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
if ('Our Services' !== $category->name) {
?>
<h2 class><?php echo $category->name; ?></h2>
<ul class="list-group list-group-flush">
<?php while ($the_query->have_posts()) {
$the_query->the_post(); ?>
<li class="list-group-item"><?php the_title(); ?></li>
<?php } ?>
</ul><br>
<?php
}
}
?>
<?php }
wp_reset_postdata();
I want to list all closes children to the current page, not grand children or siblings, but when I try this, I get all pages.
function list_my_child_pages( $post ) {
$list_p = array(
'sort_column' => 'menu_order, post_title',
'child_of' => $post->ID,
'sort_order' => 'ASC'
);
$children = get_pages($list_p);
$result = "<ul>";
foreach ( $children as $child )
{
$child_id = $child->ID;
$url = get_permalink( $child_id );
$thumb = get_the_post_thumbnail($child_id, array(240, 240));
$title= $child->post_title;
$link = "<a href='$url'><div class='child_page_thumb'>$thumb</div><div class='child_page_title'>$title</div></a>";
$result .= "<li>$link</li>";
}
$result .= "</ul>";
echo $result;
}
To me it looks like it should not list siblings with this query, but it does. And how do I remove the grandchildren?
I usually do that directly in the templates, here is my code :
<?php
// Showing the list of child pages
// Check if current page has children pages
$children = get_pages('child_of='.$post->ID.'&parent='.$post->ID);
// if current page got children pages :
if (sizeof($children) > 0):
$args = array(
'post_status' => ' publish',
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'date',
);
$parent = new WP_Query( $args );
// Showing a list in the template
if ( $parent->have_posts() ) : ?>
<aside class="item-list">
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<a class="item-from-list" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php if (has_post_thumbnail()) {the_post_thumbnail('thumbnail', array('class' => 'alignleft'));} ?>
<h2><?php the_title(); ?></h2>
<?php atm_content(20, true); ?>
</a>
<?php endwhile; ?>
</aside>
<?php endif; wp_reset_query(); ?>
<?php endif;// end if sizeof($children) ?>
And if you don't want to get a list, you can store IDs for exemple in an array as following :
if ( $parent->have_posts() ) :
$array = new Array();
while ( $parent->have_posts() ) : $parent->the_post();
array_push($array, get_the_ID());
endwhile;
endif;
I have custom post type 'cars' and its child post type is 'carvariants'.
What I want to do is get child posts (carvariants) of current post (cars). I tried this code:
<div>
<?php
$parent_id = 1064;
$the_query = new WP_Query(array(
'post_parent' => $parent_id,
'post_type' => 'carvariants',
'posts_per_page' => 1,
'meta_key' => 'wpcf-minimum-price',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
$compprd = get_the_ID(); ?>
<?php the_title(); ?>
<?php
endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
I want to display child posts of Cars order by custom field wpcf-minimum-price
but 'post_parent' is not working. This code is showing blank output. Whats wrong in this?
I didn't try this. But I hope this will work.
If it will not work, leave me a comment, and I will try to make it work.
Also, if there are better solutions, I will be glad to see the code from professionals:
<div>
<?php
$parent_id = 1064;
$args = array( 'child_of' => $parent_id );
$children_pages = get_pages( $args );
if ( count( $children_pages ) != 0 ) :
foreach ( $children_pages as $children_page ) :
if ( $children_page->have_posts() ) :
$args_for_posts = array( 'posts_per_page' => 1,
'post_type' => 'carvariants',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_parent' => $children_page );
$postlist = get_posts( $args_for_posts );
foreach ( $postlist as $post) :
setup_postdata( $post ); ?>
<ul>
<?php
the_post();
?>
</ul>
<?php
endforeach;
wp_reset_postdata();
endif;
endforeach;
else : ?>
<p>No content to show.</p>
<?php
endif; ?>
</div>
I would like to know if it's possible to show the sticky posts inside the wp_query and sort them out according to their respective categories:
loop(
- the first sticky post has the category 1
- the second sticky post has the category 2
- the third sticky post has the category 1
)
and that should display:
- category 1:
- the first sticky post
- the third sticky post
- category 2:
the second sticky post
with this html:
<div class="flex-6">
<h4><?php
$category = get_the_category();
echo $category[0]->cat_name;
?></h4>
<ul class="list">
<li><?php the_title(); ?></li>
</ul>
</div>
I have the correct loop for the sticky posts:
$sticky = get_option('sticky_posts');
$query = new WP_Query(array('post__in' => $sticky));
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
$category_name = get_the_category();
$category_name = $category_name[0]->cat_name;
endwhile; endif;
To have this final result
<div class="flex-6">
<h4>Category 1</h4>
<ul class="list">
<li>The first title</li>
<li>The third title</li>
</ul>
</div>
<div class="flex-6">
<h4>Category 2</h4>
<ul class="list">
<li>The secondtitle</li>
</ul>
</div>
Any idead?
Thanks for your time
The most straightforward way would be to get the categories first:
<?php
$cat_args = array(
'child_of' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'taxonomy' => 'category'
);
$cats = get_categories($cat_args);
and then cycle through them getting the posts:
$sticky = get_option('sticky_posts');
foreach ($cats as $cat) :
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => -1,
'orderby' => 'title', // or whatever you want
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $cat->slug
)
)
);
$posts = get_posts($args);
if ($posts) :
?>
<div class="flex-6">
<h4><?php echo $cat->cat_name; ?></h4>
<ul class="list">
<?php foreach ($posts as $post) : ?>
<li><?php echo get_the_title($post->ID); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php
endif;
endforeach;
I have a categories page template, listing all categories with featured images. But I want to show only subcategories of a parent category. I don't know where to modify the template. Here is the code.
get_header(); ?>
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<h1 class="border-radius-5"><?php the_title(); ?></h1>
<div id="page" class="post-content">
<?php the_content(); ?>
<?php
$terms = get_terms("category", $args);
$count = count($terms);
$categories = array();
if ($count > 0) {
echo '<ul class="listing-cat">';
foreach ($terms as $term) {
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$video_from_categorie = new WP_Query( $args );
if( $video_from_categorie->have_posts() ){
$video_from_categorie->the_post();
}else{}
$term->slug;
$term->name;
?>
<li class="border-radius-5 box-shadow">
<?php echo get_post_image();?>
<span><?php echo $term->name; ?></span>
<span class="nb_cat border-radius-5"><?php echo $term->count; ?> <?php if ( $term->count > 1 ){
_e( 'videos', get_theme_name() );
}else{
_e( 'video', get_theme_name() );
} ?></span>
</li>
<?php
}
echo '</ul>';
echo '<div class="clear"></div>';
}
?>
</div><!-- #page -->
<?php endwhile; ?>
<?php endif; ?>
Pass the ID of the desired parent term/category to the child_of parameter of get_terms():
$terms = get_terms( 'category', array( 'child_of' => TERM_ID_GOES_HERE ) );