0

For example, if I have a custom post type that are 'Case Studies',

Firstly I need to be able to query the post types for a single specific case study, but then lets say there are a few variations of that single case study;

'Wales', 'England', 'Scotland'... . The correct one needs to be picked out depending on the category taxonomy for that post (of the same name).

When the specific case study and its variation has been found, let's say:

Case Study: Highest Mountain

Category (taxonomy) of custom post: Scotland

I need to then put this information into a html template, for example the output would be (data pulled from the custom post):

<h3>Highest Mountain</h3>
<p>Here would be the content specific to Scotland...</p>

So when the shortcode is entered into the_content textarea by the user, all they have to input is e.g.

[casestudy study_type="mountain"]

When registering a new post for 'Case Studies', the category taxonomy (variation) would be selected. So if the Category chosen for the post was 'Scotland', and the user had this country selected in their user profile - it would pull the variation of that case study (e.g. mountain) specific to Scotland.

All help much appreciated, I haven't written a custom shortcode before - so the more explanation the better - thanks!

  • Please be more specific about your variations, how do you define them? It's not very clear. – ClemC Oct 6 '16 at 13:08
  • Sorry, edited question with missing information. – wadclapp Oct 6 '16 at 13:33
0

From your supplied info, it's not all very clear to me. But as far as I've understood your problem, here's my approach.
Assuming we have the following shortcode that the user has put inside a normal post's content and that this shortcode will pull information from your custom post type case_studies:

[casestudy study_type="mountain"] 

So firstly, your shortcode handler:

add_shortcode( 'casestudy', 'my_shortcode' );

function my_shortcode( $atts ) {
    $a = shortcode_atts( array(
        'study_type' => 'mountain',
    ), $atts );

    $content = my_template( $a );

    return $content;
}

Pay close attention to the shortocode_atts() function. It's meant to filter the accepted parameters that your template function's query below will be able to handle.

Then, your template:

function my_template( $a ) {
    /**
     * I believe we should be in the loop already when this function is being called.
     * So to get the category slug of the current post in which the user has put the shortcode, you can try this.
     */
    $category_terms = get_the_category();

    $args = array(
        'post_type'      => 'case_studies',
        'name'           => $a['study_type'],
        'category_name'  => $category_terms[0]->slug,
        'posts_per_page' => 1,
    );

    $query = new WP_Query( $args );

    ob_start();

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            the_post();

            echo '<h3>' . get_the_title() . '</h3>';
            echo '<p>' . get_the_content() . '</p>';
        }

        reset_postdata();
    }

    return ob_get_clean();

}
1

You have to create your own shortcodes to do this. Use add_shortcode(), create a fonction that can fetch all the datas you need. In your theme functions.php

add_shortcode('casestudy', 'myfunction');
function myfunction($atts){

   $a = shortcode_atts( array(
    'study_type' => 'mountain'
    ), $atts );
    $content='';
    // Get your data with get_post_meta

    return $content;
}
  • 1
    note about convention we try to follow here... since code is being copy and pasted ll the time from here, names like "myfunction" are really bad, the general convension is to prefix the function names with something that inludes the question number like wpse_241741_function – Mark Kaplun Oct 6 '16 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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