I'm having an issue where, even though the page renders originally with "All" selected in the dropdown, if you go to a category (renders fine) and then back to "All" in the dropdown, you see a single post. This is instead of the originally displayed page with all of the categories. I need the page to basically render the same page for "All" regardless. Any thoughts?
wp_dropdown_categories('show_option_all=All&hide_empty=0&show_count=0&orderby=name&echo=0');
I did a similar post on WPSE last week and it seems that the two might be related. For convenience, here is the post
Here is a variation of the code that you use. I'm using get_categories() here to achieve the same goal. I had to adjust my code slightly to make it acceptable for your need.
There are a however other modifications you have to make for this to work. When you select the All Categories option, you will be taken to a page that will display what ever you need to display. This page you will have to manually create
There is no index archive pages in Wordpress as you might know. (Check out this post I have done on the same subject). What this means is, domain.com/category/ returns a 404.
So, to make this all work, you'll have to make a copy of page.php, rename it to something like page-category.php (see the codex on how to create custom page templates), open it up, create your custom query to display what you would like to display when this page is visited
You now need to create your page in the back end. I would suggest that you use the slug category so that when you visit domain.com/category/, this page would be displayed. (Just remember, you cannot create child pages for this page, it will break the hierarchy). I have also made the code to go to domain.com/category/ when All Categories is selected
Apart from that, the code should work fine. You just need to check the URL structures maybe, and also set the parameters in get_categories() to suite your needs. Here is the drop down code.
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo esc_attr(__('Select Category')); ?></option>
<?php
$option = '<option value="' . get_option('home') . '/category/">All Categories</option>'; // change category to your custom page slug
$categories = get_categories();
foreach ($categories as $category) {
$option .= '<option value="'.get_option('home').'/category/'.$category->slug.'">';
$option .= $category->cat_name;
$option .= ' ('.$category->category_count.')';
$option .= '</option>';
}
echo $option;
?>
</select>
EDIT
I actually had an idea here that will come in handy. I've recently done an answer on displaying all categories in a list with all post titles under the specific category. This same idea can be used in your page-category.php template.
When a user selects the All Categories option, they will be taken to this page which will list all categories and post title.
Here is the complete code: (for an explanation of the code, see my post here)
In your functions.php
add_action( 'transition_post_status', 'publish_new_post', 10, 3 );
function publish_new_post() {
delete_transient( 'category_list' );
}
In your template where you need to display your list
<?php
if ( false === ( $q = get_transient( 'category_list' ) ) ) {
$args = array(
'posts_per_page' => -1
);
$query = new WP_Query($args);
$q = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = '' . get_the_title() .'';
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = '' . $category->name . '';
}
$q[$b][] = $a; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
set_transient( 'category_list', $q, 12 * HOUR_IN_SECONDS );
}
foreach ($q as $key=>$values) {
echo $key;
echo '<ul>';
foreach ($values as $value){
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
?>
Related
I have multiple categories on my wordpress page and each of the categories has 1 to n subcategories. If a subcategory contains only 1 single post I would love to display an excerpt of this post, otherwise I'll display a description of the category.
I already have the part with the "normal" categories, but there is kind of a stupid mistake regarding the "single post categories". This is what I have so far:
<?php
$args = array(
'orderby' => 'slug',
'child_of' => $cat_id,
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
$cat_count = get_category($category->cat_ID);
if($cat_count->count == 1) { ?>
<!-- Cat has only one post, display post -->
<?php } else {
<!-- Cat has multiple posts, display cat description -->
}
}
?>
Result is: I am getting the normal categories (fine!) but the first of the "single post categories" multiple times. Something might be wrong with my loop, but I don't see it. Does someone see the mistake?
There are two possible mistakes:
The category is two times in the array (Please try to var_dump it.) -> fixable with array_unique https://www.php.net/manual/de/function.array-unique.php
You forgot an echo of some debug (Somewhere - the first solution should do the trick.)
If the First Solution doesn't fix it, please post the var_dump of the array of categories.
I have a working solution now... finally!
<?php
foreach ( $categories as $category ) {
// If there is only one post available, go directly to the post
if($category->count == 1) {
$all_posts = get_posts($category);
echo '<div class="item"><h4 class="item-title">' . get_the_title($all_posts[0]->ID) . '</h4>Read more</div>';
} else {
echo '<div class="item"><h4 class="item-title">' . $category->name . '</h4>Read more</div>';
}
}
?>
I needed to build a dropdown category filter for my WordPress child theme. Here's what I have in my functions.php:
function blog_category_filter() {
if
( $terms = get_terms
( array
('taxonomy' => 'category', // to make it simple I use default categories
'orderby' => 'name'
)
)
) :
// if categories exist, display the dropdown
echo '<select name="category_filter"><option value="">Filter By Category</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as an option value
endforeach;
echo '</select>';
endif;
}
add_action( 'wp_enqueue_scripts', 'blog_category_filter' );
I call the function in my index.php like so:
<?php blog_category_filter() ?>
The dropdown is displaying twice, once at the very top of the page (where I do not want it to be) and again further down in the body (where I do want it to be).
When I look at the code in Dev Tools, I see the dropdown directly under the body tag, followed by stylesheets, WP scripts, the rest of the HTML, then my scripts. No idea why that is happening, but one catastrophe at a time, amirite?
Is this an enqueue issue? It's the only thing I can think of as I am totally lost on where to start.
I have a category within my Wordpress build that I need to exclude, the category is named 'portfolio' which is being pulled in on another page, but I need the name of that category to be hidden but show any other category names a post is under... I have searched everywhere and found nothing. I also found a plugin but it hasn't been updated for 2 years so I am wary of it.
Assuming the categories are being displayed from within the loop, here is some code that will display all of the categories the post is in, with links to the categories. "Portfolio" should be excluded.
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
if ($category['slug'] != 'portfolio') {
$output .= ''.$category->cat_name.''.$separator;
}
}
echo trim($output, $separator);
}
?>
Just replace your current code for displaying the categories with this. Note that this will not affect you site-wide; you will have to manually replace all instances where categories are displayed.
I have a website made from a template in Wordpress, I've installed a translation plugin (qtranslate).
http://madebysylvie.be/collection
In my "photo album" page when I sort the items in the default language (english) everything works fine, however if I change to another translation (ex.french), the name of the category changes and the tagged items don't appear anymore.
Here is the PHP code that executes that function,
<ul class="filter_portfolio">
<?php
// Get the taxonomy
$terms = get_terms('filter', $args);
// set a count to the amount of categories in our taxonomy
$count = count($terms);
// set a count value to 0
$i=0;
// test if the count has any categories
if ($count > 0) {
// break each of the categories into individual elements
foreach ($terms as $term) {
// increase the count by 1
$i++;
// rewrite the output for each category
$term_list .= '<li class="segment-'.$i.'">' . $term->name . '</li>';
// if count is equal to i then output blank
if ($count != $i)
{
$term_list .= '';
}
else
{
$term_list .= '';
}
}
// print out each of the categories in our new format
echo $term_list;
}
?>
</ul>
I would like to change this block of code in order to identify the tags in the translated version. I know that the trigger is the ("data-value") parameter.
The following code let's me translate the taxonomies from the default language,
function qtranslate_edit_taxonomies(){
$args=array(
'public' => true ,
'_builtin' => false
);
$output = 'object'; // or objects
$operator = 'and'; // 'and' or 'or'
$taxonomies = get_taxonomies($args,$output,$operator);
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {
add_action( $taxonomy->name.'_add_form', 'qtrans_modifyTermFormFor');
add_action( $taxonomy->name.'_edit_form', 'qtrans_modifyTermFormFor');
}
}
}
add_action('admin_init', 'qtranslate_edit_taxonomies');
?>
Thank you very much for helping!
i suspect you use the category name to filter your database records. So in your db there is only the "englsh" name and when you "translate" it your script is unable to work properly since it uses the new "french" name.
Can you try to convert the category name to engish before executing your query?
EDIT
What i actually think is for you to find a way to pass two variables to your php script. Whatever suits your needs.
For example, some possible scenarios are:
- category in english + language (fr) = based on your selected language you query your db and return proper results(based on english work thought)
- category id + language (fr) = same as above but more generic since you use an id to get data from table.
I am guessing you must change somewhat your html and you php script (one to pass correct data and the other to return correct format).
I am having a problem for a while now and I can´t seem to solve it on my own.
I have made a website, this website is multilingual and it was made in wordpress.
In my "photo album" page when I sort the items in the default language (English) everything works fine, however if I change to another translation (ex.french), the name of the category changes and the tagged items don't appear anymore.
http://madebysylvie.be/collection
In my database I manage to find the table and the rows of each category, I want to be able access it in a different language, each one has an unique ID.
I know I have to grab the ID from the database of each category and return it to my PHP script.
This is my code,
<ul class="filter_portfolio">
<?php
// Get the taxonomy
$terms = get_terms('filter', $args);
// set a count to the amount of categories in our taxonomy
$count = count($terms);
// set a count value to 0
$i=0;
// test if the count has any categories
if ($count > 0) {
// break each of the categories into individual elements
foreach ($terms as $term) {
// increase the count by 1
$i++;
// rewrite the output for each category
$term_list .= '<li class="segment-'.$i.'">' . $term->name . '</li>';
// if count is equal to i then output blank
if ($count != $i)
{
$term_list .= '';
}
else
{
$term_list .= '';
}
}
// print out each of the categories in our new format
echo $term_list;
}
?>
</ul>
However I am not good enough to do this on my own, and I would be very happy if someone could help me out on this one.
I don't know how to query my database, and I am not shore how to modify the php in order to print it in the other languages.
Thanks
Please refer to the Wordpress documentation, the "Codex":
Class Reference/wpdb – Interfacing With the Database
Displaying Posts Using a Custom Select Query
Class Reference/WP Query –
Custom Queries – how to modify queries using hooks
But remember »Most of the time you can find the information you want without actually dealing with the class internals and globals variables. There are a whole bunch of functions that you can call from anywhere that will enable you to get the information you need.«
WP_Query example from the docs:
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();