showing fifth post in wordpress - php

in my wordpress website how can start showing posts from fifth post?
Because in top of my website there is a slide show that shows title of first five posts
and again under slide show shows first five posts and it is not beautiful.

You can use either get_posts or query_posts as a means to show the most recent posts excluding the first five. Please keep in mind query_posts directly alters the main loop by changing the variables of the global variable $wp_query.
This kind of seems like what you want though so an example would be to modify the index file of your theme to include:
query_posts('posts_per_page=5&offset=5');
Note: You can change the posts_per_page to what ever you desire. You also have many more options available to you.
For more detailed information please see the following documentation :
http://codex.wordpress.org/Template_Tags/get_posts
http://codex.wordpress.org/Function_Reference/query_posts
TLDR: Use the offset parameter in WP_Query.

If you are familiar with PHP this code will solve your problem.
Setup a logic to not show data for first 5 post as follows:
$i = 0;
while (have_posts()) {
++$i;
if ($i > 5) {
the_title();
// and more content
}
}
This will show data inside loop only if $i > 5.

Related

why is WordPress printing my array instead of just constructing it?

I am trying to apply a custom view to all the results of a visit to a taxonomy page, which involves wrapping the whole lot of results in some DOM boilerplate and then invoking the function to display each result as an element within that boilerplate. My strategy of choice was to step through each result, populated dynamically depending which taxonomy term has led them here, and fill a bucket with those results, then display each one appropriately.
In other words, I want the WordPress loop to tell me WHICH things to display, then my own iteration to decide HOW to display them. This seemed like a simple strategy, but loops like the following appear to be always displaying my entire bucket, although I am nowhere telling it to actually print that bucket. Narrowing it down to a minimal example I have the following, which still is printing out $bucket. What is going on?
$bucket = array();
while ( have_posts() ) {
the_post();
$bucket[]=the_meta(); // this is all printed to the screen. Why?
}
the_meta(); // this is all printed to the screen. Why?
Wordpress has this concept of the_{name of function} and get_the_{name of function}. For example, the_title and get_the_title. the_title will echo out the title into the page but get_the_title will return the title instead.
the_{name of function} will echo out the result.
get_the_{name of function}, on the other hand, will return the result.
Read more on that
If you need to return the result instead of echoing it out into the page then use this version of the function: get_the_{name of function}.
So you could use the following code:
$bucket = array();
while ( have_posts() ) {
the_post();
$bucket[]=get_post_meta(get_the_ID());
}

inserting divs after other divs via PHP

I have a loop code within Wordpress that displays all of my wordpress posts / divs and it looks like this:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
Then of course within that loop, there are my divs that the posts are in and then some end php code.. but here is what I want to do, how I did it, and why it didn't work.
I have about 60 posts on my mainpage with all the same div classes for each posts (of course) and I wanted to insert other divs before or after some of my post divs. And the way I went about doing this was pretty idiotic looking back.
I used CSS's nth-child to select my div posts, then give them margins or clears to make room for the other divs that I was going to insert. So for example, I'd have 60 posts / divs, 5 divs per row.. on the 10th post, I'd give it say a margin-left of however many pixels to make room for the div that needed to be inserted within these posts. Then when it came down to insert the div into the open spot, I'd float it right (because the div needed to be inserted on the right) and also a margin-top of however many pixels until it fit correctly into the open spot.
I thouht this worked correctly, but since my post divs width's are percentages and not pixels (my posts are thumbnails that need to be rescaled on resolution, so I used percentages), the divs that were inserted within my posts would not be in the correct spot when the window changed size, or when being viewed on a different resolution... so what did I do? Change all of the margin values for each div that need to be inserted beside these posts into percentages instead of pixels... This almost worked.. the only problem was that when you rescaled your browser, the divs that were inserted into my posts would move up or down 5-20 pixels.. which sucks!
Then later today, I had an epiphany.. and it was to use PHP to insert the divs within these posts instead of my stupid CSS ideology, I have no idea how it would be done, but I do think it's possible for some strange reason.
So what I would need to do would be insert these div classes called "doublearticlewrapperad" amongst these other div classes that are posts.. and these post classes are called "doublearticlewrapper". I would like to be able to position my doublearticlewrapperad div classes amongst the other doublearticlewrapper loop posts / div classes. So say I wanted to insert a doublearticlewrapperad div after the 6th, 29th and 40th doublearticlewrapper post div's.. I would like to be able to do that with ease.. if it's even possible.
Just to clear up any confusion..
doublearticlewrapperad = divs I would like to insert after the div classes that get displayed via my WordPress Loop.. and
articlewrapperad= divs that are displayed as posts via my wordpress loop.
If there is any way to do this I thought php would be the proper way, but if it turns out I am wrong and there is an alternative method or different code to do this.. please let me know. And guys, any help would truly truly be appreciated, I really mean it.
Try this bud!
$count = 0;
while ( have_posts() ) {
if ( ($count % 9) == 0 ) { //this will show anything you add on the condition in every 9th posts.
//code to output your doublearticlewrapperad should go here
}
// output the post
the_post();
$count++;
}
Cheers!
I think something like the following should work. Note that I removed the if statement you had, which was pretty useless, as you then repeat the check with the while statement.
$insert_after = array(6,29,40);
$post_count = 0;
while ( have_posts() ) {
if (in_array($post_count, $insert_after)) {
//code to output your doublearticlewrapperad should go here
}
// output the post
the_post();
$post_count++;
}

Wordpress - duplicate search results listed?

The theme used is CookingPress v1.2. - http://bit.ly/zgCtE0
Problem:
If "test-blog-post" has 5 tags and is returned in search results, then it will be listed 5 times in the results list.
Question:
Is there something I can add to the code below to correct the search results from being duplicated? If not, is there any other code that might cause this?
Below is the code that renders the search results (from loop-search.php).
<?php if (!have_posts()) : ?>
// No Results
<?php endif; ?>
<?php while (have_posts()) : the_post(); ?>
// Show Posts
<?php endwhile; ?>
That is not the complete code. You have cut out the important stuff after "//Show Posts". Please post that code and please post the code that queries the database. I ask because I don't think that it is a WordPress native query. That is, I don't think vanilla Wordpress searches for tags at all. That means that this is a custom query or the developer has hooked into the WordPress query and altered it. Your question is not very answerable without that information.

Wordpress loop export category list to php echo

Am a bit stuck with a wordpress loop, am wondering if anyone can help.
I need to run a Wordpress loop but only get the category names/id (Either is fine) from each post and have all of those variables as one php item I can echo later in the page.
Its for a category list filter system, but I only want to show categories which have posted displayed on that page.
The loop will be dynamic as well, so I cant just hard code exclude/include, I need to echo the value of all the numbers in together.
I hope that makes sense! Anyone who has any ideas would be really cool. Thanks!
I would use the get_the_category function like so...
<?php
// before you begin the wordpress loop
$category_array = array();
?>
<?php
// from *within* the wordpress loop
foreach((get_the_category()) as $category) {
if (!in_array($category->cat_name, $category_array)) {
$category_array[] = $category->cat_name;
}
}
?>
<?php
// after the wordpress loop is finished
echo implode(",", $category_array);
?>
This code basically creates a new (empty) array so that for each category in the current page, check if you've already added that category name to the array, then if not, go ahead and add it. Then when the loop is finished, echo out a comma separated string of category names. (You can of course, change the separator if you want a comma and space ", " or any other delimiter).
The Codex article has a lot more information on other things you can do with that function. Hope that helps.
Edit: Fixed the implementation because I forgot this was going to be used on a page where you are listing many posts using the loop. (You need to initialize your array from outside the wordpress loop, then echo your results after the loop has been finished).

Separate Post and Pages from WordPress Search Results

I am looking for a way to present search results in my custom wordpress theme.
I was hoping being able to present the results like this:
Displaying 4 search results for "test"
Pages
testpage 1
testpage 2
Posts
testpost 1
testpost 2
I wrote a function is_type_page that I can use inside the loop (2 loops), but this breaks the pagination functionality.
Any suggestions how to achieve this?
I would run 2 separate loops on the page, after the first loop for pages run rewind_posts() and then run the loop again. Also the key to pagination is making sure the global $paged variable is being picked up on by both loops. $paged is how wordpress separates posts into pages. i.e. if you go to page 2 of something then the global $paged = 2.
Hope that helps
Multiple loops using rewind_posts here
Running two loops is the way to go if you want them displayed with separate headers. Here is code to get them to show intermingles as they come up by date created...
<?php while (have_posts()) : the_post(); ?>
<?php if ( $post->post_type == 'page' ) { ?>
**DISPLAY PAGE**
<?php } else { ?>
**DISPLAY POST**
<?php else : endif; ?>

Categories