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++;
}
Related
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());
}
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.
Been using the plugin Custom Content Type Manager to create a custom post that displays location information.
We hold weekly games for each location - So what I'm trying to accomplish is in my custom post I have a set of checkboxes so you check if the venue is played on Monday, Tuesday or Wed...etc Then in my theme I'm going to have a 7 day calendar. And if a location is checked for that day then I want the title/links to the location printed there.
I'm giving you that background because I really dont think I'm going about this the correct way. Essentially I'm doing it in a loop, and I'm pulling all the checkbox options in an Array, and if the option is equal to Monday to that specific day, then it prints the locations title name etc.
I want this setup so a non-technical person (kinda like me lol) can just add a new location and pick "friday" for example and the code does the rest.
Essentially I got it working. 2 problems though
I'm running 7 loops to accomplish this - one for each day. I know this is stupid and there is probably a better solution.
It's printing the correct information - however its also reading/printing each of the other locations except its not putting up the info for them - I know this cause its creating empty DIVs for them.
NOTE: I'm having issues posting the whole code...?? I deleted all the php tags to present this
$weekly = new WP_Query( array( 'post_type' => 'locations', 'posts_per_page' => 5 ) );
while ( $weekly->have_posts() ) : $weekly->the_post();
<div class="weekly-venue-spacer">
$day_array = get_custom_field('weekly_day:to_array');
if (in_array('3', $day_array)) {
print_custom_field('venue_display_name');
echo "<br />";
print_custom_field('city_crossroads');
}
</div>
endwhile;
wp_reset_postdata();
the '3' in the in_array statement just means "Wednesday".
Here look at this image:
http://i40.tinypic.com/svnee0.jpg
an example of the empty DIVs being created - easily seen with padding applied to the div
Thanks for reading. Any solution to approach this differently would be great.
I am not sure I understood correctly what you want , but assuming I did - I think your whole approach is a bit wrong/complicated .
First of all, you do not need 7 loops .
I have noticed have a custom field - so in that custom field , instead of an array, just store the ONE day that you need , and then simply GET by checking the custom_field value ..
Second - why do you use checkbook and not a list ? is there an eventual event that can be in several different days ? because if every event is exclusive for one day - than it would be more easy to use a drop list or even radio buttons.
and for your direct question - I do not know how the value of the custom field is structured - but you are printing ALL of it ..
EDIT I : After reading comment and understanding better the problem -
While still thinking that the approach is a bit wrong , but not knowing exactly how you construct the data - I will address the IMMEDIATE problem :
The code creates empty DIVS simply because you tell it to .
you are using a WHILE condition in the code BEFORE outputting a div.
Since your query gets 5 posts - it will create 5 divs (some of which that do not meet the NEXT condition , will be of course empty).
your function now , put in human-words is working like this :
1. Get 5 post.
2. As long as I have posts (for each post), Open a div.
3. If you have Tuesday in array - print something
4. close div
5. if not finished all posts (in our case, 5) - go back to step 2.
It is obvious that the code will print empty div also for empty events..
So to get it right you simply move the opening div tag to BEFORE the WHILE condition .
that is if you do not need to check for existence of events in the query ..
The right way would be to use also the IF statement, just like the regular wordpress loop.
The general mechanism is this :
<?php if ($weekly->have_posts()) : ?>
//now we open a div
<?php while ( $weekly->have_posts() ) : $weekly->the_post();?>
// now we check for other conditions and print them if available.
<?php endwhile; ?>
// now we close the DIV
<?php endif; ?>
First question here, though reading and searching has saved my bacon a number of times.
I'm a PHP hack at best, and a Wordpress theme I'm building is forcing me to learn a lot.
I've run into a problem in trying to get the next & previous links' formats to display differently based on their respective categories (and not the single.php's page).
Here's my best try so far:
<?php next_post_link(
'%link',
if ( in_category( 'tweet' ) ) {
get_the_content()
} else {
echo '%title'
};,
FALSE
)?>
This results in a syntax error.
My first question is can I even use an if-else statement within a function's parameters?
If I'm approaching it in completely the wrong way, then it's back to the drawing board. I'd appreciate any alternate suggestions, then, as well!
I know the following work:
next_post_link( '%link', get_the_title(), FALSE );
next_post_link( '%link', '%title', FALSE );
To try and be more clear, I'm looking to affect output of the next/previous links based on the category of the links themselves, and not the present host page they are on.
The problem I'm having is a) how to determine what category the next_post_link() and previous_post_link() are, and then b) display their Link parameter accordingly. If I may throw up the white flag, the examples I've found using get_adjacent_post() and various arrays and the like seem way over my head at this point. I'd really appreciate someone walking me through any solutions using them.
What to aim for?
I think you're mixing stuff a bit too much which makes your problem more complicated than it needs to be. I know with many new things, this can happen quite easily, so I think it's worth to spend some thoughts about the what to be done before starting to code. It should help in the end.
Start to formulate what you want to achieve. If I understand you right you write that you want to display the next and prev links below the current post differently based on category.
But which category? The one of the current post that displays? Or the category of the prev/next linked post?
Next to that, posts can have multiple categories. Do you want to change the display based on one or on multiple of or even all of the categories?
And finally: Do you want to output tons of HTML tags for the visual styling? Or do you want to use CSS for that.
How to do it?
After you have thought about what you actually want to achieve, you can make your mind how this can be done.
In the following I'll show some example code that will do the following:
Explains the use of the link-format parameter you want to change.
Shows how to get the CSS-classes for the links in question.
Extends a theme's output with these CSS-classes.
Make it re-useable for different files within your theme.
So this example covers the categories of the linked posts for visual styling via your Theme's CSS which I think is the preferable way.
The PHP code
PHP is pretty flexible so it's in the end easy to get that done, but without knowing what exactly to do, this flexibility in PHP can turn out to be a problem because it's also possible to loose the trail and then code something non-working.
Let's just say you just want to change the link-format for the next_post_link() function.
The function will replace %link with the HTML link tag (<a href=...>Post Title</a>), so if you take 'This is the next post of my blog: %link; waiting to be read!' as parameter, you've added some text around the link.
<?php
$format = 'This is the next post of my blog: %link; waiting to be read!';
next_post_link($format);
?>
You can additionally add HTML tags around it as well:
<?php
$format = '<span style="font-size:2em">%link</span>';
next_post_link($format);
?>
Which will make all next post links having a double of their standard font-size.
So now to make this a bit more easy for themeing, best would be to add css classes of the category/ies then you can change the display easily within the CSS.
<?php
$inSameCategory = true; // depends on what you want, can be false as well
$nextPost = get_adjacent_post($inSameCategory, '', false);
$cssClasses = get_post_class('next-post-link ', $nextPost);
$format = sprintf('<span class="$s>%link</span>', $cssClasses);
next_post_link($format);
?>
This will wrap the next post link into a <span> containing all CSS-classes of the linked post plus a next-post-link class in case you need this for the CSS-selector to style the links. Here some example CSS:
.next-post-link.category-tweet {font-size: 2em;}
Making it reuseable
As you have a next and prev link you could copy over that code only for changing one parameter. But we're lazy and instead we wrap it into a function of it's own:
/**
* my theme's format string for prev|next_post_link()
*
* #param bool $previous (optional) previous (true, default) or next (false) post
* #param bool $inSameCategory (optional) use same category, default: true
* #return string
*/
function my_prev_next_link_format($previous = true, $inSameCategory = true) {
$postId = get_adjacent_post($inSameCategory, '', $previous);
$cssClasses = get_post_class('next-post-link ', $nextPost);
$format = sprintf('<span class="$s>%link</span>', $cssClasses);
return $format;
}
Place this function into your theme's functions.php. It's available then in all your templates. Using it becomes now very easy:
<?php prev_post_link(my_prev_next_link_format()); ?>
...
<?php next_post_link(my_prev_next_link_format(false)); ?>
If you've put this all together, you can easily style the next and prev links within your theme's CSS.
Have you tried anonymous functions? In that way you can use if-else statements within a function
Try:
$format_string = in_category('tweet') ?
'<span style="color:red;">%link</span>' : '%link';
next_post_link($format_string);
The second and third parameters are optional and default to title and false.
Edit:
I don't know this plugin, but..
if(in_category('tweet')){
$format_string = '<span style="color:red;">%link</span>';
$link_text = get_the_content();
}else{
$format_string = '%link';
$link_text = '%title';
}
next_post_link($format_string, $link_text);
I want to display certain text in all my posts except if it's in this one category. How do I do that? Oh yea I almost forgot I want to include the title of the post in the text. So I think I need to use echo, cat='-5', and or something?? I don't know how to form it though. Thanks!
You could use the Wordpress function in_category(). When you use it inside the loop, it returns true if the current post is a member of the category you passed it.
<?php
if ( in_category('my-category'))
{
// don't output text
} else {
// do output text
}
?>
Do you require the text to be completely locked away, or just hidden from view? If you only need it hidden from view (but accessible to anyone who chooses to pry) then you may be able to do it very quickly using css.
If you have coded your theme - or are using someone else's - that adds helpful styles into the header, you may have a lot to work with already. For example, this is a body declaration generated by the Thematic theme:
<body class="wordpress y2011 m01 d31 h12 archive category category-orthopaedics">
Say you have a chunk of content to hide:
<div class="text_to_hide">This is what gets hidden.</div>
Then you declare the CSS as:
.category-orthopaedics .text_to_hide { display: none; }