• Hello!

    I want to use different shortcodes in a page or posttitle

    Total comment count;

    function comment_count() {
      global $wpdb;
      return $wpdb->get_var("SELECT COUNT(*)
                             FROM $wpdb->comments
                             WHERE comment_approved = 1");
    }

    add_shortcode('commentcount', 'comment_count');

    And Total post count;

    function post_count() {
      global $wpdb;
      return $wpdb->get_var("SELECT count(id)
                             FROM $wpdb->posts
                             WHERE post_type = 'post'
                             AND post_status = 'publish'");
    }

    add_shortcode('postcount', 'post_count');

    I know I need do something with filters? Can somebody help me with this question?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Shortcodes only work in post content. You need to use other means to output values elsewhere on a page. For example, to inject something into titles, hook into ‘the_title’ filter.

    Hi nblauw,

    How/where exactly do you want this to show up in your site? The WP function the_title() shows up in many unexpected locations, such as wp_nav_menu() and in the admin. If you are thinking that you only want this content to show up on a page’s h1 say then you could always write something like this into your theme:

    page.php (and similar in single.php):
    <h1><?php the_title(); ?> <?php post_count(); //and/or comment_count(); ?></h1>

    If you do want this to be everywhere that you use the function the_title() you could write something like this:

    functions.php

    <?php
        function add_post_count( $title ){
            return $title . '  ' . post_count();
        }
        add_filter( 'the_title', 'add_post_count' );
    ?>

    If you are looking to just process any short code that you stick into the title field you could run something like the following:

    functions.php

    <?php
        function add_shortcode_to_title( $title ){
            return do_shortcode($title)
        }
        add_filter( 'the_title', 'add_shortcode_to_title' );
        //you might be able to simplify this to just "add_filter( 'the_title', 'do_shortcode' )", though I am not 100% sure of that...
    ?>

    Though again, of all of these, the first one, is likely the best option (if it suits your needs). Hopefully this helps!

    Thread Starter nblauw

    (@nblauw)

    I want to use a shortcode in a page title like:
    Berichten ([post_count])
    And
    Reacties ([comment_count])

    I did it already with a gallery count and a guestbook count ( with help, see my blog @ bartbroersen.nl

    P.s. Your first solution ( PHP ) , how change the page title for only one page in page.php?

    P.s. I need the Total comment count, for the whole blog.

    Thanks for helping!

    Thread Starter nblauw

    (@nblauw)

    I want to see it on the mainpage and on the post or page itself.

    Hi nblauw,

    The last code snippet I provided will do that. Here it is again:

    <?php
        function add_shortcode_to_title( $title ){
            return do_shortcode($title)
        }
        add_filter( 'the_title', 'add_shortcode_to_title' );
        //you might be able to simplify this to just "add_filter( 'the_title', 'do_shortcode' )", though I am not 100% sure of that...
    ?>

    I think that you are probably better off editing the theme files though. For the “main page” (if you are talking about your main feed), it will depend upon how your theme as to which file this is (could be home.php, front-page.php, [template-name].php or something else.

    P.s. Your first solution ( PHP ) , how change the page title for only one page in page.php?

    To target just one page you can do something like this:

    <?php
      the_title();
      if( is_page('YOU_PAGE_SLUG, ID or TITLE') ) echo 'Total Post Count: ' . post_count() . ' Total Post Count: ' . comment_count();
    ?>

    I hope that this helps.

    Thread Starter nblauw

    (@nblauw)

    This is what I putted in my functions.php, but it’s not working, it broke my site

    function comment_count22( $title, $post_id ) {
      global $wpdb;
      $result = $wpdb->get_var("SELECT COUNT(*)
                             FROM $wpdb->comments
                             WHERE comment_approved = 1");
    $result2 = do_shortcode( $result );
    
    if ( preg_match( '/\[\s*commentt_count\s*\]/', $title ) ) {
    		$title = preg_replace( '/\[\s*comment_count\s*\]/', $result2, $title );
    	}
    	return $title;
    
    }
    
    add_filter( 'the_title', 'comment_count22' )

    I used your code snippet, but it changed all post/page titles in the output of the comment count.

    Hi nblauw,

    It seems like you are mixing the different approaches up. If you want to parse the shortcodes, you can in the WP admin in the title field enter something like [comment-count].

    Then in you functions file you only need the following code:

    <?php
        function my_comment_count($title){
             return do_shortcode($title);
        }
        add_filter( 'the_title', 'my_comment_count' );
    ?>

    This would of course rely on you creating a short code that caused the comment count. To create a short code you need code like this:

    <?php
        function shortCodeFunction( $atts, $content=null ) {
    	//..code here
         }
         add_shortcode( 'shortCodeName', 'shortCodeName' );
    ?>

    Does that make sense? If you want to just append the count, rather then add the ability for the title field to parse short codes then you could do something like this:

    <?php
        function add_comment_count_to_all_titles($title){
    
             $result = $wpdb->get_var("
                 SELECT COUNT(*)
                 FROM $wpdb->comments
                 WHERE comment_approved = 1
             ");
    
             return $title . "Has {$result} comments";
        }
        add_filter( 'the_title', 'my_comment_count' );
    ?>

    I do want to mention again that binding this functionality to “the_title” is probably not the best idea. You are probably better off tweaking the theme templates.

    At any rate, hopefully this helps.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How-to use a shortcode in a page or post title’ is closed to new replies.