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






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.