• Resolved arask2

    (@arask2)


    I’d like to create a custom page template that when executed does the following.

    1) Looks up the page slug value.. e.g. cardiac-anatomy

    2) Determines if there is a category that matches the page slug e.g. category called cardiac-anatomy.

    3) If so, loads all the posts for that category and displays them on the page.

    In effect, a page of posts from a specific category, where in , the category matches the page name.

    Your help is greatly appreciated.

    Cheers,
    Kedar

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter arask2

    (@arask2)

    So I have create a custom template called Page-Category.php and am including the following code for each step.

    1) Look up page slug value
    global $post;
    $page_slug = get_post( $post )->post_name;

    2) Determine if there is a category match
    foreach(get_the_category() as $category) {
    if ($category->slug == $page_slug) {
    $match = true;
    break;
    }
    }

    3) Load the posts for the given category
    if ($match) {
    $cat_posts = get_posts(“category_name=$page_slug”);
    while ( have_posts() ) : the_post();
    get_template_part( ‘content’, get_post_format() );
    endwhile;
    }

    What am I missing?

    Thread Starter arask2

    (@arask2)

    This topic has been resolved.

    What was your solution?

    Thread Starter arask2

    (@arask2)

    I had originally created pages, so that i could link them to the menu options. Given that, it made sense to try and attach each page to a specific category or more than one, if needed, so that they would be displayed accordingly.

    After doing a little more research, I realized the wordpress engine is not designed for that. If I wanted to accomplish the same within the framework of the loop, the best way to approach it would be to map menu links to categories directly. In effect, I am getting the same thing, just without the need for a page.

    Is there a way, to extract posts for a given category and display them on the page? I imagine so, but that is convoluted way of doing things, that word press architecture was not designed to do.

    Pages are for static content. Categories are pages for posts. Trying to make one look like the other is not recommended and overtly complicated, as I found out.

    Bottom line, I found the solution within the framework of word press architecture, by mapping categories to menu items on my website.

    Hope that helps.

    Cheers,
    K

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Page of Posts Question’ is closed to new replies.