[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 12 posts - 16 through 27 (of 27 total)
  • Author
    Posts
  • #829864
    Jem

    Hi again

    Still working on this. Since sorting the issues above I added a couple of functions to keep the main blog post nav within the news category and to keep galleries post nav within the galleries category. The extra two functions have stripped the artist post nav (as above). When I comment out only the second function (below) I lose the artists CPT post nav, and when I comment out only the third function I get the artist CPT post nav back but lose the ‘within category’ post nav for galleries posts (i.e. it includes News posts).

    Forgive my ignorance, but how should achieve this: news post nav within News category; gallery post nav within Galleries category; artist CPT post nav within artist CPTs (regardless of which artist category they may be in)?

    These are the three functions, in the order they appear in functions.php:

    add_action( 'generate_after_entry_content', 'jc_custom_post_type_post_nav' );
    function jc_custom_post_type_post_nav() {
    	if ( 'artist' == get_post_type() ) : ?>
    		<footer class="entry-meta">
    			<?php generate_entry_meta(); ?>
    			<?php if ( is_single() ) generate_content_nav( 'nav-below' ); ?>
    		</footer><!-- .entry-meta -->
    	<?php endif;
    }
    
    // Limit blog page to blog category
    function generate_home_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
           $query->set( 'category_name', 'news' );
        }
    }
    add_action( 'pre_get_posts', 'generate_home_category' );
    
    // Set post nav within category
    add_action( 'after_setup_theme','tu_next_prev_category_specific' );
    function tu_next_prev_category_specific() {
        add_filter( 'generate_category_post_navigation','__return_true' );
    }
    #830078
    Tom
    Lead Developer
    Lead Developer

    So the second function has no negative effect, but the third one makes the nav disappear completely?

    #830687
    Jem

    Slight error above, apologies.

    All three functions: post nav within each category on News and Galleries posts, no post nav at all for Artist CPT.

    First and second only: post nav within Artist CPTs; post nav for both News and Galleries posts mixes News and Galleries posts (i.e. not category-specific).

    First and third only: post nav within each category for News and Galleries, no Artist CPT post nav at all.

    I’m looking for News post nav to move between posts in News category only, Galleries post nav to move between posts in Galleries category only, Artists CPT post nav to move between Artists CPT posts only (regardless of Artist CPT category, which would be either day or stage).

    #831055
    Tom
    Lead Developer
    Lead Developer

    Ah, the “in same term/category” feature only works if we manually set the taxonomy.

    Instead of the first function, try this:

    add_action( 'generate_after_entry_content', function() {
        if ( is_singular( 'artist' ) ) : ?>
            <footer class="entry-meta">
                <?php generate_entry_meta(); ?>
    
                <nav id="nav-below" class="post-navigation">
                    <span class="screen-reader-text"><?php esc_html_e( 'Post navigation', 'generatepress' ); ?></span>
                    <?php
                    previous_post_link( '<div class="nav-previous"><span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '">%link</span></div>', '%title', true, '', 'your-taxonomy-name' );
                    next_post_link( '<div class="nav-next"><span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '">%link</span></div>', '%title', true, '', 'your-taxonomy-name' );
                    ?>
                </nav>
            </footer><!-- .entry-meta -->
        <?php endif;
    } );

    Then you need to replace your-taxonomy-name with the taxonomy the terms are in.

    More info here: https://wordpress.stackexchange.com/questions/57867/next-and-previous-link-in-custom-post-type-in-the-same-taxonomy-term

    #832003
    Jem

    Thanks Tom. I’m a little confused by this though, not sure you get my requirement – for the Artist CPT I just need post nav to go between all the Artist CPTs – as I say, regardless of which terms are applied – the categories are not important for their post nav, as their categories will be which stage they play on and which day – I just need visitors to be able to cycle through all the artists.

    News post nav – cycle through news posts (category: news) only.
    Galleries post nav – cycle through gallery posts (category: galleries) only.
    (Both of these are standard WP posts with relevant terms applied.)

    Artist CPT post nav – cycle through Artist CPT posts, no matter what category has been applied.

    Before I added the second and third functions the original function for Artists was working as intended, but on adding the other two functions they’ve disappeared.

    #832261
    Tom
    Lead Developer
    Lead Developer

    I’m not sure if that’s possible using the core WordPress function.

    What I do know is you definitely wouldn’t need this function:

    add_action( 'after_setup_theme','tu_next_prev_category_specific' );
    function tu_next_prev_category_specific() {
        add_filter( 'generate_category_post_navigation','__return_true' );
    }

    That function keeps things within the specific category/taxonomy – not the post type.

    Do your News and Galleries posts have their own post types?

    #833173
    Jem

    Hi Tom. I’m really confused now, I think our wires are crossed.

    As I said above, News and Galleries are standard WP posts (not CPTs), with relevant categories applied, each displayed on their own archive pages (News and Galleries). It is possible to get post nav to work for them within their categories which is why, as far as I can tell, the function you mention (#3) is needed – without it only the News posts stay within category, as they are mentioned specifically in the function above (#2), the Galleries post nav then includes News posts.

    Artists are the only CPT, and the function you originally provided early in this thread gave me the post nav I needed to cycle between them.

    So it is possible to keep both categories of standard posts within their respective categories when using their post nav. And it is possible to cycle through Artist CPTs using their post nav, regardless of any other categories applied to them.

    The issue I have is that I can’t seem to get both standard WP category-specific post nav working as well as Artist CPT post nav – it’s one or the other. When I have category-specific post nav for the standard WP posts, I don’t get any CPT post nav at all, and when I have CPT post nav working, I lose the category-specific post nav for the standard posts (specifically the Galleries posts). Which I think is down to how the functions are constructed, at which point I don’t know how to proceed.

    #833384
    Tom
    Lead Developer
    Lead Developer

    I guess the confusion is why making the links category-specific in your posts has any effect on the CPT.

    The function I mentioned above should actually help with this issue, I think: https://generatepress.com/forums/topic/cpt-and-custom-fields/page/2/#post-831055

    Instead of using the post nav the category-specific function applies to, it prints its own links with no reference to the generate_category_post_navigation filter.

    Did you try that function instead, yet?

    #833907
    Jem

    Hi Tom. Adding that function in place of function #1 above does give artist CPTs post nav, along with the standard posts cycling within their categories as intended.

    The problem is that I don’t want the artist CPT post nav to be category specific – as I said, it just needs to cycle through all artist CPTs, regardless of which terms they have applied. Artist CPTs have two categories, stage (term: stage; 5 possible choices) and day (term: when; 4 possible choices), which means if I specify any one of those terms I’m only cycling through the artists to which those terms are applied e.g. all artists playing on the main stage, or all artists playing on Sunday. I just need the post nav to cycle through them all.

    Can the function be modified so it doesn’t reference any terms, only the artist CPT itself?

    #833998
    Tom
    Lead Developer
    Lead Developer

    Something like this should do that:

    add_action( 'generate_after_entry_content', function() {
        if ( is_singular( 'artist' ) ) : ?>
            <footer class="entry-meta">
                <?php generate_entry_meta(); ?>
    
                <nav id="nav-below" class="post-navigation">
                    <span class="screen-reader-text"><?php esc_html_e( 'Post navigation', 'generatepress' ); ?></span>
                    <?php
                    previous_post_link( '<div class="nav-previous"><span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '">%link</span></div>', '%title' );
                    next_post_link( '<div class="nav-next"><span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '">%link</span></div>', '%title' );
                    ?>
                </nav>
            </footer><!-- .entry-meta -->
        <?php endif;
    } );

    Let me know ๐Ÿ™‚

    #834054
    Jem

    That’s precisely what was needed, many many thanks, also for your patience. Got there in the end.

    #834199
    Tom
    Lead Developer
    Lead Developer

    Awesome! ๐Ÿ™‚

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