0

Wordpress newbie, here.

I've created a custom post type, using CPT UI, named "menus". Later created single instances of that "menus" CPT, directed by a "single-menu" template.

So here's my hierarchy for that "menu" CPT:

one template "menus" : listing all "menu" posts one template "single-menu" : generating individual posts of the "menu" post type various pages based on the "single-menu" template : for example, a "monday" page Now, i want to retrieve this "monday" single instance of that CPT and re-use its code using get_template_part().

So far, i've been able to call the CPT listing page content ("menus") but unable to call the single instance ruled by the "single-menu" template. Here's my code:

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

The path for the page targeted reads as follow on my browser:

http://localhost/website/menu/monday/

I've tried several combinations but have been unable to call it. New to wordpress as mentioned, used to use a standard php include function in my static prototype. It worked fine, now how i can call this "monday" with the get_template_part()?

Any idea as to how this could be done? Pointers welcome too!

2 Answers 2

1

You don't reuse a specific page's data via get_template_part, you query for a specific page's data and use get_template_part to output the result. Look at how any of the core-bundled themes use get_template_part within the loop to output the main query for an example.

Query for the page:

$query = new WP_Query( array(
    'post_type' => 'menu',
    'name' => 'monday'
) );
if( $query->have_posts() ){
    $query->the_post();
    get_template_part( 'menus' );
    wp_reset_postdata();
}

Then menus.php outputs the result:

<?php
the_title();
the_content();
// etc..
2
  • Thanks Milo, i did query the specific page data and it shows fine on the page itself. Now what i'm trying to do is call that content onto another page. Not sure if i'm explaining correctly. Edited my question, hoping to be more specific.
    – Guillaume
    Commented Mar 7, 2014 at 20:56
  • I think I understood what you're after, see edit above.
    – Milo
    Commented Mar 7, 2014 at 21:09
0

Update on my problem. For the record, i had created a custom post type "menu", with a post listing page "menus" and single instances of this custom post "menu/monday", "menu/tuesday", etc.

I was trying to access the content of this single instances so as to display it on my front page, using the get_template_part() function.

Here's how i eventually proceeded:

first, i created a new php for include "content-monday":

<?php

$args = array(
    'post_type' => 'menu',
    'name' => 'monday'); // using Milo's code here to fetch the content of my single-custom

$the_query = new WP_Query( $args );
?>

<?php if ( have_posts() ) : while ($the_query->have_posts() ) : $the_query->the_post() ;?>
<?php the_field( 'plat_vegetarien' ); ?>
<?php endwhile; else: ?>

<p>No post!</p>


<?php endif; ?>

then i used the get_template_part() to display the content on my front page:

get_template_part( 'content', 'monday' );

It seems to work fine so far, thanking Milo for his help!

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.