Forums

I need to GET the ID of a Tag, so I can use it... (17 posts)

  1. Jim R
    Member
    Posted 2 years ago #

    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.

  2. charlieholder
    Member
    Posted 2 years ago #

    You could try writing a MySQL query to get it for you.

  3. Jim R
    Member
    Posted 2 years ago #

    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.

  4. charlieholder
    Member
    Posted 2 years ago #

    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.

  5. Mark / t31os
    Moderator
    Posted 2 years ago #

    <?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).

  6. Jim R
    Member
    Posted 2 years ago #

    @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.

  7. Mark / t31os
    Moderator
    Posted 2 years ago #

    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...).

  8. Jim R
    Member
    Posted 2 years ago #

    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>';
    
     ?>
  9. Jim R
    Member
    Posted 2 years ago #

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

  10. Jim R
    Member
    Posted 2 years ago #

    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>';
    }

    }
    ?>

  11. Mark / t31os
    Moderator
    Posted 2 years ago #

    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..

  12. Jim R
    Member
    Posted 2 years ago #

    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.

  13. Mark / t31os
    Moderator
    Posted 2 years ago #

    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...

  14. Jim R
    Member
    Posted 2 years ago #

    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.

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

  15. Mark / t31os
    Moderator
    Posted 2 years ago #

    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?

  16. Jim R
    Member
    Posted 2 years ago #

    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.

  17. Mark / t31os
    Moderator
    Posted 2 years ago #

    Sounds to me like something is stopping the output of code..

    If you View Source is all the code for the page still being output?.. because it sounds to me like the code is being cut off at this point... so could very well find you're missing the end parts of the document (like the closing tags, </html> etc...)

    Could be something as simple as a return; ending the document premateurely...

    I'd need to see more of the code... because from what you've posted above i can't see anything that should cause a problem.

    Been a while since i've seen the mysql functions, last time i had to use them was when i was writing my own (very basic) CMS simply to learn code a little more.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags