1

I need to query the WordPress database and display the data on one of my pages. I've looked and search for tutorials online but none of them are really complete.


I am trying to display a list of all the tags on my website on one of my pages. I know the best way to do this is with php. I've googled and binged but all they give me is code doesn't work or it probably works but it's put in the wrong location. The point is that I haven't found complete instructions to get this done. From what I've binged and google the block of code below is supposed help me display my tags list. Taken from here.

$tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {
    $tag_link = get_tag_link( $tag->term_id );

    $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
    $html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;

However, if you go to my website page, it doesn't show anything although I've got plenty of tags in the site. I looked at the code and it's very logical and looks like it's supposed to work. But being somewhat a noob in wordpress php coding, I feel like I'm making a fundamental mistake in where I should put this code for it to work on my site.

1

Looking at your page the content is empty.

<div class="post_tags"></div>

It seems you aren't inserting that PHP code correctly into your page. To do so you have to two options:

Without using a plugin.

Add the following code in your functions.php

function list_all_tags( $atts ) {
    $tags = get_tags();
    $html = '<div class="post_tags">';
    foreach ( $tags as $tag ) {
        $tag_link = get_tag_link( $tag->term_id );

        $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
        $html .= "{$tag->name}</a>";
    }
$html .= '</div>';
echo $html;
}
add_shortcode( 'sc_list_all_tags', 'list_all_tags' );

Then, on your page, you will use that shortcode, [sc_list_all_tags] and all the tags will be shown, I just used your code, it works, but the tags are looking a bit ugly, I know you can format them right.


Using a plugin.

You have to install Insert PHP Code Snippet or a similar plugin, in this case you have to add the PHP code snippet on PHPCode Snippets.

enter image description here

In the PHP Code Snippets section you can see all the available snippets (already created).

enter image description here

Finally, you can copy the shortcode of the snippet you want to use and paste into your page.

  • is there a way to do this without plugins? I already tried wordpress.org/plugins/insert-php but I think it has trouble finding the the global variable to get data from the database. I figure that I'm going to be using a lot of php from now on so I might as well learn how to do it the proper way without addons – answerSeeker Jul 11 '18 at 1:47
  • 1
    I just added a Without using a plugin alternative. – Castiblanco Jul 11 '18 at 1:59
  • I think I'm getting somewhere but after inserting the code in functions.php and putting the shortcode on the songs page, I clicked on update and it says error while savings. No tags are showing yet. – answerSeeker Jul 11 '18 at 3:18
  • Could you show me the error message? – Castiblanco Jul 11 '18 at 4:11
  • Here are the errors and configs: imgur.com/a/y18ROtr – answerSeeker Jul 11 '18 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.