admin管理员组

文章数量:1297013

There is one plugin called auto numbering posts whose function is to add a number next to the post title, but the drawback of this plugin is that it doesn't sort by category. I want the posts in each category to have their own numbering order.

add_action('the_title', 'dk_auto_numbering');
function dk_auto_numbering($title)
{
    $post_ID = get_the_ID();
    $the_post = get_post($post_ID);
    $date = $the_post->post_date;
    $maintitle = $the_post->post_title;
    $count = '';
    if ($the_post->post_status == 'publish' and $the_post->post_type == 'post' and in_the_loop()) {
        global $wpdb;
        $count = $wpdb->get_var("SELECT count(*) FROM $wpdb->posts  WHERE post_status='publish' AND post_type='post' AND post_date<'{$date}'");
        if ($maintitle == $title) {
            $count = $count . '';
            $title = $title . ' – Chapter ';
        } else {
            $count = '';
        }
    }
    return $title . $count;
} 

There is one plugin called auto numbering posts whose function is to add a number next to the post title, but the drawback of this plugin is that it doesn't sort by category. I want the posts in each category to have their own numbering order.

add_action('the_title', 'dk_auto_numbering');
function dk_auto_numbering($title)
{
    $post_ID = get_the_ID();
    $the_post = get_post($post_ID);
    $date = $the_post->post_date;
    $maintitle = $the_post->post_title;
    $count = '';
    if ($the_post->post_status == 'publish' and $the_post->post_type == 'post' and in_the_loop()) {
        global $wpdb;
        $count = $wpdb->get_var("SELECT count(*) FROM $wpdb->posts  WHERE post_status='publish' AND post_type='post' AND post_date<'{$date}'");
        if ($maintitle == $title) {
            $count = $count . '';
            $title = $title . ' – Chapter ';
        } else {
            $count = '';
        }
    }
    return $title . $count;
} 
Share Improve this question edited Mar 29, 2021 at 14:48 Rup 4,4004 gold badges29 silver badges29 bronze badges asked Mar 27, 2021 at 10:50 GuruhGuruh 32 bronze badges 2
  • I expect you can probably just enhance that plugin to do it? Or at the very least look at how it does it and then extend it yourself if you want to write new code. I'd guess it computes the current numbers in a save event, stores them in post meta and then has a the_title hook to add the number, Unless you also want the number in the slug too? – Rup Commented Mar 28, 2021 at 21:59
  • Anyway, I'm an amateur at this sort of thing, so I'm not sure. But here's the code – Guruh Commented Mar 29, 2021 at 12:21
Add a comment  | 

1 Answer 1

Reset to default 0

Here's an untested edit of your code to use the post's first category in the count:

add_action('the_title', 'dk_auto_numbering');
function dk_auto_numbering($title)
{
    $post_ID = get_the_ID();
    $the_post = get_post($post_ID);
    $date = $the_post->post_date;
    $maintitle = $the_post->post_title;
    if ($maintitle == $title && $the_post->post_status == 'publish' &&
        $the_post->post_type == 'post' && in_the_loop()) {
        $categories = get_the_category($post_ID);
        if (is_array($categories) && count($categories) >= 1) {
            $first_category_id = $categories[0]->term_id;

            global $wpdb;
            $count = $wpdb->get_var(
                $wpdb->prepare("SELECT count(*) FROM $wpdb->posts AS p " .
                    "INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = p.id AND tr.term_taxonomy_id = %d " .
                    "WHERE post_status='publish' AND post_type='post' AND post_date <= %s", $first_category_id, $date)
            );

            if (isset($count)) {
                return $title . ' – Chapter ' . $count;
            }
        }
    }
    return $title;
}

I've moved the has-something-else-already-modified-the-title check out to the top level, to run it before the database query, plus a few other minor rearrangements. It's slightly asymmetric in that it uses the current post's first category but counts any earlier post that has that category at all, but hopefully that's enough for what you want: if you need any more complicated category handling you can edit the SQL.

As I said in my original comment though I think these counts ought to be pre-computed and cached against the post in post meta, rather than run as a separate query for every post title shown on the page.

本文标签: titleI want to add number after post tittle for each category