Updates from February, 2018 Toggle Comment Threads | Keyboard Shortcuts

  • Jay Versluis 5:17 pm on February 28, 2018 Permalink | Reply  
    Categories: WordPress ( 144 )   

    How to add code to the header in WordPress 

    WordPress has a hook that lets us add arbitrary HTML code to the <header> tag of a website. Several plugins can be found that accomplish this, but it’s so easy to do that a plugin is often overkill.

    Here’s how you can execute an arbitrary PHP function using the wp_head hook:

    // write some text as part of the header 
    function writeSomeText () { 
        echo 'Hello from my new header function.'; 
    } 
    add_action('wp_head', 'writeSomeText');
    

    This example inserts some text into the <header> portion of the website. HTML tags can simply be written using echo, including double quotes.





     
  • Jay Versluis 11:07 am on February 19, 2018 Permalink | Reply  
    Categories: WordPress ( 144 )   

    How to show a list of all articles in WordPress 

    There’s a handy function in WordPress called wp_get_archives(). With it we can create a lot of useful output with just a few lines of code.

    To list all articles ever published on a site, we can do this:

    Here we setup a list of arguments and then give it to the function, which in turn gives us a nicely formatted list of every published article.

    If we set the show_post_count argument to true, and replace the type argument to something like “yearly” or “monthly”, we’ll get clickable a list similar to this:

     

    • April 2015 (20)
    • March 2015 (11)
    • February 2015 (2)
    • January 2015 (7)
    • December 2014 (4)
    • November 2014 (12)

    The number in brackets shows up when the show_post_count argument is set to true instead of false.

     

     

     

    We can even find out how many articles have been published on the entire site by using the wp_count_posts() function. Here’s how to use it:

    $totalPosts = wp_count_posts()->publish;

    $totalPosts now holds the amount published posts. We can also query drafts or posts of any particular post type too if we wanted to (such as all status updates or video posts).





     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel