Is there a way that I can change my pagination code to find the next or previous post even if there isn't a next post? So if I was on my first post it says "prev" and i want it to say next too and to find the last one posted. So it loops the posts. Not sure if this is possible
<div class="nav">
<ul>
<li><?php previous_post_link('%link', 'previous', TRUE); ?></li>
<li><?php next_post_link('%link', 'next', TRUE); ?></li>
</ul>
</div>
Yes, it is possible, through filters.
/**
* Functon creates default content for pagination function previous_post_link() and next_post_link()
* #param $original string HTML content returned by previous_post_link/next_post_link to be modified
* #return string
*/
function default_prev_next_link($original) {
//if prev/next link does not exists, $original is empty string
if(!$original)
return "<a href='#'>new content</a>";
//you need to return original value, in case it is set
return $original;
}
//hook our function to filters
add_filter('previous_post_link', 'default_prev_next_link');
add_filter('next_post_link', 'default_prev_next_link');
Eventually, you can create two separate functions, which you hook into *_post link separately
Related
I created a template (call it A.php) for a specific set of pages on my website.
How would one retrieve the list of medias (previously) attached to a page whose template is A.php in A.php?
I want to be able to iterate through that list of media in this A.php template. Here is a minimalistic version of A.php:
<?php
/**
* Template Name: test layout
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage jbWeb
* #since 1.0
* #version 1.0
*/
get_header();
query_posts(array('pagename' => get_query_var('pagename')));
while (have_posts()) {
the_post();
ob_start();
the_title();
$title = ob_get_clean();
echo $title;
$medias = get_attached_media( 'image', get_the_ID());
foreach($medias as $media) {
echo ('this is a url: ' . wp_get_attachment_url($media->id));
}
}
get_footer();
?>
apparently the only post that is retrieved from the while(have_posts()) loop is the page itself (or is it the content of the page itself?). This is what is shown on my browser:
The Url of any media attached to the page is never shown. And there are only two medias when I attached four photos to "pagetest1".
Here is what my admin page for the page "pagetest1" looks like:
Well here is a solution I have found which allows for any type of media to be displayed (you just have to tweak a part of the solution I will show how):
<?php
/**
* The template for displaying all pages
* Template Name: test layout
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage jbWeb
* #since 1.0
* #version 1.0
*/
get_header();
//the query to select the post id of the post containing all content of this page.
//have to replace pageTest by get_query_var('pagename')
$page_content = $wpdb->get_var(
$wpdb->prepare("
select post_content from (select max(post_date) as post_date from wp_posts
where post_title='%s' group by post_title) as sub,
wp_posts as p where p.post_date = sub.post_date ;",
get_query_var('pagename'))
);
$matches = array();
preg_match_all('/<img.*\/>/mU', $page_content, $matches, PREG_PATTERN_ORDER);
foreach ($matches as $m) {
foreach ($m as $m1) {
echo $m1 . "\n";
}
}
get_footer();
?>
Explanation:
The mysql query
So first there is that query on the mysql database that is done where I select the content of the most recent version of the post intrinsic to the page whose template is "test layout". I pass get_query_var('pagename') as parameter to this mysql query, this get_query_var function returns the current page name.
the pattern match
Then I find all occurences of the <img/> tag within the content of that post. About the m and U option within the pattern: m signifies that I want the input $page_content to be seen as a single line. And U signifies that I want the regexp to not be greedy, so that I find all tags and not simply one tag that would contain other tags.
printing the result
Finally the double foreach loop prints all <img/> tag within that resulting page.
Note: You could find all occurences of a <video/> tag in the same way. Just replace <img.*\/> within the pattern by <video.*\/>
I want to separate the three columns and each column having five rows in footer area of my wordpress site.. I don't know how to use short code for that.. Can anyone help me..
For example Like this footer I need using shortcode..
If you're intending to do it using a shortcode then use the following code to create it;
`<?php
function firstshortcode_function() {
*//Your Code Should Be Written Here!*
return 'My first shortcode';
}
add_shortcode('firstshortcode', 'firstshortcode_function');
?>`
.. and use this [firstshortcode] shortcode wherever you would like to call it.
Note: You can replace the function name with anything you like and replace the shortcode appropriately.
To add HTML content under it, use the following code;
<?php
function my_custom_shortcode( $atts ) {
/ Turn on buffering /
ob_start(); ?>
<div>
// YOUR HTML CODE GOES HERE
<h1>Test</h1>
<p>My content</p>
</div>
<?php
/ Get the buffered content into a var /
$sc = ob_get_contents();
/ Clean buffer /
ob_end_clean();
/ Return the content as usual /
return $sc;
}
add_shortcode( 'shortcode', 'my_custom_shortcode' );
?>
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.
i created a page template to be used as my front/home page with the wordpress loop in it, here is the code:
<?php query_posts('posts_per_page=10'); ?>
<?php
/* Run the loop to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-index.php and that will be used instead.
*/
get_template_part( 'loop', 'idex' );
?>
but the problem is that the qiuicktag <!--more--> is not working , they always show the whole content. isn't the <!--more--> stored in the database?
I think the tag is just stored inside the text of the field post_content in the wp_posts table.
If you do not want WordPress to show the whole content, then use "the_excerpt" or call "the_content" in your loop with the correct parameters:
http://codex.wordpress.org/Customizing_the_Read_More
Just use a conditional tag in your loop.php where you call "the_content":
if(is_home() || is_front_page()) {
the_excerpt(); // or the_content( $more_link_text , $strip_teaser, $more_file );
} else {
the_content();
}
I hope this solves the problem. Otherwise there could be a bug or problem with your template regarding the "the_content"-function.
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');