0

I'm dynamically generating my category post by getting the Page title and match it with the category name. The category I'm posting sometimes has a subcategory now I need to separate this subcategory by groups. I'm using this code.

<ul>
<?php 
    global $post;
    $post_slug = get_the_title(); 
    $args = array ( 'category_name' => $post_slug, 'posts_per_page' => -1, 'orderby' => title, 'order' => ASC);
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post); ?>
    <li>
        <strong><?php the_title(); ?></strong>
        <?php the_content(); ?>
    </li>
<?php endforeach; ?>
</ul>

I need to call my category only by using the page title. since I'm using a template that post different category and controlling it by name title of the page. Not quite sure if I can convert that to category => ID

Please see link here for the explanation and mock

Found this thread not sure if its going to fit on what I did? also this one

4
  • Do not cross post please Commented Jul 11, 2016 at 5:45
  • @PieterGoosen what do you mean?
    – MIke
    Commented Jul 13, 2016 at 3:58
  • Exactly what I'm saying, do not post your question across multiple stacks Commented Jul 13, 2016 at 8:11
  • Did you try any of the solutions in the links you cite? What happened?
    – dg4220
    Commented Jul 30, 2016 at 19:33

3 Answers 3

1

You can use get_term_by to get a category by name-

$category = get_term_by( 'name', $post_slug, 'category' );
echo $category->term_id;
3
  • Just a note, this is not reliable as you can have duplicate term names across the entire hierarchy is a given hierarchical taxonomy. One should always try and avoid using term names in queries Commented Jul 11, 2016 at 4:31
  • I already have a solution about that. what I need is control the post of 1 category by separating them or grouping them according to the sub-category. e.g Category 1 under it Subcategory1 and Subcategory2. right now I dont have any filter for the subcategories.
    – MIke
    Commented Jul 11, 2016 at 5:25
  • 2
    sorry, I have no clue what you're asking for.
    – Milo
    Commented Jul 11, 2016 at 15:04
0

Did this before for a website. Here's what i did:
Checked if there is a category with the same name as the title:

if( term_exists(get_the_title(), 'category' ))

If there was one then I got the category by slug:

$cat = get_term_by("slug", $post->post_name, "category"); 

Checked if category had parent or was a parent:

if($cat->parent !== 0) {
    $catID = $cat->parent;
} else {
    $catID = $cat->term_id;
}  

Then retrieved categories by said parent:

$args = array(
                'type'                      => 'post',
                'child_of'                  => $catID,
                'hide_empty'                  => false,
                'hierarchical'              => true,
                'exclude'                   => '1'
            );
$categories = get_categories($args);

Then a simple loop with links:

foreach($categories as $categorie) {
    if($categorie->slug == $post->post_name) {
        $clasa = "class='current'";
    } else {
        $clasa = "";
    }
    echo "<a ".$clasa." href='".home_url("/").$slug."/".$categorie->slug."'>".$categorie->name."</a>";
}

Hope this is what you are looking for.

2
  • Sorry, I didn't knew how. I need to learn these things. Thanks for the fix.
    – yomisimie
    Commented Jul 19, 2016 at 9:42
  • got syntax error with this. I don't know much about php.
    – MIke
    Commented Jul 28, 2016 at 6:41
0

Inside your parent post/category loop use this to generate a list of post titles/category names of the child posts/categories:

$taxonomy_name = 'category';
$this_term = get_term_by( 'name', get_the_title(), $taxonomy_name );

$term_id = $this_term->term_id;
$termchildren = get_term_children( $term_id, $taxonomy_name );

echo '<ul>';
foreach ( $termchildren as $child ) {//get the children and search the posts by title

  $term = get_term_by( 'id', $child, $taxonomy_name );
  $args = array ( 'post_title' => $term->name, 'orderby' => title, 'order' => ASC);
  $myposts = get_posts( $args );
  foreach( $myposts as $post ) :  setup_postdata($post); ?>
    <li>
      <strong><?php the_title(); ?></strong>
      <?php the_content(); ?>
    </li>
<?php endforeach; ?>

}
echo '</ul>';

UPDATE: Let's try that again. Inside your parent post/category loop use this . . .

$taxonomy_name = 'category';
$this_term = get_term_by( 'name', get_the_title( get_the_ID() ), $taxonomy_name );

$categories = get_categories( array(
        'orderby' => 'name',
        'child_of'  => $this_term->term_ID,
    ) );

if ( ! empty( $categories ) ) {

    foreach ( $categories as $category ) {
        $this_post = get_page_by_title( $category->name, OBJECT, 'post');
        echo '<h3>' . $this_post ->post_title . '</h3>';

    }
}
9
  • This is expensive and overboard Commented Jul 18, 2016 at 17:00
  • what I;m going to use to wrap this up?
    – MIke
    Commented Jul 20, 2016 at 2:32
  • I guess I'm a little uncear on what you're trying to achieve. You have post titles that are category names. This lists the children of those categories. I display the name of the child category using the corresponding $this_post->post_title. What output are you looking for?
    – dg4220
    Commented Jul 20, 2016 at 11:59
  • Code I'm using is getting the page name then convert it to slugs with has a corresponding same value as categories. now after that some of the main categories have subcategories i want it to seperate depending on what subcategories it belong if it has.
    – MIke
    Commented Jul 21, 2016 at 5:23
  • When you say 'I what it to seperate depending . . . ' What is 'it' refering too? Are you just trying to list the child categories based on a given category slug?
    – dg4220
    Commented Jul 21, 2016 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.