Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3406300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:36:40+00:00 2026-05-18T05:36:40+00:00

I’m working on a wordpress theme and I’m trying to call the parent category’s

  • 0

I’m working on a wordpress theme and I’m trying to call the parent category’s name to pull the appropriate page template.

I can get the call function to echo the correct name, but when I try to nest it the function doesn’t run. I saw that I needed to use { } since I was already inside php but it still isn’t working right. Can someone straighten me out?

This gives the correct output:

<?php $category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
echo '' . $parent;
} else {
echo '' . $category[0]->cat_name;
}
?>

. . . so I created a category_parent.php file with that in it.

This is what I’m trying to nest it in:

<?php get_template_part( ' ' ); ?>

Like this:

1.

<?php get_template_part( '<?php get_template_part( 'category_parent' ); ?>' ); ?>

or this

2.

<?php get_template_part( '{get_template_part( 'category_parent' ); }' ); ?>

Neither one works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:36:40+00:00Added an answer on May 18, 2026 at 5:36 am

    I really don’t know if this is what you want as I did not try to make sense of what you are doing. However, generally speaking, you do this:

    <?php get_template_part( get_template_part( 'category_parent' ) ); ?>
    

    Edit:

    I looked up what get_template_part() does in WP, and I think Felix Kling’s answer is what you need. There is a big difference between sending something to the screen and assigning it to a variable.

    <?php
      echo 'filename';
    ?>
    

    If you include that file, you will see filename in the browser. PHP knows nothing about it. (Okay, it could if you made use of output buffering functions, but that’s besides the point…)

    However if you do something like:

    <?php
       $x = 'filename';
    ?>
    

    You can now use it in your function:

    <?php
      get_template_part($x);
    ?>
    

    So what Felix is telling you to do is to put the logic that you currently have into a function. In this example:

    <?php
      function foo()
      {
        return 'filename';
      }
    
      get_template_part(foo());
    ?>
    

    Now whatever value foo() returns will be sent to your get_template_part().

    Taking your code:

    $category = get_the_category();
    $parent = get_cat_name($category[0]->category_parent);
    if (!empty($parent)) {
      $name = $parent;
    } else {
      $name = $category[0]->cat_name;
    }
    
    get_template_part($name);
    

    You could take Felix’s answer and put it into a file called category_parent.php, and then use it like:

    require_once 'category_parent.php'
    get_template_part(getName());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.