PHP WordPress How To Display All Of Your Post Tags

PHP WordPress How To Display All Of Your Post Tags

Here is how you can display all of your post tags very easily. Add the code below to your functions.php and just place the shortcode wherever you want the tags to be shown – a post, a page, a widget, or anywhere else.

function display_all_post_tags($attributes)
{
    $tags = get_tags('post_tag'); //taxonomy=post_tag
    
    $string =<<<CSS
<style>
.tagbox { 
background-color:#eee;
border: 1px solid #ccc;
margin:0px 10px 10px 0px;
line-height: 200%;
padding:2px 0 2px 2px;
 
}
.taglink  { 
padding:2px;
}
 
.tagbox a, .tagbox a:visited, .tagbox a:active { 
text-decoration:none;
}
 
.tagcount { 
background-color:black;
color:white;
position: relative;
padding:2px;
}	
</style>
CSS;
    
    foreach ($tags as $tag) { 
        //$string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a><span class="tagcount">'. $tag->count .'</span></span>' . "\n";
        $string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a></span>' . "\n";
    } 
    return $string;
    
}
add_shortcode('display_all_post_tags', 'display_all_post_tags');

The code includes some simple styling, which you can modify to suit your own WordPress theme. On top of that, there is a commented line that is like so

//$string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a><span class="tagcount">'. $tag->count .'</span></span>' . "\n";

Currently, the code doesn’t display the number of post the tag is linked with. If you wish to display the post count, simply uncomment the above line of code in the snippet given above, and comment out the existing

$string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a></span>' . "\n";

If you need any help, let me know in the comments below.

Enjoyed the content ? Share it with your friends !

PHP WordPress Print List Of Categories And Their Information

PHP WordPress Print List Of Categories And Their Information

A quick copy-paste function to print all your categories and dump some of their crucial information out.

function print_all_categories_html()
{
    $categories = get_categories( array(
        'orderby' => 'name',
        'order'   => 'ASC'
    ) );
     
    foreach( $categories as $category ) {
        $category_link = sprintf( 
            '<a href="%1$s" alt="%2$s">%3$s</a>',
            esc_url( get_category_link( $category->term_id ) ),
            esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
            esc_html( $category->name )
        );
         
        echo '<p>' . sprintf( esc_html__( 'Category: %s', 'textdomain' ), $category_link ) . '</p> ';
        echo '<p>' . sprintf( esc_html__( 'Description: %s', 'textdomain' ), $category->description ) . '</p>';
        echo '<p>' . sprintf( esc_html__( 'Post Count: %s', 'textdomain' ), $category->count ) . '</p>';
    } 
    
    return $categories;
}

 

Enjoyed the content ? Share it with your friends !

PHP WordPress Delete Duplicate Post By Title And Category

PHP WordPress Delete Duplicate Post By Title And Category

I have been looking online for a code solution that is able to check through specific categories for duplicate posts and never been able to found a solution good enough for me. Solutions online were slow and memory-consuming which was a major reason why I couldn’t use them. So I wrote my own.

For the code below to work, you need to ensure that you sort your posts in alphabetical order. The logic is that you have a inner-loop that checks from the given post (Post A) till the last post for any duplicate titles. The logic at the same time ensures that Post A will never be deleted, ensuring you have at least 1 copy of the post.

$args = [
    'post_type'     => 'post',
    'post_status'   => 'publish',
    'category_name' => $category->name,
    'orderby'   => 'post_title', 
    'order' => 'ASC',
    'post_per_page' => -1,
    'nopaging' => true,
];
$my_query = new WP_Query( $args );
$posts = $my_query->posts;

    $num_of_posts = count($posts);
    echo "category : {$category->name} <br/>";
    echo "num_of_posts : {$num_of_posts} <br/>";
    
    for($j = 0; $j < $num_of_posts; $j++)
    {
        $post = $posts[$j]; // this post will be the post that doesnt get deleted, if any duplicates exist down the line //
        $current_title = $post->post_title;
        
        for($k = $j+1; $k < $num_of_posts; $k++)
        {
            $next_post = $posts[$k];
            $next_title = $next_post->post_title;
            $next_id = $next_post->ID;
            
            if( strcmp($current_title, $next_title) == 0 ) {
                echo "Duplicate for {$current_title} with ID {$next_id} will be deleted <br/>";
                wp_delete_post($next_id, false); // move to trash first
            }
        }
    }	
}

 

Enjoyed the content ? Share it with your friends !

Setup PHP WordPress Dev Envrionment for Windows

Setup PHP WordPress Dev Envrionment

Requirements :

    1. Have a local php dev environment setup (MAMP/XAMPP/..)

If you have not already setup your local dev environment, you can do so by following my tutorial here

Now that you have a local php dev environment, installing WordPress is very easy.

  1. Go to the wordpress download page here and download the latest wordpress.
  2. Extract the archive to the directory of your web server. The default for XAMPP is C:\xampp\htdocs
  3. Start up Apache and MySQL
  4. Open a browser and go to http://localhost/wordpress/ and you will be greeted a page as shown below
  5. Go to your phpMyAdmin and create a database named wordpress_dev. You need not create any tables. You can refer to the images below
    xampp phpmyadmin
    xampp phpmyadmin
    xampp phpmyadmin home
    xampp phpmyadmin home
    xampp phpmyadmin create tables
    xampp phpmyadmin create tables

    xampp phpmyadmin create database
    xampp phpmyadmin create database
  6. Go back to the browser and continue with the installation, filling up the necessary details. The default username and password for MySQL in XAMPP is root and nothing/blank(there is no password)
  7. Click Continue and setup some of your other WordPress settings
  8. Your done !
Enjoyed the content ? Share it with your friends !