Can't get the categories to show - php

I am working off the TwentyTwelve theme and I have modified the index file by adding this snippet before the loop
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main" class="clearfix">
<?php
$terms = get_the_category();
$count = count($terms);
echo '<ul id="post-filter">';
echo '<li>All</li>';
if ( $count > 0 ){
foreach ( $terms as $term ) {
$termname = strtolower($term->name);
$termname = str_replace(' ', '-', $termname);
echo '<li>'.$term->name.'</li>';
}
}
echo "</ul>";
?>
<div id="mwrapper">
<?php query_posts('cat=-6,-7'); ?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="box">....
I'm trying to create a filter that will filter through the blog post. Like the demo here. Currently I have five categories: Agency Notes, Design Notes, Featured, Humor, Uncategorized. And there's at least one post per category but it seems to be pulling in Design Notes only.
I have also tried changing the get_the_category(); to wp_list_categories(); but that ended up showing all the categories.
Source I'm getting the snippet from.

get_the_category() grabs the current post's category/ies information, not the list of categories in the full WP installation.
I think what you're looking for is the get_categories() function (more info here at the codex: http://codex.wordpress.org/Function_Reference/get_categories)
<?php
$categories=get_categories( array( 'order' => 'ASC', 'orderby' => 'name' ) );
$count = count($terms);
[...]

First off, you want to get all categories. get_the_category() does not do this. You probably want get_categories() instead.
$terms = get_categories();
$count = count($terms);
echo '<ul id="post-filter">';
echo '<li>All</li>';
if ( $count > 0 ) {
foreach ( $terms as $term ) {
echo '<li>'.$term->name.'</li>';
}
}
echo "</ul>";
I also made a few modifications: removed the hash and rel attribute. We can use data-attributes instead, which are more semantic.
The next part depends on your post HTML, but I'm assuming they have a class of post and the category they're in. If they do, you can do something like this with jQuery:
$('a', '#post-filter').on('click', function(e) {
e.preventDefault();
$('.post').hide().filter('.' + $(this).attr('data-slug')).show();
});
Which will hide all posts, and only show the ones in the selected category. I'll leave it to you to sort out the animations.

Related

Insert content after first post in WordPress loop

I want to display a list of categories after the first post in the Loop of index.php (this is the template my WP theme uses to display posts).
I've searched around on the web and found some code (see below) which is supposed to do as I want - inject a list of category titles as links between a list of posts in the Loop.
However, it is not working as expected. It only shows one category title, not all of them. Interestingly, it displays the title of the first post's category (the post that comes before the custom code), but no others.
My Loop code, including the custom code I inserted, is as follows:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('content'); ?>
// START CUSTOM CODE
<div>
<?php
if( $wp_query->current_post == 0 ) {
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
}
?>
</div>
// END CUSTOM CODE
<?php endwhile; ?>
Hoping someone can help.
Thanks,
Mekong
It is a little unclear to me from your question, but it seems like you want a list of all categories, correct? I think the line "$categories = get_the_category();" is getting categories for the current (in this case first) post only.
If you want a list of all categories that exist in your blog/website try 'get_categories', https://developer.wordpress.org/reference/functions/get_categories/
Try this code, small change in your code...
<?php if (have_posts()) : $i = 1; while (have_posts()) : the_post(); ?>
<?php get_template_part('content'); ?>
<div class="categories">
<?php
if( $i == 1){
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0
) );
foreach ( $categories as $category ) {
printf( '%2$s<br />',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
}
?>
</div>
<?php $i++; endwhile; ?>

php generate links to all wordpress categories

Im having a little problem. I want to display all categories connected to a wordpress post.
I found several codes and plugins, but all of them have problems. Some dont display correct anchor text, others dont link to correct url.
So i took the working parts from different places and put them together to the code below, and its working.
The problem is that it does not generate links to ALL connected categories, it only show link to 1 single category (first one that was connected to the post)
what is wrong with the code ? Why will it not show links to all connected categories. I have been working on it for 2 days now, and i give up.
<?php
$the_cat = get_the_category();
$category_name = $the_cat[0]->cat_name;
$category_link = get_category_link( $the_cat[0]->cat_ID );
?>
<?php
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>
<a class="button" href="<?php echo get_category_link( $category_id ); ?>"title=”<?php echo $category_name ?>” ><?php echo $category_name ?></a>
Please try this code
<?php
$category_args_query = array(
'orderby' => 'name',
'parent' => 0,
'hierarchical' => 1,
'number'=> 10
);
$categories = get_categories( $category_args_query );?>
<ul>
<?php
foreach ( $categories as $category )
{ ?>
<li><?php echo $category->name; ?> </li>
<?php
}
?>
</ul>
If you want to show the category that is related with the posts on post details page and archive page add this code in your child theme functions.php and call the function where you want to display it.
function postsCategories()
{
$categories_list = get_the_category_list( esc_html__( ', ', 'your-text-domain' ) );
$allowed_tags_before_after=array('span' => array('class'=>array()),'i'=>array('class'=>array()),'a'=>array('class'=>array(),'href'=>array(),'rel'=>array()));
if ( $categories_list )
{
printf(wp_kses(sprintf(__('<span class="blogmeta cat-links"> <i class="fa fa-folder-open"></i> %1$s </span>','your-text-domain'), $categories_list ),$allowed_tags_before_after));
}
}
Call in the template : <?php printf(__('%s','your-text-domain'),postsCategories()); ?>
Actually just <?php the_category(', ') ?> should be enough (within the WP while loop). The , determines what should go in-between, if there is more than one category.

Exclude page from a wordpress sitemap

I came across a tutorial on the internet for creating a sitemap in WordPress. It does what I want - it lists all pages and posts on the website however I was wondering if it was possible to exclude a page from the sitemap. In this case I want to exclude the sitemap link. Is it possible to do this? I have included the code for the sitemap below.
<?php
/*
Template Name: Sitemap
*/
get_header(); ?>
<?php if ( have_posts() ) : while( have_posts() ) : the_post();
the_content();
endwhile; endif; ?>
<h2>Pages</h2>
<ul>
<?php
// Add pages seprated with comma[,] that you'd like to hide to display on sitemap
wp_list_pages(
array(
'exclude' => '',
'title_li' => '',
)
);
?>
</ul>
<h2>Posts</h2>
<?php
// Add categories seprated with comma (,) you'd like to hide to display on sitemap
$cats = get_categories('exclude=');
foreach ($cats as $cat) {
echo "<ul>";
query_posts('posts_per_page=-1&cat='.$cat->cat_ID);
while(have_posts()) {
the_post();
$category = get_the_category();
// Only display a post link once, even if it's in multiple categories
if ($category[0]->cat_ID == $cat->cat_ID) {
echo '<li>'.get_the_title().'</li>';
}
}
echo "</ul>";
}
?>
<?php get_footer(); ?>
I am aware that the comments within the code give me some idea as to where to list the pages I want to hide however I am unsure as to how to do this. If anyone can help me it would be much appreciated.
Many Thanks.
If you want to exclude pages from your Site Map you have to write here
wp_list_pages(
array(
'exclude' => 'ID1, ID2',
'title_li' => '',
)
);
ID1 and ID2 it's your page id on WordPress.

Show category name above category posts

I'm trying to get the category name to show above posts. I'm customizing bones and am a little stuck.
Here's the code that has my posts displaying, and I have each in a category in the wp admin. I'm just not sure how to get the category to display above the appropriate posts.
Hope this makes sense. Thanks in advance!!
<?php
$args = array( 'post_type' => 'custom_type', 'posts_per_page' => 100 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php } else { ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php } ?>
It looks like you are doing a custom loop (as opposed to "The loop"), hence you'll need to pass the id of the post to get_the_category. However, judging by the docs, it seems that WP_Query->the_post() sets the global post.
So, if this does not work:
<h2><?php echo get_the_category() ?></h2>
You would do:
$postsQuery = new WP_Query( $args );
$posts = $postQuery->get_posts();
foreach ( $posts as $i => $e ) {
$category = get_the_category($e->ID);
}
https://developer.wordpress.org/reference/functions/get_the_category/
https://codex.wordpress.org/Class_Reference/WP_Query
https://codex.wordpress.org/The_Loop
You can use like <?php the_category();?>. This will returns the categories name with link. Hope this help you!
Hi I could not post in comments, but if above answer is working for you as you posted in comments but having issue with getting category as it give array due to many categories of a post, so you need to fetch first category from the array. Use below code snippet:
if ( ! empty( $category ) ) {
echo '' . esc_html( $category[0]->name ) . '';
}

Wordpress Loop - Show posts from custom taxonomy terms in heirarchical order

ok, here's what I'm trying to do:
I've got a custom post type called drinks-menu, a taxonomy called drinks-menu-categories, and a page template called template-drinks-menu.php.
The taxonomy has a bunch of terms that are heirarchical - Wine, with children White and Red; Beer, with children Pilsener, Stout, etc...
I want to use one loop to display all the posts from these terms in the same order that they're ordered by in the admin. Here's the code I've got so far:
<?php
$post_type = 'drinks-menu';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array('post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
echo '<h1>'.$term->name.'</h1>';
if ( $term->description !== '' ) {
echo '<div class="page_summary">'. $term->description .'</div>';
}
echo '<br>';
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=-1&orderby=id&order=DESC" );
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
?>
<?php the_title(); ?>
<?php
if( get_field( "price" ) ): ?>
<?php echo '<span">'; the_field( "price" ); echo ' | '; the_field( "abv" ); echo '</span>';?>
<?php endif;
echo '<em>'; the_excerpt(); echo '</em><br><br>';
endwhile; endif;
endforeach;
endforeach;
?>
This is working well to bring all the posts from the terms onto the page, but not in order. You can see I tried taxonomy=$taxonomy&term=$term-slug&posts_per_page=-1&orderby=id&order=DESC but it's not working, everything shows in alphabetical order.
Any Wordpress gurus who can point me in the right direction? I hope I've been clear about the issue. Thanks :-)
Posts ordered in admin page by "menu_order";
If u want to order posts using query_posts (WP_Query constructor) u should use value of variable "orderby" in upper case -> "ID".

Categories