[Resolved] CPT and custom fields

Home Forums Support [Resolved] CPT and custom fields

Home Forums Support CPT and custom fields

  • This topic has 26 replies, 2 voices, and was last updated 5 years ago by Tom.
Viewing 15 posts - 1 through 15 (of 27 total)
  • Author
    Posts
  • #781826
    Jem

    Hi

    I’m struggling with displaying a CPT with custom fields on a music festival website (which I’m moving from Genesis to GeneratePress), having spent all day trawling the web and the GP forums I can’t see wood for trees. I have very limited experience dealing with CPTs, mainly from originally developing this site in Genesis several years ago, and only the most basic grasp of PHP (usually via snippets). At this stage it’s a local site so no URL, sorry.

    I have created the CPT (‘artist’) and two taxonomies (‘stage’ and ‘when’*) using Custom Post Types UI and the custom fields using ACF.

    I’m using a child theme so I’ve copied archive.php, single.php and content-single.php to the child theme directory and renamed them to archive-artist.php, single-artist.php and content-artist.php.

    Initially I had the ACF fields in content-artist.php but they weren’t displaying at all so now I’ve got them in a hook (after_entry_header) for the CPT, which is now displaying them, of course before entry-content. I would prefer if I could have the fields inside entry-content, replacing what would be the default WP post content for a standard post. Is this possible? Would that be by using content-artist.php instead of a hook? If so, which part of it do I need to modify, and can I just insert the content of the hook directly?

    Two other issues:

    1. Post navigation for the CPT – I’ve added the code from this post to my functions.php but nothing shows up on the front end (no other functions in there yet). Post nav is set to display in the customizer and I’ve disabled all but essential plugins for this page to display as it should.
    2. I need to display the custom taxonomies in the post footer, but as unlinked/text only as there is no need for taxonomy archives. How can I achieve this?

    Any pointers much appreciated.

    *’when’ is actually ‘day’, as in the day of the performance but ‘day’ is reserved for core WP so I had to name it ‘when’.

    #782060
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    For the first issue, make sure you’re changing this line your single.php file: https://github.com/tomusborne/generatepress/blob/2.2.1/single.php#L26

    It should be:

    get_template_part( 'content', 'artist' );

    Let me know if that fixes it.

    1. Did you update project in that code to artist (if that’s your CPT name)?

    2. You could try something like this:

    add_action( 'generate_after_content', function() {
        if ( is_singular( 'artist' ) ) {
            $terms = get_terms( array(
                'taxonomy' => 'post_tag', // Update to your custom taxonomy
                'hide_empty' => false,
            ) );
    
            foreach( $terms as $term ) {
                echo '<span class="term-name">' . $term->name . '</span>';
            }
        }
    } );
    #782592
    Jem

    Hi

    Thanks. Line 26 of content-artist.php is (and was):

    get_template_part( 'content', 'artist' );

    No joy, all I’m getting on the front end is the title and featured image. This is how I have inserted content between line 62 and line 65 of content-single.php (renamed to content-artist.php and copied to the child theme directory):

    <?php
    	the_content(); ?>
    			
            ALL THE ACF FIELDS HERE IN VARIOUS DIVS
    
    	<?php	
    	wp_link_pages( array(

    Is that correct?

    1. Yes, project is changed to artist, nothing is showing up.
    2. I’ve added that code to functions.php but again, nothing is showing up, even when the artist post content is visible (via the hook).
    #782695
    Tom
    Lead Developer
    Lead Developer

    That looks right. What happens if you just add plain text in that space? Does it display?

    Any chance you can set it up on a live server so I can see one of the single artist pages?

    #782835
    Jem

    Nothing shows up when I add text to content-artist.php in place of the ACF fields either. Something, probably very small, is missing from the set up.

    The site is very bulky so it’ll take a while to get it on a staging server. I’m uploading it now, I’ll let you know when it’s up and I’m happy to set up admin access if needed, and also to email the relevant template files if you need to see them. Thanks.

    #782878
    Jem

    Is there any way I can get the URL – once it’s uploaded – to you privately, and maybe login details too? It’s not ready at all for public viewing.

    #783109
    Tom
    Lead Developer
    Lead Developer

    Hmm ok, that means the templating isn’t working.

    Are you sure the custom post type has artists as the ID/slug?

    You can send the details through our contact form: https://generatepress.com/contact

    Just be sure to mention this topic.

    #783923
    Jem

    Thank you, I appreciate your attentive support. I had a bunch of other things to attend to today but the site is now on a server and I’ve re-done all the steps to be sure everything is the way it should be, still no joy.

    URL etc coming through the pre-sales contact form.

    #783973
    Jem

    Just to say, not sure if you’ve got to it yet but it seems I’ve been an idiot. I was trialling a customised child-theme and forgot to switch back to the standard child theme, which was the one I’ve been editing templates in, it seems to be more or less working now.

    #784003
    Jem

    Just a couple more things – the taxonomy terms are all showing up below the post. I’ve set the function to display the ‘stage’ category and it’s showing all the stage names, where I actually only want the stage that a given artist will be appearing on, along with the day of their appearance. How would I edit that snippet to show both the stage and day (‘stage’ and ‘when’ taxonomies) relating to a given artist?

    Also, is it possible to give a higher priority to the content? I have Shariff Wrapper set to display share icons below posts, but they’re appearing above the CPT content.

    #784133
    Tom
    Lead Developer
    Lead Developer

    Ah, let’s try this instead:

    add_action( 'generate_after_content', function() {
        if ( is_singular( 'artist' ) ) {
            $terms = wp_get_post_terms( get_the_ID(), 'your-taxonomy-name' );
    
            foreach( $terms as $term ) {
                echo '<span class="term-name">' . $term->name . '</span>';
            }
        }
    } );

    You’d have to set the sharing plugin to not automatically display the sharing icons. Then you could manually display them within the same hook as the terms.

    Plugins that automatically add content to your content filter the actual content, so there’s no way a hook can appear above them.

    #784178
    Jem

    Thanks very much Tom, that works. 99% done.

    How to reference multiple terms (stage and day)? So far I’ve just added the snippet twice, once for each. Is there a way to reference both in one function (an array?) and output them both in one div? I’ve been trying various things without success.

    #784785
    Tom
    Lead Developer
    Lead Developer

    Are those separate taxonomies?

    If so, maybe this:

    add_action( 'generate_after_content', function() {
        if ( is_singular( 'artist' ) ) {
            $stages = wp_get_post_terms( get_the_ID(), 'stage' );
            $days = wp_get_post_terms( get_the_ID(), 'day' );
    
            echo '<div class="term-names">';
    
            foreach( $stages as $term ) {
                echo '<span class="term-name">' . $term->name . '</span>';
            }
    
            foreach( $days as $term ) {
                echo '<span class="term-name">' . $term->name . '</span>';
            }
    
            echo '</div>';
        }
    } );
    #784871
    Jem

    Thanks Tom, excellent. I just needed to put the right term names in. All good, many thanks for excellent support.

    #784906
    Tom
    Lead Developer
    Lead Developer

    Glad I could help 🙂

Viewing 15 posts - 1 through 15 (of 27 total)
  • You must be logged in to reply to this topic.