wordpress: previous_post_link() / next_post_link() placeholder - wordpress

I am having trouble with the previous_post_link() and next_post_link() functionality. When there is no previous post, the function previous_post_link() does not display a link, likewise for the next_post_link() and the last post. I would like to have a placeholder image so that the design stays consistent.
Currently I have images of green arrows pointing left and right, I would like to place an image of a grey arrow if there are no more posts to go back to.
Is there a way to use the next_post_link()/previous_post_link() functions but not have the link removed.
I also wonder if there is a way for the links to cycle, so that if you come to the most recent post, the next post link would bring you back to the first post.
************ UPDATED ************
Here is the code, based on "silent's" advice (accepted answer) to use get_adjacent_post():
<?php
if(get_adjacent_post(false, '', true)) {
previous_post_link('%link','<img src="larr.gif"/>');
}
else {
echo '<img src="larr2.gif"/>';
};
if(get_adjacent_post(false, '', false)) {
next_post_link('%link','<img src="rarr.gif"/>');
}
else {
echo '<img src="rarr2.gif">';
};
?>

So you can "capture" what next_post_link() and previous_post_link() return using ob_start() and ob_get_clean(), then apply a conditional to it.
Code in practice:
$previous_string = "<-Back";
ob_start(); // start output buffering
previous_post_link("%link", $previous_string);
$previous_link = ob_get_clean(); // stop output buffering and store
if ($previous_link == '') {
echo '<span style="color: #ccc">' . $previous_string . '</span>';
} else {
echo $previous_link;
}

I never try this myself. However, you may refer to this post. It uses get_adjacent_post().

Why cant you try the below?
<div style="float:right"><?php if(get_previous_posts_link()) { previous_posts_link('Newer Entries »'); } else { echo 'No Newer Entries'; } ; ?></div>
Alternatively to display Next and Previous Post Links to Blog post you can easily do that. This is very well explained at globinch.com (WordPress Tips : How to Add Next and Previous Post Links to Blog? )
http://www.globinch.com/2010/10/12/wordpress-tips-how-to-add-next-and-previous-post-links-to-blog/

Related

Where exactly is the "Before Header Content hook" in Wordpress?

I want to add a little code to the "Before Header Content hook" but I don't know where that is... Can you please help me?
Try:
add_action('init', 'process_post');
function process_post()
{
echo "test";
}
this is a modified version of #ajay 's solution
if you are going to use this, then you have to make sure that the current user is not an admin using is_admin() function.. and only display it if he is not an admin ...
why !?
because if you didn't, it may mess up the wp-admin of your website..
add_action('init', 'process_post');
function process_post()
{
if (!is_admin()) {
echo "test";
}
}
This could be tricky simply because every theme differs with how the loop is displayed, however you could create a plugin to use the loop_start action, which is called before the first post of the standard WP loop:
add_action( 'loop_start', 'test_loop_start' );
function test_loop_start( $query ){
echo 'this is my inserted text';
}
Now using this would display it every single time the loop is called (whether on a page, a post, category page, search page, etc.), which you may not want.
add_action( 'loop_start', 'test_loop_start' );
function test_loop_start( $query ){
if(is_category() OR is_singular()) {
echo 'this is my inserted text';
}
}

Is there a concise Wordpress function for building a page link from an ID?

I'm currently building links like this:
<?php echo get_the_title(111); ?>
I was building links like this using the WPML plugin (but steering away from it due to various reasons):
<?php icl_link_to_element(111); ?>
This builds a link similar to my first example.
So my question is is there a native Wordpress function that does this? I'm sure there must be, but cannot find the solution anywhere. I'm looking to reduce my markup...
Thanks!
EDITED WITH ANSWER
This is how I built my custom function:
function build_pretty_link($id,$link_title='') {
if($link_title=='') {
$link_title = get_the_title($id);
}
$link_url = get_permalink($id);
echo "{$link_title}";
}
WordPress give a function that print an anchor tag with the title and the url, but you have to be in a the loop (http://codex.wordpress.org/Function_Reference/permalink_anchor).
I suggest you to create your own function (the functions.php file in your theme is here for that).
You can do someting like that :
function vp_link_to($post_id) {
echo '<?php echo get_the_title($post_id); ?>';
}
get_permalink(x);
Where the ID of the page is x and wrap this in whatever you need, so
$id = 10;
$link = get_permalink($id);
echo 'Linked text';

hatom-entry errors on the Google snippet test

Almost sure that I'm not the first one that has this question but when I test my (WordPress) page on Google Snippet Test Tool I got hatom-enty errors like:
hatom-feed
hatom-entry:
Fout: At least one field must be set for HatomEntry.
Fout: Missing required field "entry-title".
Fout: Missing required field "updated".
Fout: Missing required hCard "author".
I found some tutorials about this but that is for standard WordPress themes. I'm using Inovado from ThemeForest and I can't figure out in which file I have to edit this data.
Someone familiar with this?
I also got problems with snippet review... Good in testresults but doesn't show up in de Google Search Results. Don't know why...
You can Add this code to the function.php file in your theme's directory and it will solve the problems.
//mod content
function hatom_mod_post_content ($content) {
if ( in_the_loop() && !is_page() ) {
$content = '<span class="entry-content">'.$content.'</span>';
}
return $content;
}
add_filter( 'the_content', 'hatom_mod_post_content');
//add hatom data
function add_mod_hatom_data($content) {
$t = get_the_modified_time('F jS, Y');
$author = get_the_author();
$title = get_the_title();
if(is_single()) {
$content .= '<div class="hatom-extra"><span class="entry-title">'.$title.'</span> was last modified: <span class="updated"> '.$t.'</span> by <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
}
return $content;
}
add_filter('the_content', 'add_mod_hatom_data');
The code contains 2 functions. The first function will use the WordPress filter hook for “the_content” to add the class of “entry-content” to the article. To add in the other essential hAtom fields, the second function will add a short sentence to the end of your post article, which contains updated time, post title and author, with required microdata.

Wordpress: Add content to edit.php

I'm trying to find out what action hook/filter I can use to insert content on the admin "edit.php" page (i want to place a few links above the 'posts' table)? I've found "edit_form_after_title" and "edit_form_after_editor" (these do exactly what I want to do, but they are for posts.php, not edit.php).
With the help of this answer: How do you create a custom edit.php / edit pages page
I came up with this:
<?php
# Called only in /wp-admin/edit.php pages
add_action( 'load-edit.php', function() {
add_filter( 'views_edit-talk', 'talk_tabs' ); // talk is my custom post type
});
# echo the tabs
function talk_tabs() {
echo '
<h2 class="nav-tab-wrapper">
<a class="nav-tab" href="admin.php?page=guests">Profiles</a>
<a class="nav-tab nav-tab-active" href="edit.php?post_type=talk">Talks</a>
<a class="nav-tab" href="edit.php?post_type=offer">Offers</a>
</h2>
';
}
?>
And it looks like this:
If you just wanted to add to the post title link you could do something like this
if (is_admin()) {
add_filter('the_title', function($title) {
return $before_title . $title . $after_title;
});
}
however, it doesn't sound like you want to add text to the title link.
To add html after the title and before the actions links, you could do like this
if (is_admin()) {
add_filter('post_row_actions', function($args) {
// echo your custom content here
return $args; // and dont forget to return the actions
});
}
There is also page_row_actions for the page edit screen (post_row_actions is only for posts)
As far as adding stuff before the title, I don't see a hook/filter to do that. See wp-admin/class-wp-posts-list-table.php line 463 function single_row if you want to look for yourself.

targeting title in wordpress post

I am working on a wordpress plugin that modifies the title of a post. I only want to do this when I am viewing a single post. To be specific, I want to add a link beside the title, but for purposes of the question, I will be adding some arbitary text.
I started out by using the 'the_title' filter hook, and calling this function.
function add_button_to_title($title)
{
global $post;
if(is_single())
{
return $title.'googly googly';
}
return $title;
}
The problem is, the links on the side bar apparently also use 'the_title', as I saw my text showing up in the side bars as well, which led me to:
if(is_single() && in_the_loop())
But then, in my theme(and i suppose themes in general) there is a link to the previous post and next post, which also uses 'the title' filter. So finally I have:
if(is_single() && in_the_loop() && ($post->post_title == $title))
The last conditional basically makes sure that it is the title of the post that is being printed, not the title of the next or previous post. This works but I am not sure how well it will work given different themes...It seems like terribly hacked together. Any advice from wordpress gurus out there? I am worried that the title would be modified for other reasons and the conditional will fail.
Any help is appreciated!
Ying,
There isn't really a good solution except, as ShaderOp said, requiring theme modification. Your solution will work for the most part. The only exception is if the theme developer has changed the query in a page. I'd say this is probably a good enough solution that it'll cover more than 95% of the cases you'd run into.
I solved a similar issue by adding a check to see if the title being filtered matches the title of the post. This avoids the issue with other post titles on the page (in sidebar, menu) also getting filtered.
function add_button_to_title( $title ) {
global $post;
if( is_single() && $title == $post->post_title ) {
return $title . 'googly googly';
} else {
return $title;
}
}
Wouldn't it be easier to keep the original version of your add_button_to_title function, but instead of hooking it to a filter, call it directly from your single.php page in the appropriate place?
For example, somewhere in your theme's single.php, instead of this:
<h3 class="storytitle">
<?php the_title(); ?>
</h3>
Use this:
<h3 class="storytitle">
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php echo add_button_to_title(the_title('', '', false); ?>
</a>
</h3>
today I ran into a similar problem. the_title gets called several times accross the whole page (e.g., in the html-head, the menus, the sidebar). I followed a similar approach using conditionals and the post/page id.
Additionally, I added a boolean flag which is set to true using the 'the_content' filter. So the title gets changed until the content is displayed. This way, I ensure that sidebars/widgets are not affected (e.g. Thematic theme has a default widget with links to pages - here the other conditionals would not be helpful as get_the_id() would return an equivalent). This ONLY works if the theme uses sidebars on the right. I did not find a way yet how to hook in directly before the 'the_title' call for the page/post to enable the boolean flag.
function myplugin_adjust_title($title, $id) {
global $myplugin_title_changed;
if ($myplugin_title_changed) {
return $title;
}
if (in_the_loop() && is_page('myplugin') && $id == get_the_ID()) {
$title = '';
}
return $title;
}
add_filter('the_title', 'myplugin_adjust_title', 10, 2);
function myplugin_adjust_title_helper_content($content) {
global $myplugin_title_changed;
$myplugin_title_changed = true;
return $content;
}
add_filter('the_content', 'myplugin_adjust_title_helper_content');

Resources