get current category ID in wordpress - wordpress

I'm trying to get category ID of the current archive displayed.
<select id="post-select">
<?php
$posts = get_posts(array(
'category' => get_query_var('cat'),
'numberposts' => -1
));
foreach ($posts as $post) {
echo '<option value="' . get_permalink($post->ID) . '">' . $post->post_title . '</option>';
}
?>
</select>
<script>
document.getElementById("post-select").addEventListener("change", function() {
window.location = this.value;
});
</script>
But the posts of all categories are displayed

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,
)
)
)
);

Display all taxonomy terms in a Select box - ACF Wordpress

I need to display all taxonomy terms in a select box (form) for my search engine.
With my code i can only display 1 but i want to see all.
I need also to display in a first choice the name of the taxonomy
<select name="chambre" class="form-control selectform" data-live-search="true">
<?php
global $post;
$terms = wp_get_post_terms($post->ID, 'chambre');
if ($terms) {
$output = array();
foreach ($terms as $term) {
$output[] = '<option value="' . $term->name_id . '">' . $term->name . '</option>';
}
echo join(' ', $output);
};
?>
</select>

Wordpress dropdown categories as radio fields

I'm trying to make the default wp_dropdown_categories select dropdown as a radio input with submit button.
This is on the front end.
<form id="filter-select" class="filter-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
<?php wp_dropdown_categories(); ?>
<input type="submit" name="submit" value="view" />
</form>
Then in my functions.php I tried to do this
add_filter( 'wp_dropdown_cats', 'dropdown_filter', 10, 2);
function dropdown_filter( $output, $r ) {
$output = preg_replace( '/<option/i', '<input type="radio"', $output );
$output = str_replace( 'class="level-0"', 'name="filter"', $output );
$output = str_replace( "value=\"{$value}\"", "value=\"{$value}\" selected", $output );
return $output;
}
This actually works in swapping out the select dropbox for a radio field. But it does not actually work correctly.
Instead of going to /category/CATEGORYNAME - Like the default select box does.
It goes /?filter=6&submit=view
Any advice is greatly appreciated, thank you.
Try with a custom Walker to output category names instead of IDs.
<div class="radiobuttons">
<?php
$args = array(
'orderby' => 'name',
'hide_title_if_empty' => true,
'title_li' => '',
'walker' => new List_Categories_Radiobuttons
);
wp_list_categories( $args );
?>
</div>
The custom Walker in functions.php:
/**
* Custom Walker to list categories with radio buttons
*/
class List_Categories_Radiobuttons extends Walker_Category {
function start_el(&$output, $category, $depth=0, $args=array()) {
$category_name = esc_attr( $category->name );
$radiobutton = '<input type="radio" name="filtercategory" value="' . $category_name . '">';
$output .= '<div class="radiobutton">' . $radiobutton;
}
function end_el(&$output, $category, $depth=0, $args=array()) {
$output .= "</div>\n";
}
}
Does this point you in the right direction?

How to display subcategories on click categories page wordpress categories

How to display sub categories on category page if category has subcategories else show posts of that categories wordpress category page
$id = $wp_query->get_queried_object_id(); // now you have the ID of the category
Now check if something is returned and do whatever from there:
$args = array('child_of' => $id);
$subcats = get_terms( $args );
if(!empty($subcats)){
foreach($subcats as $subcat) {
echo get_term_link( $subcat->slug, $subcat->taxonomy ); // for example
}
} else {
// do the usual stuff
}
You can try get_categories() with args child_of function like below.
$args = array('child_of' => 'category_id');
$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>';
}
Also, $category->count ===0 then category has no post. we can use it to check that category's post.

Show "No result" when there is no post by the user (WordPress)

I have a custom post type and have a shortcode to make it appear, but I want if there is no post in that custom post, then it should written "No post"...
Here is code. All things are working on but it don't show "No post" when it's empty.
add_shortcode( 'upme_recent_scholarships', 'upme_recent_scholarships',10,2);
function upme_recent_scholarships($display,$id){
global $upme,$upme_options;
$current_options = $upme_options->upme_settings;
$id = $upme->current_view_profile_id;
$post_limit = $current_options['maximum_allowed_posts'];
$feature_image_status = $current_options['show_feature_image_posts'];
$args = array(
'author' => $id,
'order' => 'DESC',
'post_type' => 'scholarship',
'orderby' => 'date',
'posts_per_page' => $post_limit,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
$display .= '<div class="upme-main upme-main-">';
// Display different views based on posts with featured images or posts as text
if ('1' == $feature_image_status) {
while ($query->have_posts()) : $query->the_post();
$image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail');
$image_src = upme_url . 'img/default-post-thumbnail.png';
if (is_array($image_attributes) && ('' != $image_attributes[0])) {
$image_src = $image_attributes[0];
}
$display .= '<div class="upme-field upme-post-list-field">
<div class="upme-post-feature-image"><img src="' . $image_src . '" /></div>
<div class="upme-post-feature-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
} else {
while ($query->have_posts()) : $query->the_post();
$display .= '<div class="upme-field ">
<div class="upme-post-field-type"><i class="upme-icon upme-icon-file-text"></i></div>
<div class="upme-post-field-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
}
$display .= '</div>';
}
return $display;
}
Regards
try this modified script, Please check shortcode parameter, here I have updated correct method.
add_shortcode( 'upme_recent_scholarships', 'upme_recent_scholarships',10,2);
function upme_recent_scholarships($atts){
global $upme,$upme_options;
$current_options = $upme_options->upme_settings;
$display = $atts['display'];
$id = $atts['id'];
$id = $upme->current_view_profile_id;
$post_limit = $current_options['maximum_allowed_posts'];
$feature_image_status = $current_options['show_feature_image_posts'];
$args = array(
'author' => $id,
'order' => 'DESC',
'post_type' => 'scholarship',
'orderby' => 'date',
'posts_per_page' => $post_limit,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
$display .= '<div class="upme-main upme-main-">';
// Display different views based on posts with featured images or posts as text
if ('1' == $feature_image_status) {
while ($query->have_posts()) : $query->the_post();
$image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail');
$image_src = upme_url . 'img/default-post-thumbnail.png';
if (is_array($image_attributes) && ('' != $image_attributes[0])) {
$image_src = $image_attributes[0];
}
$display .= '<div class="upme-field upme-post-list-field">
<div class="upme-post-feature-image"><img src="' . $image_src . '" /></div>
<div class="upme-post-feature-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
} else {
while ($query->have_posts()) : $query->the_post();
$display .= '<div class="upme-field ">
<div class="upme-post-field-type"><i class="upme-icon upme-icon-file-text"></i></div>
<div class="upme-post-field-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
}
$display .= '</div>';
}else{
/*need to add this*/
$display .= '<p>Sorry!, No Record Found</p>';
}
return $display;
}
Please Try This Code
add_shortcode( 'upme_recent_scholarships', 'upme_recent_scholarships',10,2);
function upme_recent_scholarships($display,$id){
global $upme,$upme_options;
$current_options = $upme_options->upme_settings;
$id = $upme->current_view_profile_id;
$post_limit = $current_options['maximum_allowed_posts'];
$feature_image_status = $current_options['show_feature_image_posts'];
$args = array(
'author' => $id,
'order' => 'DESC',
'post_type' => 'scholarship',
'orderby' => 'date',
'posts_per_page' => $post_limit,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
$display .= '<div class="upme-main upme-main-">';
// Display different views based on posts with featured images or posts as text
if ('1' == $feature_image_status) {
while ($query->have_posts()) : $query->the_post();
$image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail');
$image_src = upme_url . 'img/default-post-thumbnail.png';
if (is_array($image_attributes) && ('' != $image_attributes[0])) {
$image_src = $image_attributes[0];
}
$display .= '<div class="upme-field upme-post-list-field">
<div class="upme-post-feature-image"><img src="' . $image_src . '" /></div>
<div class="upme-post-feature-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
} else {
while ($query->have_posts()) : $query->the_post();
$display .= '<div class="upme-field ">
<div class="upme-post-field-type"><i class="upme-icon upme-icon-file-text"></i></div>
<div class="upme-post-field-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
}
$display .= '</div>';
}else{
$display .= '<p>No Post Found</p>';
}
wp_reset_query();
return $display;
}

Resources