[Support request] "Next" and "Previous" links at ends of cpt loop

Home Forums Support "Next" and "Previous" links at ends of cpt loop

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #305248
    Callista

    Hi there,
    LOVE GP so far!
    I am using IssueM to create magazine issues with articles. “Article” is a cpt, and “Issue” is a taxonomy. (https://zeen101.com/documentation/how-issuem-adds-issues-your-wordpress-site/)

    I created a single-article.php from single.php for my article cpt (it’s just a full-width template without the site header) and added the generate_content_nav funtion just after the loop. It works great for cycling through my article cpt in order. However, when it gets to the last article, I’d like to add a link to either the home page or the archive page, because right now there’s no link, just nothing. And, once you navigate from the home page to the first article, the “previous” link is pointing to an article in a different issue, so I’d like that to go to the home page.

    Thanks so much!
    Callista

    GeneratePress 1.3.46
    GP Premium 1.2.96
    #305290
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Glad you’re enjoying GP!

    Any chance you can link me to what you have so far? This should be possible, but I’ll have to look into available filters in the next/prev function.

    #305373
    Callista

    Thanks for the quick reply!
    I’m currently using my local server/test environment. I’ll work on getting something set up for you to look at and let you know when it’s ready.
    Thanks!
    Callista

    #305496
    Callista

    Hi again!
    I’ve got a very basic site up that will eventually be the home of my magazine, but right now it’s just got some test content in it so I can work out the kinks: http://www.beautiful-muslimah.com/magazine/ I’ve made 2 issues, Issue 1 and Issue 2. Issue 2 is supposed to be a draft (meaning the articles, even though they’re “published” behind the scenes, shouldn’t be visible), and Issue 1 is live.

    If you go to the homepage and click on the first article (http://www.beautiful-muslimah.com/magazine/article/article-1/), you see that you can go forward just fine to article 2. But for the previous link, it’s showing article 10 (which isn’t supposed to be visible yet!) – and logically, that should go back to the home of the currently-being-viewed issue. I could just keep the articles as “draft” until the issue goes live, but then there would be no link at all, and then what happens when I do publish them? I might end up with 2 links if I add a link manually.

    Also, when you get to Article 5, it’s the end of the road. It would be nice to have a link back to the home page or the archive page or something. When I have more than one issue up, I could run into the problem of the last article pointing towards an article in a previous issue, instead of the homepage or archive page as well.

    Let me know if you need me to change any settings or anything or if you have any questions or insights.
    Thanks so much!
    Callista

    #305584
    Tom
    Lead Developer
    Lead Developer

    Alright, so to add the home page URL to the next/prev link when there’s no next/previous article, you can add these functions:

    add_filter( 'next_post_link','tu_no_next_post' );
    function tu_no_next_post( $output ) {
    	$next_post = get_next_post();
    	if ( $next_post ) { 
    		return $output;
    	} else { 
    		return '<div class="nav-next"><span class="next" title="Next"><a href="URL TO YOUR HOMEPAGE" rel="next">Home</a></span></div>';
    	}; 
    }
    
    add_filter( 'previous_post_link','tu_no_previous_post' );
    function tu_no_previous_post( $output ) {
    	$previous_post = get_previous_post();
    	if ( $previous_post ) { 
    		return $output;
    	} else { 
    		return '<div class="nav-previous"><span class="prev" title="Previous"><a href="URL TO YOUR HOMEPAGE" rel="prev">Home</a></span></div>';
    	}; 
    }

    Now for the other issue.. The only thing I can think of is if you categorize your posts with their issue (Issue 1, Issue 2 etc..).

    Then add 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' );
    }

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    #305616
    Callista

    Great! The first code works perfectly!
    The second code removed the post navigation completely (except for the Home links). I’ll take a look at it later tonight or tomorrow. Instead of the category taxonomy for posts, I have issues, and the articles are grouped by issue (so it’s sort of like a category). I’ll do some research on WP codex about taxonomies and functions and see if I can’t translate it myself before I ask for more help. You’ve been so much help!! Thanks so much. I really appreciate it.
    Callista

    #305704
    Tom
    Lead Developer
    Lead Developer

    Ah, you’re using a taxonomy of than “category”?

    #305904
    Callista

    Yes, that’s right. The IssueM plugin adds the custom post type Article, and the taxonomies of Issue, Article Category and Article Tags. An Issue is a group of articles, just like in a real magazine. Article categories I see as sort of like columns or regular features in a magazine, like “Letter from the Editor,” “This Month in X Magazine,” etc. The plugin comes with a shortcode and a widget that displays the articles from that Issue, with the article category above the article title.

    There is an option to use WP’s built-in categories and tags instead of article categories and tags, but that still doesn’t solve the problem of Issues (which is the main grouping). So, if I use WP category instead of Article Category, that’s fine, the widget and shortcode will still display the article list with the category title above it, but then if I assign another category of the issue number, then I’m not sure if it will display both, and then how will it affect the navigation loop? Also, I could use the category for issue, but then the article list will put the issue number above each article instead of the category.

    I will play around with the options in the shortcode/widget and see if I can get it to work with WP’s categories and tags. I might be able to get it to display the tag instead of category so I could mimic what it’s doing now with native WP taxonomies. I’ll let you know if I can get it to work!

    #305935
    Callista

    OK, so I set it up where the WP category is the issue and the former article category is now the WP tag. I assigned the articles to their issues (WP category of Issue 1 or Issue 2), used your code above, and it’s not limiting the loop to only the same category. Meaning, if I click on Article 1, at the bottom of Article 1 there is a link to go forward to Article 2 and a link to go back to Article 10, not the home page.

    http://www.beautiful-muslimah.com/magazine/article/article-1/

    I’ll see if there is a WP plugin that can control and limit the post navigation within a WP category. I think I saw one. I don’t want to keep bothering you. Thanks so much!

    #305995
    Tom
    Lead Developer
    Lead Developer

    Hmm, strange. That second snippet should only show posts within the same category.

    What about if you remove the first snippet of code I gave you dealing with the home links? Same issue?

    #306248
    Callista

    Hi! Yes, removing the first snippet didn’t change anything. I also tried a plugin designed to only show next/previous posts within the same category and I’m having the same problem. I think maybe the IssueM plugin is messing with the coding somehow. I might have to insert links manually in this case – or hire a developer to solve this particular issue since I know only enough about this stuff to be dangerous. 😉

    #306310
    Tom
    Lead Developer
    Lead Developer

    Hmm, that is strange. I can see why it wouldn’t work with a custom taxonomy, but it should have worked while using the core “Category” taxonomy.

    Let me know if you don’t make any progress using manual links etc.. and we’ll keep on trying 🙂

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