WordPress.org

Ready to get started?Download WordPress

Forums

Display ID of post without going through the Loop (5 posts)

  1. Mystrique
    Member
    Posted 8 years ago #

    Hi,

    I found out that function below ( single_post_title() ) can display the title without having to go through the Loop. The functions is in /wp-includes/template-functions-general.php

    My question is,

    Is there a way to modify the function so that it can display the content ID?
    (Or is there any other alternative which can achieve similar result?)

    Pls help.

    Thanks.

    function single_post_title($prefix = '', $display = true) {
    global $wpdb;
    $p = get_query_var('p');
    $name = get_query_var('name');

    if ( intval($p) || '' != $name ) {
    if ( !$p )
    $p = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$name'");
    $post = & get_post($p);
    $title = $post->post_title;
    $title = apply_filters('single_post_title', $title);
    if ( $display )
    echo $prefix.strip_tags($title);
    else
    return strip_tags($title);
    }
    }

  2. Orin
    Member
    Posted 8 years ago #

    I would think the simplest way would be to replace everything related to

    post_name = '$name'

    with

    post_ID = '$id'

    This should read from your database in the exact same fashion. And you wouldn't need the filters applied below there since the data is an integer and not text.

  3. Mystrique
    Member
    Posted 8 years ago #

    Thank you Orin for your prompt reply.

    Here is the changes that I made.

    I duplicate the single_post_title() function and rename it as single_post_id() plus the changes as you suggested. I pasted the modified function below.

    To call the function; <?php single_post_id(); ?>

    Problem is there were nothing being displayed. Can you please advise on what should I do?

    About the filter, I am not sure which part need to be deleted, is it the one in italic below?

    Kindly advise.

    Thanks,

    [Modified function]

    function single_post_id($prefix = '', $display = true) {
    global $wpdb;
    $p = get_query_var('p');
    $name = get_query_var('name');

    if ( intval($p) || '' != $name ) {
    if ( !$p )
    $p = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_ID = '$id'");
    $post = & get_post($p);
    $title = $post->post_title;
    $title = apply_filters('single_post_id', $title);
    if ( $display )
    echo $prefix.strip_tags($title);
    else
    return strip_tags($title);

    }
    }

  4. Kafkaesqui

    Posted 8 years ago #

    All post data is available through the $post object in a single post page. You don't need to perform a SQL query and such just to get at it. Try this:

    function single_post_id($prefix = '', $display = true) {
    if(is_single()) {
    global $wp_query;
    $post = $wp_query->post;
    $id = $post->ID;

    if($display) {
    echo $prefix . $id;
    }

    return $prefix . $id;
    }
    }

  5. Mystrique
    Member
    Posted 8 years ago #

    Cool...

    Thank you Orin, Thank you Kafkaesqui... :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags