• How do I automatically Get the ID of a Tag so I can pass that variable on for another function?

    I’ve looked at all the template tags, and nothing that I can tell just grabs the ID of a tag. They only function in a way that I provide the ID#, and it prints what that tag represents.

Viewing 15 replies - 1 through 15 (of 16 total)
  • You could try writing a MySQL query to get it for you.

    Thread Starter Jim R

    (@jim-r)

    Dang it, I knew my request was too simple, trying to avoid a double post. I need to get the Tag ID passed through after clicking on the Tag’s link. So I click on siteURL.com/tag/tag1_tag2, and it takes me to an Archive.php. I need to get the ID of that Tag so I can use it for something else to put on that Archive.php along with the other content.

    AFAIK, you can’t get the tag ID using any available methods. Mind explaining in further detail? Maybe I can suggest another method of solving the problem.

    <?php echo get_query_var('tag_id'); ?>

    or

    <?php echo $wp_query->query_vars['tag_id']; ?>

    Would print the ID for the current tag being queried.. (both the above do the same thing).

    Thread Starter Jim R

    (@jim-r)

    @t31os_,

    The second one isn’t working, and I’m guessing that’s the one I need since I need to do more than echo it. I’m using it in a PHP block widget, and it makes my other code not work. It’s just:

    echo 'Just testing...';

    The $wp-query makes it disappear.

    Regarding the first option, would I assign a string variable to that result so I could use it later?

    $ID = get_query_var(‘tag_id’);

    Still, very helpful. I feel like I’m getting closer, and once I can carry that value over, the rest, for me at least, is easy.

    Yeah the first is more reliable…

    Your example is right for using as a variable..

    $somevar = get_query_var('tad_id');

    Code posted before was simply an example. I’d suggest not using ID, incase it’s being used for something else, use something unique and less likely to already be in use ($my_tag_id – etc…).

    Thread Starter Jim R

    (@jim-r)

    Take a look at this. When I echo out $wp_tagID, which is defined in the first line of the PHP code, it outputs the correct value. There is a corresponding ID in my playerRank table, and it should be echo’ing information from the one that matches.

    I’m putting this code in a PHP widget block.

    <?php
     $wp_tagID = get_query_var('tag_id');  
    
    mysql_select_db("jwrbloom_hhr");
    
    $query = 'SELECT * FROM playerRank';
    $results = mysql_query($query);
    while($line = mysql_fetch_assoc($results)) {
    
    if ($line['wpID'] = $wp_tagID)  {
    
    	echo '<div>' . $line['nameFirst'] . ' ' . $line['nameLast'] . '</div>';
    
     ?>
    Thread Starter Jim R

    (@jim-r)

    BTW…the result in the PHP widget block is basically nothing, just blank.

    Thread Starter Jim R

    (@jim-r)

    Figured it out. I didn’t have the double == in my “IF” statement, and I hadn’t close my while or if loops.

    <?php
    $wp_tagID = get_query_var(‘tag_id’);

    mysql_select_db(“jwrbloom_hhr”);

    $query = ‘SELECT * FROM playerRank’;
    $results = mysql_query($query);
    while($line = mysql_fetch_assoc($results)) {

    if ($line[‘wpID’] == $wp_tagID) {

    echo ‘<div>’ . $line[‘nameFirst’] . ‘ ‘ . $line[‘nameLast’] . ‘</div>’;
    }

    }
    ?>

    I had spotted the error, but looks like you beat me to it.

    If you’re only selecting wpID from that playerRank table, then you’d be better off selecting only that in your query, else you’re grabbing extra amounts of unused data.

    $query = 'SELECT * FROM playerRank';
    $query = 'SELECT wpID FROM playerRank';

    Of course if you have to do stuff with the other data, then just disregard the above.

    Also you may wish to add a check to see if the query var is set before placing it in a variable, but rather then doing isset/(if else etc..), you could use a one liner to set a default if it’s not set..

    $wp_tagID = (get_query_var('tag_id')) ? get_query_var('tag_id') : 0;
    // Sets the variable to 0 if the query var didn't exist..

    NOTE: 0 default value was an example, you can default it to what you like.

    Just avoids PHP chucking up an error if the query var isn’t set.. and sets a default value when needed..

    Thread Starter Jim R

    (@jim-r)

    Since I see your name dotted around the forum providing good solutions, any idea why the code above would make widgets disappear from the sidebar? It has something to do with the select_mysql_db command. I even tried closing the connection, but that didn’t work either…unless I’m not closing it properly.

    Hi,

    First update these 2 lines.

    mysql_select_db("jwrbloom_hhr");
    $results = mysql_query($query);

    to.

    mysql_select_db("jwrbloom_hhr") or die('Failed to select database');
    $results = mysql_query($query) or die('Could not execute query');

    You’ll at least know if both of those lines are getting a result, and if not show an error.

    I assume you have the other db connection stuff before what you’ve shown another…

    Thread Starter Jim R

    (@jim-r)

    Not sure I follow. I made the change you suggested, but my code was working as it’s intended, with the exception the widgets in the sidebar stop functioning. The code above is in the tag.php file, using Hyrbrid News theme.

    https://hoosierhoopsreport.com/tag/alexander-hutson/

    So if you remove your code the widgets reappear?

    You sure they’re not just pushed out of view?… does the widget code still appear in the source code when your code above is also present?

    Thread Starter Jim R

    (@jim-r)

    When I remove the mysql-select-db code the widgets reappear. It does it on other pages too where I have that code in a tab. It just stops them working. The framework kind of shows up, but the content doesn’t.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘I need to GET the ID of a Tag, so I can use it…’ is closed to new replies.