Desizn Tech logo
  • Home
  • Freebies
  • Showcase
  • Tutorial
  • Photography

Useful WordPress Snippets and Queries to Customize Your Content

Posted November 15, 2012 & filed under Tips & Tools, Wordpress

Wordpres provides many useful functions to truly customize your theme. If you are familiar with PHP, wordpress conditional tags and template structure you can pretty much customize your website however you want. This also help you avoid installing too many plugins. In this post we will take those function a bit further.

List Post Any Where Using Custom Query

If you wanted to output posts from any category in your theme you can do so using following snippet. It uses wordpress Custom Query and get_post function to return post from any category you specify. Replace ‘category’ name or ID with your own category.

List WordPress Post
<div id="list_wp_post">
<p>More From WordPress Category</p>
  <ul>
   <?php
     global $post;
     $args = array( 'numberposts' => 6, 'category' => wordpress,'orderby' => 'date', 'order' => 'DESC');
     $myposts = get_posts( $args );
      foreach( $myposts as $post ) : setup_postdata($post);?>
      <li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
     <?php
    endforeach;
   wp_reset_query();
   ?>
 </ul>
</div>

You can basically throw in any argument you want to this query such as : multiple category ‘category__and’ => array(5,3), order by title ‘orderby’ => ‘date’ and more. Also, notice the wp_reset_query(); . You must reset the query every time you use this loop. Otherwise, it will conflict the main wordpress loop. Don’t forget to add in your CSS to change the look of the list.

Query Just A Single Post

If you just want to list a single post with image and more details you can use the snippet above but, if you need to just show one single post with title and description you can use get_post function. It takes a post ID and returns the database record for that post. It is very short and easy to use.

<?php
 $post_id = 14013; // ID of the post
 $post_id_14013 = get_post($post_id);
 $queried_post = get_post($post_id);
?>
<p><a href="<?php echo $queried_post->post_name ;?>"><?php echo $queried_post->post_title; ?></a><br />
<?php echo $queried_post->post_excerpt; ?></p>

The post_name string here returns the post’s URL slug/Link. Other strings you can use is post_category, comment_count and more. You can view the full list at wordpress codex.

Using Alternate Header, Footer and Sidebar

Although this sounds a like a lot of work this is one easily thing accomplish with wordpress. You can copy your existing header/footer/sidebar, rename it, customize and use it. Let’s say you wanted to use a different sidebar for blog posts then home page. Copy your current header and customize it and call it in your single.php file.


<?php get_header('single'); ?>

Wordpress Alternate Header

You can use this method for the header and footer as well. If you have custom page template you can also call your alternate header and footer to truly customize your theme.

<?php get_sider('single'); ?>
<?php get_footer('single'); ?>

Targeting Content for Different page

Now, what if you just wanted to have a different header image/text on blog posts, a single page or category? Instead copying header file you can use the following snippet :

<?php if (is_single()) {;?> <!--Content for Blog Post -->

   This will show up on blog post.

<?php } else if (is_page('123')){?><!--Content for page-->

  This will show up on blog specified page.

<?php } else{;?><!--Content for Blog Post -->

  This will show up all other pages.

<?php };?>

I have commented it out the code, so you can see where you can add content. You add HTML and PHP as well between the conditional statements. If you also wanted to target more specific pages you add more elesif statements. As, I said before you can use that snippet your header and footer to have different content. If you wanted customize content for category page you can use is_category conditional tag.

You can also mix this snippets for example: lets say On the homepage I wanted to load regular sidebar but one blog post I want to the single sidebar. Here is a conditional statement for that :

<?php if (is_single()) {;?>

 <?php get_sidebar('single');?> <!-- Load sidebar for blog post -->

<?php } else {;?>

 <?php get_sidebar();?> <!-- Load sidebar for the whole site-->

<?php };?>

If you ever run into any wordpress problem a great place to start is the wordpress forums and wordpress stack exchange . Feel free share improvement or additional functions for wordpress related to this post.

More about Kawsar Ali

About the author

Kawsar Ali is a web designer who also likes blogging. He is the curator of Desizn Tech. In his spare time he walks around with a Nikon D5100 hunting for good textures.

Tags: Wordpress


Related posts:
  • Remove WordPress Meta
  • WordPress Hacks and Tweaks to Visually Empower Your Website
  • WordPress Custom Fields and Hacks for Bloggers
  • WordPress Hacks for Members Only Website
  • How to Create WordPress Category Template Page?

Comments

Due to a high number of spam comments on site we have disabled comment system at desizntech.info. In a few days comments will be available again after we delete spam.

Cheap Tablet PC

Stay Connected

rss twitter facebook google pinterest
Get free updates in your inbox!

Subscribe via rss

Our Supporters

  • Cloud Storage
  • Dedicated Server
  • Modern Furniture
  • Online Business Printing
  • Online Backup
  • Logo Design
  • Wow Gold
  • Web Hosting UK
  • Secure and protect your business with SiteLock.
  • Festklänningar
  • Blogroll

    AnimHut
    Designmodo – Web Design
    Design Superhero
    Inspireyourway
    iBrandStudio
    OurTuts
    Skyje
    TextureQualityPro
    Stock Images
    Vector Pack
    365icon
    TexTuts
    • About
    • Contact
    • Contribute
    • Privacy Policy

    Copyright © 2008-2013 Desizn Tech

    Theme by Kawsar Ali.