Again: add articles of a category to a page with the same title (3 posts)

  1. piedro
    Posted 6 years ago #


    I used this code

    <?php $listCategory = get_the_title($post_id, 'the_category', true); ?>
    <?php query_posts( 'category_name=' . $listCategory ); ?>

    to list all the articles of e.g. the category "Injuries" on the page named "Injuries". Works great.

    But: I use the german language. So I get some pages named with special characters like "Ɯberraschung". I have a category named "Ɯberraschung" also. The slug shows in both cases the nicename "ueberraschungen".

    But the code above doesn't get the match for these.
    Now the slug shows "ueberraschung" as nicename for both, the page and the category. But the code above doesn't result in the correct list. Instead it shows ALL articles of All categories.

    How do I have to modify the code to match the page title with the category in these cases. Or maybe it would be better to look for a matchup beetween the nicename parts of the slug instead?

    I tried desperately without getting there.

    Any help is very much appreciated.

    thx alot,

  2. MichaelH
    Posted 6 years ago #

    Try this:

    // on a page, get category that has slug equal to the page slug, display posts in that category
    if ( is_page() ) {
      $page_name = $posts[0]->post_name; //or use $posts[0]->post_title
      $category = get_term_by('slug',$page_name,'category');
      if ($category) {
          'category__in' => array($category->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'caller_get_posts'=> 1
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    wp_reset_query();  // Restore global post data stomped by the_post().
  3. piedro
    Posted 6 years ago #

    Hi Michael!

    Thx alot! It took me a while to figure out what your code is doing and how to adapt to my page but now(without complete understanding of your code though ...) - it works!!!

    One sieeffect is I don't have to use
    <?php rewind_posts(); ?>
    anymore. This part I quoted was the second loop on a page! :-)

    One followup question:
    Since there's no "rewind_post" anymore used on the page, is there a way to include the pagination again? ... and where in the code?

    Any ideas?

    But once again:


Topic Closed

This topic has been closed to new replies.

About this Topic