WordPress.org

Ready to get started?Download WordPress

Forums

Get Blog posts by Category - using page slug (3 posts)

  1. joadard
    Member
    Posted 3 years ago #

    Hello,

    I have a blog that has pages that are structured such that I have page content and post content on the same page.

    I have set it up so that I have categories for my posts that correspond to the slug for the relative page so that I can display posts for a particular category that shares the same slug as a page name.

    So far... so good.

    What I'd like to do is have the sidebar content display the following:

    top section - pages and children - so far this is working
    middle section - most recent 7 or so blog postings for category
    bottom section - archives of blog postings for category

    Its the middle and bottom section that aren't working properly.

    On the middle section I have been pulling the category id by doing the following

    $categories = get_the_category($my_query->ID);
    $category_ID = $categories[0]->cat_ID;

    This is pulling the category ID from the category of the posts on the page. Not such a huge problem unless the post is assigned multiple categories. What I'd really like to do is use the category based on the slug from the page to obtain the category id to use.

    I suppose this applies to the archive section as well.

    Is this possible?

    Thanks in advance!

  2. joadard
    Member
    Posted 3 years ago #

    I figured it out... and have listed the code below... - BUT, I am still having an odd issue and am not sure how to resolve it. I am using a custom permalink (/%postname%) which works well. However, when I use this in my test site the archives are working properly and are displaying the correct format in the url - eg. 2010/08?cat=23, but when I try in the live site, even though the URL in the link is shown as 2010/08?cat=23, it redirects to a permalink URL. Is there a setting somewhere that I'm overlooking?

    $post_obj = $wp_query->get_queried_object();
    $post_ID = $post_obj->ID;
    $post_title = $post_obj->post_title;
    $post_name = $post_obj->post_name;
    
    $idObj = get_category_by_slug($post_name);
    $id = $idObj->term_id;
    
    $category_ID = $id;
    
    if (empty($id))
    {
    $categories = get_the_category($my_query->ID);
    $category_ID = $categories[0]->cat_ID;
    }
  3. oudein
    Member
    Posted 3 years ago #

    I have set it up so that I have categories for my posts that correspond to the slug for the relative page so that I can display posts for a particular category that shares the same slug as a page name.

    joadard, how did you get the the pages displaying the category posts? I have been trying to do this for a while with no luck.

Topic Closed

This topic has been closed to new replies.

About this Topic