Wordpress: the_post_navigation() 'excluded_terms' not excluding posts - wordpress

I'm trying to create the next/previous post navigation for single post on a wordpress blog.
There is a post category called "lifestyle-blog" with id 37 that is different from other posts, so the navigation on the posts in that category is supposed to display only posts from that category, while on all other posts it should display posts from all categories except from the one with id 37.
I was able to set the navigation on posts from "lifestyle-blog" category to display only the posts from that category, but I don't understand why I can't exclude "lifestyle-blog" category posts from the navigation on posts from other categories.
Here is the code I'm using at the moment.
$next_post = get_next_post();
$previous_post = get_previous_post();
if (has_category( 'lifestyle-blog', $post )) {
$in_same_term = true;
$post_ids = '';
} else {
$in_same_term = false;
$cat_post_ids = get_posts(array(
'numberposts' => -1, // get all posts.
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => '37',
),
),
'fields' => 'ids', // Only get post IDs
));
$post_ids = implode( ',', $cat_post_ids );
}
the_post_navigation( array(
'next_text' => '<div class="next-text"><span class="meta-nav">' . __( 'Next: ', 'twentyfifteen' ) . '</span> ' . '<span class="post-title">%title</span></div>' . get_the_post_thumbnail($next_post->ID,'paging-thumb'),
'prev_text' => get_the_post_thumbnail($previous_post->ID,'paging-thumb') . '<div class="prev-text"><span class="meta-nav">' . __( 'Previous: ', 'twentyfifteen' ) . '</span> ' .'<span class="post-title">%title</span></div>',
'in_same_term' => $in_same_term,
'excluded_terms' => $post_ids
) );
I tried placing print_r($post_ids) beneath the navigation, and on regular posts it prints the string of comma separated ids of posts from lifestyle-blog, so I don't understand why they are not excluded from the_post_navigation().
Can anyone help me solve this?

I ended up ditching the_post_navigation() in favor of next_post_link() and previous_post_link()
$cat_id = get_cat_ID( 'Lifestyle Blog' );
$taxonomy = array(
'category'
);
$ct_args = array(
'exclude' => array($cat_id),
'fields' => 'ids'
);
$ct_ids = get_terms( $taxonomy, $ct_args );
if (has_category( 'lifestyle-blog', $post )) {
$in_same_term = true;
$excluded_terms = $ct_ids;
$next_post = get_next_post($in_same_term, $excluded_terms);
$previous_post = get_previous_post($in_same_term, $excluded_terms);
} else {
$in_same_term = false;
$excluded_terms = $cat_id;
$next_post = get_next_post($in_same_term, $excluded_terms);
$previous_post = get_previous_post($in_same_term, $excluded_terms);
}
$prev_text = '<div class="next-text"><span class="meta-nav">' . __( 'Next: ', 'twentyfifteen' ) . '</span> ' . '<span class="post-title">%title</span></div>' . get_the_post_thumbnail($previous_post->ID,'paging-thumb');
$next_text = get_the_post_thumbnail($next_post->ID,'paging-thumb') . '<div class="prev-text"><span class="meta-nav">' . __( 'Previous: ', 'twentyfifteen' ) . '</span> ' .'<span class="post-title">%title</span></div>';
<nav class="navigation post-navigation">
<div class="nav-links">
<div class="nav-previous">
<?php next_post_link('%link', $next_text, $in_same_term, $excluded_terms); ?>
</div>
<div class="nav-next">
<?php previous_post_link('%link', $prev_text, $in_same_term, $excluded_terms); ?>
</div>
</div>
</nav>

Related

How show in foreach post of only parent category

I have a script that creates tabs.
The parent categories are shown in the navigation tabs.
While the posts are shown in the content of the tabs.
I have a problem with the first foreach which should only print posts belonging to parent categories.
But in the third tab "Punti vendità" which should print only the posts of the subcategoories the 1st foreach prints the posts of the subcategories erroneously duplicating the posts.
Summary...
I have the first foresch that needs to print only .post-category cards for parent categories.
And the second foreach correctly prints the sub-category .post-sub-category cards.
How do you tell the first foreach to print only posts belonging to parent categories?
Thanks a lot for the help
`
<div data-parent="true" class="vc_row row-container" id="tab-category-distributori">
<div class="row no-top-padding triple-bottom-padding tab-category-distributori single-h-padding limit-width row-parent" data-imgready="true">
<div class="wpb_row row-inner">
<div class="wpb_column pos-top pos-center align_left column_parent col-lg-12 single-internal-gutter">
<div class="uncol style-light">
<div class="uncoltable">
<div class="uncell">
<div class="uncont no-block-padding">
<div class="uncode-tabs wpb_content_element border-100" data-interval="" data-target="row">
<div data-parent="true" class="vc_row row-container" id="tab-category-distributori">
<!-- start tab category -->
<div class="uncode-wrapper tab-container default-typography">
<?php
function ow_categories_with_subcategories_and_posts($taxonomy, $post_type) {
// pure CSS example https://codepen.io/maheshambure21/pen/VvxWYZ
function card_distributori() {
$foto_sede = get_field('foto_sede');
$ragione_sociale = get_field('ragione_sociale');
$indirizzo = get_field('indirizzo');
$telefono = get_field('telefono');
$fax = get_field('fax');
$cellulare = get_field('cellulare');
$e_mail = get_field('e-mail');
$sito_web = get_field('sito_web');
$labelTel = '';
$labelFax = '';
$labelCelulare = '';
$labelEmail = '';
$labelWeb = '';
if (ICL_LANGUAGE_CODE == 'ita') {
$labelTel = 'Tel';
$labelFax = 'Fax';
$labelCelulare = 'Mobile';
$labelEmail = 'E-mail';
$labelWeb = 'Sito web';
} elseif (ICL_LANGUAGE_CODE == 'eng') {
$labelTel = 'Tel';
$labelFax = 'Fax';
$labelCelulare = 'Mobile';
$labelEmail = 'E-mail';
$labelWeb = 'Website';
} elseif (ICL_LANGUAGE_CODE == 'fra') {
$labelTel = 'Tel';
$labelFax = 'Fax';
$labelCelulare = 'Portable';
$labelEmail = 'E-mail';
$labelWeb = 'Site Internet';
} elseif (ICL_LANGUAGE_CODE == 'deu') {
$labelTel = 'Tel';
$labelFax = 'Fax';
$labelCelulare = 'Mobile';
$labelEmail = 'E-mail';
$labelWeb = 'Webseite';
} elseif (ICL_LANGUAGE_CODE == 'pol') {
$labelTel = 'Tel';
$labelFax = 'Faks';
$labelCelulare = 'Mobilny';
$labelEmail = 'E-mail';
$labelWeb = 'Stronie internetowej';
} elseif (ICL_LANGUAGE_CODE == 'rus') {
$labelTel = 'тел';
$labelFax = 'Факс';
$labelCelulare = 'мобильный';
$labelEmail = 'Эл. почта';
$labelWeb = 'Веб-сайт';
} elseif (ICL_LANGUAGE_CODE == 'zhs') {
$labelTel = '电话';
$labelFax = '传真';
$labelCelulare = '移动的';
$labelEmail = '电子邮件';
$labelWeb = '网站';
}
echo '<div class="post-category_card-distributori">';
if ($foto_sede) {
echo '<div class="image"><img src="' . $foto_sede . '"/></div>';
}
if ($ragione_sociale) {
echo '<h4 class="ragione-sociale">' . $ragione_sociale . '</h4>';
}
if ($indirizzo) {
echo '<div class="indirizzo">' . $indirizzo . '</div>';
}
if ($indirizzo) {
echo '<p class="telefono">' . $labelTel . ': ' . $telefono . '</p>';
}
if ($fax) {
echo '<p class="fax">' . $labelFax . ': ' . $fax . '</p>';
}
if ($cellulare) {
echo '<p class="cellulare">' . $labelCelulare . ': ' . $cellulare . '</p>';
}
if ($e_mail) {
echo '<p class="e_mail">' . $labelEmail . ':<br> ' . $e_mail . '</p>';
}
if ($sito_web) {
echo '<p class="sito_web">' . $labelWeb . ':<br> ' . $sito_web . '</p>';
}
echo '</div><!-- end post-category_card-distributori -->';
};
// Get the top categories that belong to the provided taxonomy (the ones without parent)
$categories = get_terms(
array(
'taxonomy' => $taxonomy,
'parent' => 0, // <-- No Parent
'orderby' => 'term_id',
'hide_empty' => true // <!-- change to false to also display empty ones
)
);
echo '<div class="category-class">';
echo '<div class="vc_tta-tabs-container">';
echo '<ul class="nav nav-tabs wpb_tabs_nav ui-tabs-nav vc_tta-tabs-list text-center">';
// Iterate through all categories to display each individual category
$countTab = 0;
foreach ($categories as $category) {
$cat_name = $category->name;
$cat_id = $category->term_id;
$cat_slug = $category->slug;
$countTab++;
// Display the name of each individual category
// echo '<h3>Category: ' . $cat_name . ' - ID: ' . $cat_id . ' - Slug: ' . $cat_slug . '</h3>';
// echo '<h3>Category: ' . $cat_name . '</h3>';
if ($countTab === 1) {
echo '<li data-tab-id="' . $cat_slug . '" data-tab-o-id="' . $countTab . '" class="vc_tta-tab active ' . $countTab . '">
<span>' . $cat_name . '</span>
</li>';
} elseif ($countTab > 1) {
echo '<li data-tab-id="' . $cat_slug . '" data-tab-o-id="' . $countTab . '" class="vc_tta-tab ' . $countTab . '">
<span>' . $cat_name . '</span>
</li>';
}
}
echo '</ul><!-- // nav-tabs -->';
echo '</div><!-- end vc_tta-tabs-container -->';
echo '<div class="tab-content wpb_wrapper">';
// Get the top categories that belong to the provided taxonomy (the ones without parent)
$categoriespost = get_terms(
array(
'taxonomy' => $taxonomy,
'parent' => 0, // <-- No Parent
'orderby' => 'term_id',
'hide_empty' => true // <!-- change to false to also display empty ones
)
);
$countTabCont = 0;
foreach ($categoriespost as $category) {
$cat_name = $category->name;
$cat_id = $category->term_id;
$cat_slug = $category->slug;
$countTabCont++;
// Get all posts that belong to this specific subcategory
$postsCategory = new WP_Query(
array(
'post_type' => $post_type,
'posts_per_page' => -1, // <-- Show all posts
'hide_empty' => true,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $category->term_id,
)
)
)
);
// echo '<div class="tab-content">';
// open tab content
if ($countTabCont === 1) {
echo '<div data-id="' . $cat_slug . '" class="tab-pane tab-pane-distributori fade half-internal-gutter single-block-padding active in">
<div>
<div class="uncode_text_column">';
} elseif ($countTabCont > 1) {
echo '<div data-id="' . $cat_slug . '" class="tab-pane tab-pane-distributori fade half-internal-gutter single-block-padding">
<div class"container-cards">
<div class="uncode_text_column">';
}
// If there are posts available within this subcategory
// show post of all category but need show only post parent of category level 0
if ($postsCategory->have_posts()) {
echo '<div class="post-category">';
// se la categoria è òa 209 .. la salto perchè faccio vedere le sottocategorie
//if ($cat_id != 209){
// As long as there are posts to show
while ($postsCategory->have_posts()) : $postsCategory->the_post();
card_distributori();
endwhile;
//}
echo '</div><!-- end post-category -->';
} else {
echo '<div class="hide-post-nofound">No posts found</div>';
};
// Get all the subcategories that belong to the current category
$subcategories = get_terms(
array(
'taxonomy' => $taxonomy,
'parent' => $cat_id, // <-- The parent is the current category
'orderby' => 'term_id',
'hide_empty' => true
)
);
echo '<div class="sub-category-class">';
// Iterate through all subcategories to display each individual subcategory
foreach ($subcategories as $subcategory) {
$subcat_name = $subcategory->name;
$subcat_id = $subcategory->term_id;
$subcat_slug = $subcategory->slug;
// Display the name of each individual subcategory with ID and Slug
// echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug . '</h4>';
echo '<h4 class="sub-category-class_title">' . $subcat_name . '</h4>';
// Get all posts that belong to this specific subcategory
$postsSubcategory = new WP_Query(
array(
'post_type' => $post_type,
'posts_per_page' => -1, // <-- Show all posts
'hide_empty' => true,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => $subcat_id,
'field' => 'id'
)
)
)
);
// If there are posts available within this subcategory
if ($postsSubcategory->have_posts()) {
echo '<div class="post-sub-category">';
// As long as there are posts to show
while ($postsSubcategory->have_posts()) : $postsSubcategory->the_post();
card_distributori();
endwhile;
echo '</div><!-- end post-sub-category -->';
} else {
echo 'No posts found';
};
wp_reset_query();
} // end foreach subcategory
echo '</div><!-- end sub-category-class -->';
// echo '</div><!-- tab-content -->';
// close tab content
echo '</div><!-- // .uncode_text_column -->
</div><!-- // .container-cards -->
</div><!-- // .tab-pane -->';
} // end foreach category
echo '</div><!-- end category-class -->';
} ow_categories_with_subcategories_and_posts('categoria-distributore', 'distributore');
?>
</div><!-- close tab category -->
</div>
</div>
</div>
</div>
</div>
</div>
<script id="script-tab-category-distributori" data-row="script-tab-category-distributori" type="text/javascript" class="vc_controls">
UNCODE.initRow(document.getElementById("tab-category-distributori"));
</script>
</div>
</div>
</div>
</div><!-- fix full with under block -->
`
I fixed it by putting this parameter in the array
'include_children' => 0,
// Get all posts that belong to this specific subcategory
$postsCategory = new WP_Query(
array(
'post_type' => $post_type,
'posts_per_page' => -1, // <-- Show all posts
'hide_empty' => true,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $category->term_id,
'include_children' => 0,
)
)
)
);

Get featured images from custom taxonomy terms

Oke, first of I downloaded this plugin. This enables me to give my custom taxonomy terms an image. This works great. But the way my theme is setup and the way the plugin wants to work, isn't for me.
I only want the images to show in the place where I say it should. That's why I used terms (categories). Because then I can use this code:
<?php
//list terms in a given taxonomy
$taxonomy = 'our_team';
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>
That only shows the names of the team members when you activated them (checked the box) in the post, and not in other places.
Now I would like to be able to use a similar code but for the featured images. The plugin uses this code to get the thumbnails.
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="my-custom-class-name">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'post_id' => 15,
'taxonomy' => 'our_team',
) );
Does anyone know how to "merge" these two codes together and generate an image only if I checked the box in the custom post type. Or knows another way to do this?
** UPDATE **
Ok, getting somewhere. The code is now only displayed where I want it. With the help of Rohit Kishore! Thanks! Here is the working code:
<div class="teamleden over-ons-ul">
<?php $terms = get_the_terms( $post->ID , 'our_team' );
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="our-team">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'taxonomy' => 'our_team',
) );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'our_team' );
if( is_wp_error( $term_link ) )
continue;
}
?>
</div>
The print code had to be before the foreach part...

How To Get the children of the parent category by parent id in wordpress

i try to get All child categories from my parent category
like my parent category is news and child is news1 news 2 news 3
Use following code for to get children category of parent category.
<?php
$parent_cat_arg = array('hide_empty' => false, 'parent' => 0 );
$parent_cat = get_terms('category',$parent_cat_arg);//category name
foreach ($parent_cat as $catVal) {
echo '<h2>'.$catVal->name.'</h2>'; //Parent Category
$child_arg = array( 'hide_empty' => false, 'parent' => $catVal->term_id );
$child_cat = get_terms( 'category', $child_arg );
echo '<ul>';
foreach( $child_cat as $child_term ) {
echo '<li>'.$child_term->name . '</li>'; //Child Category
}
echo '</ul>';
}
?>
This code will display all the child categories of selected parent category, tested and works in category.php template file...
$this_category = get_category($cat);
//echo $this_category->cat_ID;
$parent_term_id =$this_category->cat_ID; // term id of parent term
//$termchildren = get_terms('category',array('child_of' => $parent_id));
$taxonomies = array(
'taxonomy' => 'category'
);
$args = array(
// 'parent' => $parent_term_id,
'child_of' => $parent_term_id
);
$terms = get_terms($taxonomies, $args);
if (sizeof($terms)>0)
{
echo ' <div class="categories"> ';
echo '<p> Sub Categories of '. get_cat_name( $parent_term_id ) .'</p>';
foreach ( $terms as $term ) {
$term_link = sprintf( '<div class="custom-cats">%3$s</div>', esc_url( get_category_link( $term->term_id ) ),
esc_attr( sprintf( 'View all posts in %s', 'textdomain' ), $term->name ),
esc_html( $term->name ));
echo sprintf( $term_link );
}
echo '</div><!-- categories div end-->';
}

Get Wordpress child category from specific parent

I'm building a small thumbnail gallery with posts from a category that has the ID 406.
Some of the posts are in multiple categories and I'm not sure how to grab the category name that is a child of 406. $post_cat[0]->name; returns a category, but I only need it to return children of 406.
$thumbnails = get_posts('posts_per_page=10&cat=406');
foreach ($thumbnails as $thumbnail) {
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => 406,
);
$categories = get_categories ( $args );
foreach ( $categories as $category) {
$cat_name = $category->name;
$cat_slug = $category->slug;
}
echo $cat_name;
echo $cat_slug;
}
*EDIT**
$thumbnails = get_posts('posts_per_page=10&cat=406');
foreach ($thumbnails as $thumbnail) {
$post_cat = get_the_category( $thumbnail->ID );
echo '<li><a class="side-thumb" href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
if ( has_post_thumbnail($thumbnail->ID)) {
echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
$upload_dir = wp_upload_dir();
$art_image = ''.$upload_dir['basedir'].'/icons/'.$post_cat[0]->slug.'.png';
if (file_exists($art_image)) {
echo '<p class="artist-latest"><img src="http://site.com/wp-content/uploads/icons/'.$post_cat[0]->slug.'.png" alt="'.$post_cat[0]->slug.'"/>'.$post_cat[0]->name.'</p>';
} else{
echo '<p class="artist-latest">'.$post_cat[0]->name.'</p>';
}
} else {
}
echo '</a></li>';
}
So now I'm fetching a list of category and its child, then I fetch posts having parent category assigned to it, after I get posts array, I loop into it and get all the categories assigned to the post, If there are additional categories other than the required category and its child I skip the post, otherwise do whatever I want to do.
$category = get_category_by_slug( 'category-name' );
$args = array(
'type' => 'post',
'child_of' => $category->term_id,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => FALSE,
'hierarchical' => 1,
'taxonomy' => 'category',
);
$child_categories = get_categories($args );
$category_list = array();
$category_list[] = $category->term_id;
if ( !empty ( $child_categories ) ){
foreach ( $child_categories as $child_category ){
$category_list[] = $child_category->term_id;
}
}
$posts_args = array(
'posts_per_page' => 10,
'cat' => $category->term_id,
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts = new WP_Query ( $posts_args );
if ( $posts->have_posts() ){
while ( $posts->have_posts() ){
$posts->the_post();
$category_array = array();
$post_categories = get_the_category ( get_the_ID() );
if ( !empty ( $post_categories ) ){
foreach ( $post_categories as $post_category ) {
$category_array[] = $post_category->term_id;
}
}
//Checks if post has an additional category
$result = array_diff( $category_array, $category_list );
if ( empty ( $result ) ) { ?>
<li>
<a href="<?php the_permalink(); ?>" class="side-thumb" title="<?php the_title(); ?>"> dfdf<?php
if ( has_post_thumbnail() ){
echo get_the_post_thumbnail();
}
//Put your default icon code here
?>
</a>
</li> <?php
}
}
}
wp_reset_postdata();
Above answer is as per your explanation, but I think what you want is, like you have a category Artists and there are subcategories each having a specific name, So for Artist gallery you want to display list of all the artists with their pictures.
I also needed to get list of child categories of selected parent category with url,
I found following code so useful
<?php
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 5
) );
foreach ( $categories as $category ) {
printf( '<li><a class="btn btn-light btn-light-custom mt-2" href="%1$s">%2$s</a></li>',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
?>
Replace "5" with your selected parent category ID

wordpress list custom categories with posts list below

I got custom post types and categories made for them. All I want to do is to list all the custom categories with list of all posts that belongs to this category (with title and excerpt). None of the solutions I found works. I can display custom categories but can't list posts that belong to these categories. Here's my code
<?php
$show_count = 0;
$pad_counts = 0;
$hierarchical = 1;
$taxonomy = 'timeline_category';
$title = '';
$description = true;
$args = array(
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'taxonomy' => $taxonomy,
'use_desc_for_title' => $description,
'title_li' => $title
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
//display posts
$cat_new = $category->term_id;
$post_args = array( 'numberposts' => 5, 'category' => $cat_new, 'caller_get_posts' => 0 );
$myposts = get_posts( $post_args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach; } ?>
The code above shows even that there is 1 post in this category (Post Count) so the problem is somwhere in foreach agrument for posts.
Any ideas?
Thanks
I'm not too familiar with endforeach;, but after a quick Google search, it looks like you have a syntax error.
<?php foreach($foo as $bar):
// Stuff
endforeach; // No trailing "}" ?>
Or
<?php foreach($foo as $bar){
// Stuff
} ?>

Resources