简体   繁体   中英

Count Posts In A WP Taxonomy

I have this code in my functions PHP file which I made from searching forums:

function sc_estio_project_management_count() {
  echo '<div class="estio-counter" style="background-color: #FF5000";>We currently have<br />' . '<span class="estio-counter-span">' . esc_html( get_tag(210)->count ) . '</span>' . '<br />vacancies in<br />Project <br id="estio-counter-break"/>Management<br /><br /><a href="/training/job-type/project-management/">View all vacancies</a></div>';
}
add_shortcode( 'estio_project_management_count', 'sc_estio_project_management_count' );

The . esc_html( get_tag(210)->count ) . . esc_html( get_tag(210)->count ) . is great & currently echos out the number of posts within a custom post type. Currently there are 8 job types within the post type, each with an individual ID, ie the above is '210' so on my front end this says there are currently X amount of jobs available in Project Management.

Can anyone help how I can add all these post 8 job type counts together so I can make a shortcode which echos out the total number of posts? Ie 'We currently have a total of X jobs available')

Since you say the jobs are tucked in a post: Wordpress has a function called wp_count_posts

$count_posts = wp_count_posts( 'jobs' )->publish ;

So for example, you can do a count on the CPT jobs that are published like above example. The 'Jobs' is the CPT you want to count

For more reference: https://codex.wordpress.org/Function_Reference/wp_count_posts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM