• Resolved forevangel23

    (@forevangel23)


    Okay, I’ve combed the internet, raked the Codex, fiddled with code, and am still having one issue with my code. It–sort of–works. But not quite how I want it to. (It’s all local development at the moment, so I can’t link, but I’ll try to do a good job at describing.)

    What I want to accomplish is have my single.php display posts in the same category for the previous and next posts underneath the post. The problem is that each of my posts are in multiple categories, and they are display through one of the other categories versus the one I want them to display from. Does that even make sense?

    Example Categories:

    Characters (parent category) (Subcategories listed below:)

    – Tony
    – John (This one is displaying instead.)
    – Farah
    – Stories (I want these ones to display.)

    Now, my question is, how do I choose the category I want to be pulled from instead of the one being automatically pulled? Here is my code below (that I’ve found and been fiddling with), with what I’ve tried so far.

            <?php
            if (is_single() && in_category('stories')) {
                $post_id = $post->ID;
                $cat = get_the_category(); //I've tried changing this to my category (both ID and slug)
                $current_cat_id = $cat[0]->cat_ID;  //Also tried plugging ID and slug
    
                $args = array(
                    'category' => $current_cat_id, //Also tried plugging ID and slug
                    'orderby' => 'post_date',
                    'order' => 'DESC'
                );
                $posts = get_posts($args);
    
                $ids = array();
                foreach ($posts as $thepost) {
                    $ids[] = $thepost->ID;
                }
    
                $thisindex = array_search($post_id, $ids);
                $previd = $ids[$thisindex - 1];
                $nextid = $ids[$thisindex + 1];
    
                if (!empty($nextid)) {
                    ?><div class="double-grid"><a rel="next" href="<?php echo get_permalink($nextid) ?>"><div class="image-tile tile-on-archive-page" style="background-image: url('<?php echo get_the_post_thumbnail_url($nextid); ?>'"> <div class="gold-button">LAST STORY >></div></div></a></div><?php
                }
                if (!empty($previd)) {
                    ?><div class="double-grid"><a rel="prev" href="<?php echo get_permalink($previd) ?>"><div class="image-tile tile-on-archive-page" style="background-image: url('<?php echo get_the_post_thumbnail_url($previd); ?>'"> <div class="gold-button">NEXT STORY >></div></div></a></div><?php
                }
            }
            ?>

    Any input appreciated!

    • This topic was modified 7 years, 9 months ago by forevangel23.
    • This topic was modified 7 years, 9 months ago by forevangel23.
    • This topic was modified 7 years, 9 months ago by forevangel23.
    • This topic was modified 7 years, 9 months ago by forevangel23.
Viewing 14 replies - 1 through 14 (of 14 total)
  • Moderator bcworkz

    (@bcworkz)

    How would script determine which category you want to use in getting related posts? You need to arrive at some distinguishing characteristic that script can use to pick one category over the others. The order selected is not saved, so that’s out. It could be the highest or lowest ID, first or last alphabetically, some arbitrary ranking stored in term meta, etc. We need something to go on.

    Thread Starter forevangel23

    (@forevangel23)

    @bcworkz, thanks for the reply.

    What do you mean you need something more to go on? What is my code lacking? Or is it in my description of my problem?

    I’m not sure I understand you response. Thanks.

    Moderator bcworkz

    (@bcworkz)

    Your code is OK, except that it’s not using the categories you want to use.

    If a post has the categories Tony, John, Farah, and Stories; how is code supposed to know to use “Stories” and not something else? What’s so special about Stories that distinguishes it from other categories? Special from how code sees the data. “Not a person’s name” is not something code can use. What can code use to know you want Stories? Something that will apply no matter what other categories also belong to a post.

    Thread Starter forevangel23

    (@forevangel23)

    Ahhh! We are on the same train of thought. That’s exactly where my question comes in. I’m not entirely sure where/how to tell the code to know “Stories” and none of the others.

    Moderator bcworkz

    (@bcworkz)

    If you always wanted “Stories” that would be easy enough. But what about another post with a different set of categories? There needs to be something in the data somewhere that distinguishes which category should be picked. The data fields for a category term are: term_id, name, slug, term_taxonomy_id, description, parent, and count. In addition there can be a group ID and any sort of term meta data, but there is no existing UI for this additional data, it can be set by code or additional UI elements would need to be added.

    One of these has to have an attribute that no other category added to any given post would have. Then code can see this and know it has picked the correct category. You need to determine what this attribute is or there is no possible solution.

    For example, if you are not using the description field, it could contain a “preference number”. Which ever category assigned to any given post has the lowest preference will be the category used for the related post query. A term with preference 1.5 will be used in preference over categories with 1.8, 2.5, or 4.2 preference numbers.

    Or another example: code can simply use the first top level category found. If only child categories are found, no related posts are listed. If there are more than one top level category for a post, the first one found is used.

    You need to come up with some sort of system like these examples. Once you do, then there is some sort of code that can implement it.

    Thread Starter forevangel23

    (@forevangel23)

    I get there’s more to it and it gets complicated. But for now, how would I just do the stories? That’s literally all I’m trying to accomplish. That’s it. I can figure the rest out later.

    • This reply was modified 7 years, 9 months ago by forevangel23.
    Moderator bcworkz

    (@bcworkz)

    Oh, wait! I see now. Your entire code snippet is restricted by in_category('stories'), so that category must exist for the post. Since that’s the only one you care about, just change 'category' => $current_cat_id, to use the actual category ID for the stories term. Skip the entire get_the_category() bit.

    Sorry for my lack of detailed attention.

    While it’s often unwise to hardcode values (they should be determined dynamically where possible), you’ve already hardcoded ‘stories’ so using the related ID is no different.

    Thread Starter forevangel23

    (@forevangel23)

    Ahhhh! That’d make sense. So when I swap out the 'category' => $current_cat_id, which parts do I gut? (Forgive me, I am new-ish to this.)

    • This reply was modified 7 years, 9 months ago by forevangel23.
    Moderator bcworkz

    (@bcworkz)

    No problem. Remove $current_cat_id and replace it with the actual term ID as an integer (not quoted). You can get the ID from the category list screen linked under the Posts admin menu item. Hover over the Edit option under the stories item. If your browser displays links on hover, locate the tag_ID= argument. The ID is the following number. If you do not get links on hover, follow the edit link and get the tag_ID= from the address bar.

    Be sure the trailing comma remains. If the ID were 123, your argument would be:
    'category' => 123,

    Thread Starter forevangel23

    (@forevangel23)

    Thanks! I’ve implemented that part. But then it only displays one link, and it’s to the very last post (instead of the previous or even next). Am I missing something else in the code?

    • This reply was modified 7 years, 9 months ago by forevangel23.
    Moderator bcworkz

    (@bcworkz)

    No, your code is correct. I even tested it on my site to be sure. Using my own category of course. What you see depends on where the current post falls in the list of found posts. From your description, it sounds like the query only returned two posts, the current (not linked) and last. Are you sure you got the right ID? And you have more than two posts in Stories? Just posts, not any custom post types. Be sure you have any caching disabled.

    As another test, determine the ID of whatever category was coming up before (John?) and set the category argument for that ID number. The initial results should be exactly what you had seen before with your original code. Though when navigating the links after the initial ones you may see different posts because the original code kept changing the category queried. With the ID hardcoded you always get the same category. Maybe not much of a test.

    Here’s something to try, add this into the $args array:
    'numberposts' => -1,
    Be sure the trailing commas are correct for all array items.

    Still no joy? It’s possible for other code to skew queries, so that can cause improper results. To be sure nothing is affecting your queries, deactivate all plugins and switch to the twentysixteen theme. Add your code to the single.php template for twentysixteen (keep a backup of the original), replacing the default post navigation (everything inside the final {curly braces} ).

    There’s only one reason I can imagine why it wouldn’t work in this state. Your WP installation is corrupted. If so, do a manual “update” using the exact same version you currently have.

    Thread Starter forevangel23

    (@forevangel23)

    Ahhhh! That 'numberposts' => -1, did EXACTLY the trick and fixed my problem. THANK YOU. I’ve been struggling with this problem for a week now!

    PS–Sorry for the belated reply. I was away from my computer this weekend.

    Moderator bcworkz

    (@bcworkz)

    Awesome! Sorry it took a while to get on track. I hope you enjoyed a computer free weekend!

    Thread Starter forevangel23

    (@forevangel23)

    Seriously, no worries at all! I’m just super thrilled that with your help it’s working the way I want it to!

    Thanks again!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Previous/next post links displaying wrong category than is desired’ is closed to new replies.