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.
Related
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; ?>
I'm building a Wordpress site right now and I'm trying to create a tab structure on a single page where I can create a sort of sub-menu and display 4 different custom-post-type loops, depending on what menu item is selected. I've been able to get the nav-items block consistent with a foreach loop, and the content blocks are switching correctly, but right now, each content div is displaying all the posts of every custom-post-type that I have.
I've experimented with switch statements and if statements as well as the is_singular conditional tag, but nothing is working quite right thus far
I've put comments in the code to delineate for myself what each section is doing, thought they might help you get into my brain space.
<ul class="tab" role="tablist">
<?php
//All public posts that are not built into Wordpress
$argsnav = array(
'public' => true,
'_builtin' => false,
);
$output = 'objects'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
//Get the Post Types of each post as an object
$post_types = get_post_types( $argsnav, $output, $operator );
//Remove Product from Array
unset( $post_types ['product'] );
//Iterate through each post type
foreach ($post_types as $post_type) {
//And print out a list item with the corresponding ID and text
echo '<li>
<a href="#' . $post_type->name . '" role="tab" data-toggle="tab">
'. $post_type->label .'
</a>
</li>';
}
echo '</ul>';
?>
//New section for post content/ This is the buggy portion
<div class="tab-content">
<?php
//Iterate through each post type
foreach($post_types as &$post_type) { ?>
<!--Create div with corresponding id-->
<div class="tab-pane" id="<?php echo "$post_type->name" ?>">
<!--Create new query for all posts of each type-->
<?php $loop = new WP_Query(
array(
'post_type' => array('standup', 'novels', 'short_stories', 'music'),
));
//Loop through and display each post's title
if ( $loop->have_posts()) :
while ( $loop->have_posts() ) :
$loop->the_post(); ?>
<h2><?php the_title(); ?></h2>
<!--Stop the loop-->
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php } wp_reset_postdata(); ?>
</div>
I think I know what needs to be done, but HOW is where I'm stuck. Any help is very much appreciated!
As I feel, you do not need to set all post_type in wp_query, please try this
<!--Create new query for all posts of each type-->
<?php $loop = new WP_Query(
array(
'post_type' => $post_type->name,
));
//Loop through and display each post's title
Here is the problem:
I've been using this code to get other posts to display in the end of the page (as a list), and those posts should be in the same category as the post that a visitor is currently reading. It works fine, but the problem that occurs is when the post is assigned to multiple categories, then I have multiple listings which is not needed.
How can I restrict this , to just one category (by name, or by id or the first one), so that the loop shows me posts from just one category when post with multiple categories is displayed?
Many thx.
<?php global $post;
$categories = get_the_category();
foreach ($categories as $category) :
?>
<h3 class="naslovostalih">Ostali članci iz ove kategorije:</h3>
<ul class="clanciostalih">
<?php
$posts = get_posts('numberposts=10&category='. $category->term_id);
foreach($posts as $post) :
?>
<li>
<?php
$title = get_the_title($ID);
$link = get_permalink();
printf('<a class="linkpost" title="%s" href="%s">%s</a>', $title, $link, $title);
the_post_thumbnail('thumb-232');
echo '<div id="excerptcu">';
echo excerpt(25);
echo '</div>';
?>
<p class="more-link-wrapper2"><?php _e( 'Opširnije »', 'fearless' ); ?></p>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
You can add the category id to get_posts just as in your code
Instead of looping the categories try like below: here 3 is the category id
$posts = get_posts('category=3&numberposts=10');
Also if you want to retrieve the category id of specific category by name you can use
get_cat_ID( $cat_name )
Perhaps you could just take the first category in your foreach loop instead of iterating through all of them? You can do this by setting a variable like
$first = true;
Before your loop. Then, set your foreach loop in an "if" that checks to see if $first == false. If so, do the loop. In your loop, do your stuff and set $first = true and it will only iterate once.
I'm new to PHP and WordPress.
I'm using Types plugin for creating my custom types (more info at here).
Here is my code:
<?php
$args = array('post_type' => 'project-list');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
?>
<span><?php echo $post -> post_title; ?></span>
<ul class="project-list">
<?php
$argsChild = array('orderby' => 'post-order', 'order' => 'ASC');
$mypostsChild = types_child_posts('project', $argsChild);
foreach ( $mypostsChild as $postChild ) :
?>
<li>
<p class="project-cost"><?php echo $postChild -> post_title; ?></p>
<span class="project-name"><?php echo $postChild -> fields['project-name']; ?></span>
<hr />
<p class="project-type"><?php echo $postChild -> fields['project-type']; ?></p>
<hr />
<span class="project-location"><?php echo $postChild -> fields['project-location']; ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
What I expect:
I need to load the list of projects by their type. For example I need the final output to look like this.
Project Group Name-1
proj-1
proj-2
proj-3
Project Group Name-2
proj-1
proj-2
proj-3
I've two problems here:
1: I don't know where to put the parent post "ID", and this code is now rendering me all the projects from all the project lists. I need to load projects separately for every project list (like above).
Q: How can I do this?
2: I can't sort the posts by their post-order (which is a custom field type). Thus, I noticed that the following part of the code is not working proper.
$argsChild = array('orderby' => 'post-order', 'order' => 'ASC');
Q: How can I sort the child posts based on a custom field type, which is "post-order" in my case?
Thanks guys!
Posts haven't any parent or child. Posts are created under categories. You should create some categories and subcategory under category. i.e.
CATEGORY :Project Group Name-1
- SUBCATEGORY: pro 1
- SUBCATEGORY: pro 2
CAT:Project Group Name-2
- SUBCATEGORY: pro 1
- SUBCATEGORY: pro 2
Moreover, you have created custom post type. There are many ways to retrieve the terms of custom post types depending your spec. However, try the following code to make some sense:
<?php
//list terms in a given taxonomy
$taxonomy = 'project-list';
$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>
Learn more about custom post type , terms and taxonomy. And read this link, you will get more codes what is related to your need. these will help you.
FYI: page has parent or child page
I have a function going that displays all posts under the same custom taxonomy called "issue". I need to adjust it so that it further narrows it down to also only display posts under the same category.
I took a look at the WordPress get_the_category() function but didn't have much luck with that.
Here is the code:
<?php
$issueid = get_the_term_list( $post->ID, 'issue', '', ', ', '' );
$postslist = get_posts("numberposts=100&issue=$issueid");
foreach ($postslist as $post) :
setup_postdata($post); ?>
<div class="sidebar-box">
<div class="sidebar-left">
<p><?php the_title(); ?></p>
<p><?php the_date(); ?></p>
</div>
<div class="sidebar-right">
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
</div>
</div>
<?php endforeach; ?>
This will properly show the current category id:
<?php
$category = get_the_category();
echo $category[0]->cat_id;
?>
So I tried editing the first batch of code to only show posts within the current category id but it still returns everything:
$category = get_the_category();
$categoryid = $category[0]->cat_id;
$issueid = get_the_term_list( $post->ID, 'issue', '', ', ', '' );
$postslist = get_posts("numberposts=100&issue=$issueid&category=$categoryid");
foreach ($postslist as $post) :
setup_postdata($post); ?>
This is the get_the_category function reference: http://codex.wordpress.org/Function_Reference/get_the_category
Any help would be greatly appreciated.
Thanks,
Wade
get_the_term_list() returns an html string, not ID's of related categories. So when you pass $issueid to get_posts(), you are including an html string, not an integer. I believe the reason you are getting all posts returned is because WP is ignoring that query var because it isn't what its expecting.
You want to use get_posts() and include the ID for 'issue' to get all posts assigned the category of 'issue'.
You want to use get_the_category() to get all categories related to a post.
Can you clarify if you want to show all posts under the same categories as the current post which is under the category 'issue'? Do you want to list the related posts right after the current post, or do you want to display ALL the related posts to ALL the 'issue' posts in the sidebar?