Example:
We got some nice posts that got a link to jump to the next post with scrolling down the page. No reload, new tab or anything just a simple smoothscrolling of the page.
Problem:
We are inside a Wordpress Loop that creates some content from the DB and want to generate a link that jumps to the next run-through that will be generated by the Loop. Within the loop we cant predict what will be the next post/product or whatever post-type the loop should go through. So how am I giving the Link the correct anchor link?
Possible Solutions:
Maybe we can store the data from each run in an array?
Maybe we should create a second loop that only saves the data?
Maybe I should just use JS to rename that anchors after it finishes...
You can add an incremental id with the loop index and assign the anchor to the current index + 1. A simple example:
<?php
global $wp_query;
$args = array(
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
$index = $wp_query->current_post + 1;
?>
<div id="my_post_<?php echo $index; ?>"><?php the_title(); ?></div>
<?php
if (($wp_query->current_post +1) <= ($wp_query->post_count)) {
echo 'Next post';
}
?>
}
}
?>
Related
I've successfully been able to count the number of posts within a loop before, but for some reason this time it won't work.
<?php $count = 0; if (have_posts () ) { while (have_posts()) { the_post(); $count++; ?>
<div class="post-<?php echo $count; ?>"></div>
<?php } } ?>
Anything Im missing?
UPDATE
After talking to #FlashThunder about this problem. it seems that putting the beginning of the loop in the home.php template and using get_template_part() for the posts that are to be counted won't work unless they are in the same template, otherwise we would need to use global $count. After putting all of the php in the same template, the posts counted and applied the numeric class to the div's like I wanted.
I'm trying exhaustively to get a specific row from within a loop using the_meta() array in Wordpress and assigning a variable to it (like the procedural while loop). Right now it simply pulls all of the rows at once. The result from this query returns 3 rows from the database. What I am trying to do it wrap a div tag around one of the returned rows that is stored within the_meta();.
I've tried exploding the array and returning the first row but it just returns everything all at once. I just want to get a row and put it in a variable so that I can style it using CSS.
Here is the code:
$args = array(
'post_type' => 'membersprofile',
'posts_per_page' => 20);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$newvar = explode(" ", the_meta());
echo $newvar[0];
endwhile;
Any help would be greatly appreciated!
EDIT: Thanks to Youn for pointing me in the right direction and finding the answer for me, The problem was I was using the_meta() which only returned the whole rows. By using get_post_meta I was able to assign variables to each row returned. The code that works is:
$key_2_value = get_post_meta(get_the_ID(), 'wpcf-shortbio', true);
// check if the custom field has a value
if($key_2_value != '') {
echo $key_2_value;
}
Hope this helps someone else!
Try using get_post_meta() in place of the meta. It will return the meta value for a specific page/post.
For more info visit at http://codex.wordpress.org/Function_Reference/get_post_meta
In loop you can use
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<p><?php echo post_custom( 'my_meta_key' ) ; ?></p>
<?php endwhile; endif; ?>
I'm trying to add a button to my site which links to the next page of posts. I'm looking for a way to add an HTML link to the button so it actually works.
http://codex.wordpress.org/Template_Tags/wp_link_pages
I believe that explains what I want, but I'm not having any luck trying to get those snippets to work.
My button looks like:
Next Page
How can I link the button to the next page function in Wordpress?
EDIT:
My problem is I can't figure out how to associate this code with a button.
There are two 'built-in' ways of doing this in Wordpress:
1) If you need access to the link as a PHP variable, try Using get_next_posts_link
<?php $nextLink = get_next_posts_link( $label , $max_pages ); ?>
<?php $previousLink = get_previous_posts_link( $label ); ?>
From here the links are stored in variables and you can do whatever you'd like with them.
2) Otherwise use these:
<?php next_posts_link( $label , $max_pages ); ?>
<?php previous_posts_link( $label ); ?>
Where $label is the name of the link ( "Next Page" in your case ) and $max_pages is the max number of pages (if you want a limit), that the link shows up on.
If you want to style these, without having to enclose them inside another DIV, use Wordpress Filters
function apply_my_next_link_style ( ){
return 'class="button"';
}
apply_filters( 'next_posts_link_attributes', 'apply_my_next_link_style' )
3) If you need even more control you can try this, from the source code for the above functions:
if ( !is_single() ) {
echo '<a href="' . next_posts( 0, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', "Next Page") . '</a>';
}
Also, I would suggest asking these types of questions over at wordpress.stackexchange.com
Is necesary Snippets , visit this and this solve
<?php next_posts_link('Older Posts'); ?>
<?php previous_posts_link('Newer Posts'); ?>
I'm creating my own wordpress theme which is a bit different because it will not have single pages (or atleast, no single page will be reachable). The whole website contains just the homepage (with the loop) and previous posts pages.
I want to link to individual posts inside the loop like site.com#post-124 or site.com/paged=5#post-214.
I already created a function that does this:
function getPermalink($id,$postsPerPage) {
$postNumber = Get_Post_Number($id);
//a function that get's the post number based on
//the chronical order of published posts.
$page = floor(($postNumber - 1) / $postsPerPage);
$url = get_option('home');
if($page > 0) {
$url .= '/?paged=' . ($page + (1 - floor($page / 5)));
}
$url .= '#post-' . $id;
return $url;
}
You can see it live here: http://mijnrealiteit.nl (the previous posts pages are replaced by an infite scroll plugin).
This works, however it breaks when I start adding posts because all the posts before will get shifted back to pages further away (this makes the link invalid).
The way I see it there are two possible solutions:
Change the permalinkstructure to display paging backwards (so x.com/paged=231 becomes the first 'previous' page. However this is not userfriendly.
Make links with just the ID and let wordpress handle custom redirection to the page at that current point in time.
Are there better alternatives? I'm sure this is already solved somewhere, I just couldn't find it.
I got pushed in the right direction by a friend, I build it quite easily using option 2:
The getPermalink function is now much simpler:
function getPermalink($id) {
return get_option('home') . '/?f=' . $id;
}
I didn't make any custom redirection, I just checked at the homepage for a an 'f' being passed in the GET request:
$perma = $_GET['f'];
if(isset($perma) && !is_paged()) {
$customposts = get_posts('p=' . $perma );
foreach( $customposts as $post ) :
setup_postdata($post); ?>
//load the post
<?php endforeach;
}?>
If that is true the post will be fetched using the get_posts function by Wordpress. I also check the (normal) loop for the post that already has been served:
<?php while (have_posts()) : the_post();
if(get_the_ID() != $perma) { ?>
//load the post
<?php } endwhile; ?>
I'm creating a custom Google map that plots multiple markers.
View the link as it will make it easier to explain what is happening and what I want
If you click on each marker it shows company names that are grabbed from the child pages. At the moment it's showing ALL the company names on each marker. How can I show just one company name per marker? i.e So one say "MediWales" and the other says "Teamworks Design & Marketing", and so on when I add more companies.
Here's the code controlling the little popup:
<?php $pages = get_pages(array('child_of' => 1873, 'sort_column' => 'menu_order'));
$counter = 1;
foreach($pages as $post)
{
setup_postdata($post);
$fields = get_fields(); ?>
<p><?php $counter++; echo $fields->company_name;?></p>
<?php } wp_reset_query(); ?>
Once it's looped through once, the next time it loops through I need it to start on the next child and not show the first one.
UPDATE:
It seems like it's very close, it's showing one company but the same one on both markers.
<?php
$counter = 1;
$pages = get_pages(array('child_of' => 1873, 'sort_column' => 'menu_order', 'offset' => $counter, 'number' => 1));
foreach($pages as $post) {
setup_postdata($post);
$fields = get_fields(); ?>
<p><?php echo $fields->company_name; echo $counter; ?></p>
<?php $counter++; }
wp_reset_query(); ?>
Your issue is the fact that PHP is by nature preprocessed. JavaScript doesn't run that PHP everytime you click that marker.
Your best bet is to output a JSON object from PHP containing all the markers and their attributes, then parse it dynamically with JavaScript.