Forums

[resolved] Different meta if post is custom post type 'agenda' (5 posts)

  1. Sem90
    Member
    Posted 4 months ago #

    I'm making my own template. To display events I made a custom event-list with the help of this tutorial: http://www.makeuseof.com/tag/events-listing-custom-post-types-wordpress/.

    All works fine, however when I open an 'event' (which is basically a post) WordPress will still display the meta information of a normal single-post. This makes sense, but I would like to change WordPress, so that when the post type is 'agenda', it will replace the default meta information with my own meta information.

    Default meta information

    <span class="postmeta"><img class="date" src="http://tikvah.unitymedia.nl/wp-content/themes/Petachtikvah_v2/images/icons/date.png">
    <?php the_time('l d F Y') ?>, <?php the_time('G:i') ?><img src="http://tikvah.unitymedia.nl/wp-content/themes/Petachtikvah_v2/images/icons/user.png"> <?php the_author(); ?><img src="http://tikvah.unitymedia.nl/wp-content/themes/Petachtikvah_v2/images/icons/category.png"> <?php the_category(', ') ?></span>

    Meta information to be showed if post type 'agenda'

    <ul class="eventdetails">
    <li><b>Locatie:</b> <?php
    $locatie = get_post_meta($post->ID, 'locatie', true);
    if ($locatie){
    echo $locatie;
    }
    ?>
    </li>
    <li><b>Tijdstip:</b> <?php
    $tijd = get_post_meta($post->ID, 'tijd', true);
    if ($tijd){
    echo $tijd;
    }
    ?></li>
    </ul>

    Obviously this is just an example...but I hope you can help me out here. I have tried using custom post ifs.....but so far I would not give me what I'm looking for.

  2. zoonini
    help me help you
    Posted 4 months ago #

    You can try using a conditional tag. For example if your custom post type is called agenda:

    is_singular( 'agenda' )

    Reference & examples: http://codex.wordpress.org/Conditional_Tags#A_Single_Page.2C_Single_Post_or_Attachment

    If you need more basic help with conditionals, here's a presentation I did: http://www.slideshare.net/zoonini/take-control-of-your-templates-with-wordpress-conditionals-8546925

  3. zoonini
    help me help you
    Posted 4 months ago #

    p.s. alternatively you could create a separate template file for your custom post type. You'd call the file single-agenda.php

    Reference: http://codex.wordpress.org/Template_Hierarchy#Single_Post_display

  4. Sem90
    Member
    Posted 4 months ago #

    I solved it with the last suggestion! I now have to single-post files, one called single.php, the other one called single-events.php. Works brilliantly. The only downside is that this will require more 'clicks' to change the code to all single-posts, as now I have to edit two files. But that's worth it!

    Thanks!

  5. zoonini
    help me help you
    Posted 4 months ago #

    Glad it works. You can also think about using PHP includes in single.php and single-events.php to avoid duplicating code. Good luck!

    <?php include("your-file-name.php"); ?>

    Put your-file-name.php in your theme folder.

Reply

You must log in to post.

About this Topic