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'); ?>
Related
So basically i have a main page and a footer page. Both are seperate .php files.
Im using ACF for this site.
Following the documentation, ive created a while loop for my 'flexible content' in the main page and it works, displaying all the data that gets looped and hooked from the CMS input fields.
My Problem is in the footer, i have a while loop that displays links, but it wont display unless i remove the while loop from the main page, then the links display in the footer.
I honnestly dot get why this happens ive tested allot and get my head wrapped around this, please help.
Main page code:
<?php
// check if the flexible content field has rows of data
if( have_rows('flexible_content_field_name') ):
// loop through the rows of data
while ( have_rows('flexible_content_field_name') ) : the_row();
// check current row layout
if( get_row_layout() == 'gallery' ):
// check if the nested repeater field has rows of data
if( have_rows('images') ):
echo '<ul>';
// loop through the rows of data
while ( have_rows('images') ) : the_row();
$image = get_sub_field('image');
echo '<li><img src="' . $image['url'] . '" alt="' . $image['alt'] . '" /></li>';
endwhile;
echo '</ul>';
endif;
endif;
endwhile;
else :
// no layouts found
endif;
?>
<?php get_footer(); ?>
Footer Code:
<div class="links">
<?php
if( have_rows('footer_page_links', 'option') ):
var_dump("test");
while( have_rows('footer_page_links', 'option') ): the_row();
?>
<p><?php the_sub_field('footer_link_name'); ?></p>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php wp_footer(); ?>
I would just like to add , not even the vardump() displays in the footer if main page while loop is implemented so it never gets inside the footer loop. The footer uses ACF option page - > LINK
Also all other option fields in footer displays, if its not within the while loop. I have removed the main page while loop then the footer while loop works, and this only happens with flexible content, my other pages with loops, that does non consists of flexible content works perfectly.
So This issue is resolved on my side, after contacting the ACF guys, they made a duplicate of what i did and could not recreate my issue.
Which got me thinking since i had the latest Wordpress (Version 4.9.7) the only difference is the hosting.
What i used on localhost was XAMP Version 3.2.2, which i did not think was the problem, but it was, so upgraded to a live server and everything works as expected, so for future ref should you run into these simple unexplained code errors, check the hosting, or upgrade.
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';
}
?>
}
}
?>
Basically, my Wordpress site has about 50,000 unique posts, I want to incorporate asdfly to the next/previous post link buttons. I want to insert asdfly/527275432/ before the next/previous url, so it would look like this..
asdfly/527274352/www.google.com/previous
(Previous Post Title)
asdfly/527274352/www.google.com/next
(Next Post Title)
Is there any possible way to do this? I tried with the template but I couldn't figure it out.. Someone had managed to help me before but the same code doesn't work on the new site I'm working with, this is a different theme so maybe that's why. Here is what my current next and previous post buttons are/look like:
<div class="right-side">
<?php
if( $prev_link != NULL )
echo $prev_link . "<i title='Last Episode' class='playerleft tooltips fa fa-angle-double-left'> </i></a>";?>
<a class="tooltips" title='View all <?php echo $name; ?> Episodes' href='<?php echo $series_link; ?>'><i class="allepisodes fa fa-sort-amount-asc"></i></a>
<?php
if( $next_link != NULL )
echo $next_link . "<i title='Next Episode'class='playerright tooltips fa fa-angle-double-right'> </i></a>"; ?>
</div> <!-- End of right side -->
This is the previous code someone has gotten for me, however it doesn't work on this website:
function modify_prev_link( $link ) {
$link = str_replace( "href=\"http://", "href=\"http://asdf.ly/527274352/", $link );
return $link;
}
add_filter( 'previous_post_link', 'modify_prev_link' );
function modify_next_link( $link ) {
$link = str_replace( "href=\"http://", "href=\"http://asdf.ly/527274352/", $link );
return $link;
}
add_filter( 'next_post_link', 'modify_next_link' );
An example of what this would look like on the page:
http://animewolf.tv/anime/yama-no-susume-2nd-season-episode-21-subbed/
If you refer to the buttons on the right side below the video player, I want to modify them so that the adfly code will come before the url of the next episode.
I've been going crazy trying to find an answer for this, if anyone knows of one I will tip you $5 paypal, just leave me your pp after I've confirmed it's worked :) just a little side note to show my appreciation.
Thanks
Not sure if this will work, but tested on wordpress with different template.
I tried with your button link which don't work on my templates.
Changed code for button link (most likely on single.php file)
<?php next_post_link( '%link', "<i title='Last Episode' class='playerleft tooltips fa fa-angle-double-left'> </i>", TRUE ); ?>
<a class="tooltips" title='View all <?php echo $name; ?> Episodes' href='<?php echo $series_link; ?>'><i class="allepisodes fa fa-sort-amount-asc"></i></a>
<?php previous_post_link( '%link', "<i title='Next Episode'class='playerright tooltips fa fa-angle-double-right'> </i></a>", TRUE ); ?>
edit on your function.php file (changing function) working fine.
Hope it help.
:)
It does what I want it to do, displays a certain category of posts in a column on a page. Right now its just the title, but I want to link that title and the permalink section isnt working, or rather the href.
[php]
// The Query
$the_query = new WP_Query( 'cat=3' );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul style="list-style:none;">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . '<a href="the_permalink();">' . get_the_title() . '[/a]'. '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
[/php]
It links to subdomain.domain.com/site/the_permalink(); instead pulling the permalink for that post and linking to it.
the_permalink(); return to echo your link.
You need to return only string. For this you can use get_the_permalink($post->ID);
Because your permalink function is inside of echo function.
When you enter the a-tag you start with HTML, if you close it you change to BBCode.
Fatih is right, you need to use get_permalink function. Since you're inside the loop, you don't need to specify the parameter ($post->ID).
Also, you need to switch back to PHP (that is your main problem) as you did with get_the_title function. There are multiple syntax issues with your code (brackets!).
The line should go like this:
echo '<li>' . get_the_title() . '</li>';
Learn the difference between echo and return in (not only) PHP functions!
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; ?>