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.
Related
I'm building a wordpress theme, but got stuck on a problem. It's on the blog archive page. When you go to the blog, you see a list of blog posts. And on each post preview, you see the author, title, et cetera. My problem is that I'm trying to display one category for each post preview. I don't want to show people an entire list of every category a post has. I want to limit the results of the_category() to ONE.
You could do something like this:
$category = get_the_category(); echo $category[0]->cat_name;
However, that will just return the first category name on the list. Instead, I'd suggest using a plugin to set the primary category.
For instance, with WP Category Permalink, (possibly outdated) you can get the primary category like this:
<?php
$perma_cat = get_post_meta($post->ID , '_category_permalink', true);
if ( $perma_cat != null && is_array($perma_cat)) {
$cat_id = $perma_cat['category'];
$category = get_category($cat_id);
} else {
$categories = get_the_category();
$category = $categories[0];
}
$category_link = get_category_link($category);
$category_name = $category->name;
?>
<?php echo $category_name ?>
i want to display multiple categories with their post counts with help of short code without plugin.
i found many functions code here but didn't work with multiple categories with shortcode.
how to create function that is use to display all categories post counts using shortcode.
Example:
category 1: 20 posts
category 2: 10 posts
category 3: 15 posts
the display results 20,10 or 15 should be shown by help of shortcode.
This is a basic way to show a list of categories with their post counts. You could use other objects to link to the category pages etc. From what you described you just need a list, but this is the foundation.
function category_post_count() {
ob_start(); //php output buffer
$categories = get_categories(); ?>
<ul>
<?php foreach($categories as $cat) { ?>
<li><?php echo $cat->name . ': ' . $cat->count; . ' posts '; ?></li>
<?php } ?>
</ul>
<?php $endBuffer = ob_get_clean();
return $endBuffer;
}
add_shortcode( 'category_post_count', 'category_post_count' );
Then all you need to do is add this to the area where you want it to show up on the page...
[category_post_count]
I'm trying to print only the parent category for each post and a permalink to that category within a custom Wordpress RSS feed.
Using <?php the_category_rss(); ?> prints all categories for a post (parent and child categories) like this:
<category><![CDATA[Category 1]]></category>
<category><![CDATA[Category 2]]></category>
<category><![CDATA[Category 3]]></category>
I'd like to print only the parent categories for a post rather than parent and child categories.
I'm also looking for the proper way to print the parent category's permalink within the RSS feed.
I have a feeling the way to do this is a filter, but I'm not sure how to implement it. This is what I have right now, but it returns a 1 instead of the parent category name:
add_filter('the_category_rss', 'only_parent_rss_categories');
function only_parent_rss_categories( $allparents ) {
$categories = get_the_category();
$category = $category->category_parent == 0;
$allparents= esc_html("<category><![CDATA[{$category}]]></category>");
return $allparents;
}
I'm still interested in hearing how this can be accomplished using the Wordpress function the_category_rss(), but I did find another way to do this:
<?php $parentsonly = "";
// loop through categories
foreach((get_the_category()) as $category) {
// exclude child categories
if ($category->category_parent == 0) {
$parentsonly = '<category domain="' . get_category_link($category->cat_ID) . '">' . $category->name . '</category>';
}
}
echo $parentsonly; ?>
This returns each parent category in the proper RSS validated format, using <category> and domain for the category URL:
<category domain="http://www.yourdomain.com/category-archive/">My Category</category>
Hope this helps someone else!
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>';
}
?>
I'm trying to make a theme which shows an overview of child categories with title, link and description when entering a category archive. However, I only want to show child categories one level below the current category, and not the children of child categories.
How do I do that?
<?php
global $ancestor;
$childcats = get_categories('child_of=' . $cat . '&hide_empty=1');
foreach ($childcats as $childcat) {
if (cat_is_ancestor_of($ancestor, $childcat->cat_ID) == false){
echo '<li><h2><a href="'.get_category_link($childcat->cat_ID).'">';
echo $childcat->cat_name . '</a></h2>';
echo '<p>'.$childcat->category_description.'</p>';
echo '</li>';
$ancestor = $childcat->cat_ID;
}
}
?>
I found that code, but it only returns one child. It returns. (Faa and Faq are child categories)
Faa
Faq -- Not displayed
Thanks!
Make sure 'FAQ' has post under it. If you notice the "code you found" is passing the parameter "&hide_empty=1" which means it will not return categories that are empty.
So your options are to either remove that or make sure your category has posts under it.