• Hi All,

    I’m trying to get taxonomies and their descriptions echo’d on a template page that I added. I’ve scoured wordpress for this but for the life of me, I can’t find anything on it. I know it’s out there but I need help.

    My goal is to have a page called “Categories” which is linked on my main menu. Once clicked, it’ll take you to a page that lists every single category that has been used on my website (including their descriptions). They should link to their respective archive.

    I would surely appreciate any help anyone could offer.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter harshclimate

    (@harshclimate)

    I seriously doubt anyone will ever respond to this topic but I’ll do some typing out loud:

    My main goal was to display all categories that I have added to every single post. What I didn’t mention, was that I wanted to display all categories that I have not assigned to a post and that just isn’t possible I don’t think. I mean, how can you display a category if it hasn’t been assigned to anything, right? I didn’t quite think that one through before I posted on wordpress.

    After searching around on the internets, I’ve discovered that what I really wanna do is display all posts according to the category. There’s tons out there on that one. But the hitch is, I would love to be able to display the DESCRIPTION of the category under the category name. Then display all the posts under that.

    Anyway, thanks for any thoughts on this one…

    Moderator bcworkz

    (@bcworkz)

    I’m sorry you feel like your posts are being ignored. It can take quite some time to get a response in these forums, no one responds unless they feel like their response will be helpful. You just need to be patient until the right person finds your post 🙂

    I don’t know which of the tons of approaches for displaying posts by category you would want to use. I would suggest one that uses a custom page template. The code on this template is capable of doing anything that is possible with PHP.

    Such an approach would loop through the different categories. In each iteration, if the term ID is not already available in a variable, it can be determined from the term name or whatever is used to drive the loop. Once you have the term ID, you can output the description with echo term_description( $term_id, 'category' );

    Thread Starter harshclimate

    (@harshclimate)

    Thanks, bcw. Yeah, I didn’t realize that you couldn’t echo terms that were not assigned to a post. That’s kind of what confused me at first.

    Now I’m trying to add an edit category link from the list of categories. My php experience is in the negatives. All I really know is <?php echo “sup”; ?>.

    What I was able to copy and use on my page-categories.php is this bit of code:

    <?php
    					echo "\n" . '<div class="row">';
    						$categories = get_categories( array(
    							'orderby' => 'name',
    							'order'   => 'ASC'
    						) );
    						foreach( $categories as $category ) {
    							$category_link = sprintf( '<a href="%1$s" alt="%2$s">%3$s</a>',
    								esc_url( get_category_link( $category->term_id ) ),
    								esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
    								esc_html( $category->name )
    							);
    
    							echo '<div class="col span-12">';
    
    								echo '<span>' . sprintf( esc_html__( '%s', 'sts' ), $category_link ) . '<span class="beenused">' . sprintf( esc_html__( 'has been used %s times', 'sts' ), $category->count ) . '</span>' . '</span>';
    								echo 	'<p>' . sprintf( esc_html__( '%s', 'sts' ), $category->description ) . '</p>';
    
    							echo '</div>';
    						}
    					echo '</div>';
    				?>

    So somewhere in that mess I’m trying to spit out:

    <?php
        // Get the ID of a given category
        $category_id = get_cat_ID( 'Category Name' );
    
        // Get the URL of this category
        $category_link = get_category_link( $category_id );
    ?>
    
    <!-- Print a link to this category -->
    <a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">Category Name</a>
    Thread Starter harshclimate

    (@harshclimate)

    Here’s the page where I’m trying to echo the edit link. I’m hoping right at the end of the category count:

    SaturdaythruSunday.com

    Hi harshclimate,

    If you want to display all categories regradless they are assigned to any posts or not, then in your function “get_categories”, you can pass this parameter

    'hide_empty' => false

    and it will list all categories.
    Also, i can see on this page the categories are having description as well, have you figured that out?

    Thanks,
    Swayam

    Moderator bcworkz

    (@bcworkz)

    For an edit link, add the following between the count output and description output:
    echo " <a href=\"http://saturdaythrusunday.com/wp-admin/term.php?taxonomy=category&tag_ID={$category->term_id}\">Edit</a>";

    You may want to put this inside a conditional so it is only output for admin users.

    Thread Starter harshclimate

    (@harshclimate)

    Thanks you guys.

    Yeah, swayam, the descriptions are there because I wanted them to be but I should maybe shorten them so it doesn’t take up too much of the page. I’m currently redesigning that whole area now.

    bcw, thanks for the edit link! I’ll plug that in shortly. I think the current php that is there now will only show up if an admin is logged in, isn’t it?

    Moderator bcworkz

    (@bcworkz)

    I don’t think so. There is no evidence of such in the snippet you posted, though the conditional could have been prior to the snippet. Additionally, I believe I saw the snippet’s output on your site, and I was certainly not logged in as anything, much less as admin 🙂

    Thread Starter harshclimate

    (@harshclimate)

    Yikes, well I’m glad you were able to catch that. Thanks! I’m trying to kind of redesign that page a little bit. I don’t really see the use in my having an edit link on each one. Obviously when I’m logged in I can just right to it too.

    I’m only decent at html/css. When it comes to php and creating conditional statements… well… let’s just say the only work-arounds I can really come up with is just eliminating the idea if I can’t see an obvious fix for it.

    For some reason, php and me don’t mix. As much as I love what php is and can do, I just could never grasp it. The more I mess around with wordpress, maybe the better chance I have at understanding it a little more 🙂

    Moderator bcworkz

    (@bcworkz)

    I can relate, I’m the other way around. I’m comfortable with PHP (not an expert by any means), but CSS and I do not get along at all. My CSS strategy is keep trying different things until I stumble upon something that works.

    On your template, you can use a conditional like so:

    if ( current_user_can('manage_categories')) {
       //code here only executes for editors and admins
    }

    You could replace ‘manage_categories’ with some other capability, depending on which roles you want to enable the edit link for. Default capabilities for each role are listed in Roles and Capabilities. In this case ‘manage_capabilities’ makes sense, but the capability you check for doesn’t necessarily need to relate to the content you are restricting.

    Thread Starter harshclimate

    (@harshclimate)

    Conditional worked like a charm. Now, I tried adding in that link you had there for edit term but I used it in a post edit link instead so I changed up the verbiage and code but wasn’t working (of course) so I did a little interweb searching and found just a plain ole edit_post_link(). It’s a little crude in that it says “edit this” by default but I can live with that 🙂

    I’m a little addicted to logging out and logging back in just because it keeps disappearing and reappearing lol – MAGIC!

    Moderator bcworkz

    (@bcworkz)

    😀 Yes, it’s very cool when your coding efforts actually do what you want!

    You can change the “Edit this” to something else by passing the desired text as a parameter to edit_post_link(). Let’s say you’d rather have it say “Alter Post”, just do this: edit_post_link('Alter Post');

    If you look at the page’s HTML source, you can see the edit link anchor has the class ‘post-edit-link’, so you can style the link to appear as a button or something else with CSS.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Displaying Taxonomy and Descriptions’ is closed to new replies.