WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to get category ID? (9 posts)

  1. migbrasil
    Member
    Posted 5 years ago #

    hello, so im gonna use this plugin in my sidebar which gets the posts of a specific category. something like this: <?php wp_cat_posts(3); ?> where 3 is the category id, the thing is i cant get to work /;

    i wanted like, <?php wp_cat_posts(get_post_category); ?>
    can somebody please help me?

    Thanks

  2. tomontoast
    Member
    Posted 5 years ago #

    To find a categories ID go to /wp-admin/categories.php and click on one of the category names. The url of the page you are on will show what the category id is. eg wp-admin/categories.php?action=edit&cat_ID=11 means the category is 11

  3. migbrasil
    Member
    Posted 5 years ago #

    yeah, but i mean, is there a php code that do that by itself? because i want to plac eon my sidebar so the categories are going to be different for some posts so i wanted like a php code that gets the posts category id....

    im sorry about my english ;p i know its hard to understand

  4. migbrasil
    Member
    Posted 5 years ago #

    never mind i found a plugin that did my job =]
    if anyone has the same question just get this plugin
    http://picandocodigo.net/programacion/wordpress/list-category-posts-wordpress-plugin-english

    you can follow the instructions he gave it or if you want to post on your sidebar or templete or something I used:

    <div class="menupost"><?php $result = "<ul class='lcp_catlist'>";
    $catposts = get_posts('category='.$ID."&orderby=title&order=asc&numberposts=".$NUMBEROFPOSTS);
    foreach($catposts as $single):
    	$result .= "<li><a href='";
    	$result .=get_permalink($single->ID)."'>".$single->post_title."</a></li>";
    endforeach;
    $result .= "</ul>";
    echo $result; ?></div>

    you may change, or add more on the part where it says &orderby=title&order=asc& just go to the plugin website that he gave all the stuff you can use....

    sorry for my english.... tried to help =] buh bye

  5. tomontoast
    Member
    Posted 5 years ago #

    Ok, I understand now. This piece of code might help you:

    $cat_name='cars'; //for example
    $categories=get_the_category(); //get the categories
    foreach($categories as $category){ //loop through categories
    	if($category->name==$cat_name or $category->slug==$cat_name){ //if category slug or name = $cat_name
    		wp_cat_posts($category->term_id); //gets post of that category
    		break; //end loop
    	}
    }
  6. migbrasil
    Member
    Posted 5 years ago #

    oh mann :/
    i thought i had solved my problem but i didnt :/
    so i go this code

    <div class="menupost"><?php $result = "<ul class='lcp_catlist'>";
    $catposts = get_posts('category='.$ID."&orderby=title&order=asc&numberposts=".$NUMBEROFPOSTS);
    foreach($catposts as $single):
    	$result .= "<li><a href='";
    	$result .=get_permalink($single->ID)."'>".$single->post_title."</a></li>";
    endforeach;
    $result .= "</ul>";
    echo $result; ?></div>

    where it says '.$ID." i need to replace with something that can get the post category's ID. =[ is there a way?

  7. t31os
    Member
    Posted 5 years ago #

    Try this...

    <?php foreach(get_the_category() as $category)
    { $thecat = $category->cat_ID; } ?>
    <div class="menupost"><?php $result = "<ul class='lcp_catlist'>";
    $catposts = get_posts('category='.$thecat."&orderby=title&order=asc&numberposts=".$NUMBEROFPOSTS);
    foreach($catposts as $single):
    	$result .= "<li><a href='";
    	$result .=get_permalink($single->ID)."'>".$single->post_title."</a></li>";
    endforeach;
    $result .= "</ul>";
    echo $result; ?></div>

    That work?

  8. migbrasil
    Member
    Posted 5 years ago #

    WOOOOOOOOOOOOOOOOOOW
    it worked! THANKSSSSSSSSSSSSS O//////
    thank you thank you thank you =D
    u the best!!!

  9. t31os
    Member
    Posted 5 years ago #

    Just pinched an example from here and then plonked it into your code...

    http://codex.wordpress.org/Template_Tags/get_the_category

    :) Glad it works for you.. ;)

Topic Closed

This topic has been closed to new replies.

About this Topic