Wordpress, if function returns "nothing" - php

I am building a wordpress theme I want to have the next and previous DIV only appear IF their is a next and previous button available.
If there is 10 posts and the display limit is 10 posts I don't want an empty div. However when there are 20 posts and the display limit is 10 I want the next and previous buttons to show in there own div.
I got this far and realized that posts_nav_link() doesn't return null.
function next_previous_div(){
print '<!-- ';
$output = posts_nav_link();
print " -->\n";
if ($output != null)
{
echo '<div class="float post" style="text-align:center">';
echo posts_nav_link();
echo '</div><!-- end post-->';
};
};

There are a few different ways you could test if you'll have a previous/next link:
Test $wp_query->max_num_pages
Get a global reference to the $wp_query object and see if it's greater than 1. If yes, you've got paging links:
global $wp_query;
if($wp_query->max_num_pages > 1){
posts_nav_link();
}
Capture the output of posts_nav_link()
This function echos by default, which is why your null test isn't working. You need to capture the echoed output and then test:
// Capture the echoed output in the $links variable and test on it
ob_start();
posts_nav_link();
$links = ob_get_clean();
if(strlen($links) > 0){
echo $links;
}

I think the closest you will get without custom scripting will be...
if (!empty(get_posts_nav_link()) {
echo '<div class="float post" style="text-align:center">';
echo posts_nav_link();
echo '</div><!-- end post-->';
}

Related

ACF Repeater field [shortcode] - do not show anything if subfields are empty

I am trying to create a TO DO list with ACF Advanced Custom Fields in Wordpress.
What I want to achieve is a shortcode that will display the TO DO repeater wrapped in Div tags with H3 title.
But IF the sub-fields are empty, nothing should show up, not even the H3 title.
I got as far as this:
add_shortcode( 'TO-DO-LIST', 'my-to-do-list');
function my-to-do-list($atts){
if(!function_exists('get_field'))
return;
ob_start();
// check if the repeater field has rows of data
if( have_rows('acf_to_do_repeater_group') ):
echo '<div id="to-do-list-wrapper">';
echo '<h3>TO DO:</h3>';
echo '<div class="to-do-content-wrapper">';
echo '<ul class="to-do-list-wrap">';
?>
<?php
// loop through the rows of data
while ( have_rows('acf_to_do_repeater_group') ) : the_row();
// display a sub field value
$content = get_sub_field('to-do-repeater-subfield');
echo '<li>' . $content . '</li>';
endwhile;
echo '</ul></div></div>';
endif;
$output = ob_get_clean();
return $output;
}
It works for getting the values, so if the rows have input, everything shows up correctly, however I can't seem to be able to figure out how to hide the entire thing when the rows are empty.
Currently even if the rows are empty, it still shows the list.
What am I doing wrong here?
Add a count on the have rows function: if its returning a empty set for some reason for 0, you can increase the count to 1.
if( have_rows('acf_to_do_repeater_group') && count(have_rows('acf_to_do_repeater_group')) > 0):

Wrap outputs of a Wordpress query every second item

I think I am pretty close to the solution but I cannot figure out the logic for where I should close the last div.
I have a wordpress query which requests four posts.
I want to wrap every two items in <section class="sponsor-row">
So I have implemented a counter in my query that counts each post, and if it finds two posts have been output it closes the <section> div off.
But I cannot work out how I should work out if the row should be closed again.
Can anyone figure this out? How can I make it wrap every two outputs in <section class="sponsor-row"> ?
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$counter = 0;
echo '<section class="sponsor-row">'; // Opening the section here
if ( has_post_thumbnail() ) {
$counter++;
echo '<div class="grid half">';
echo '<a class="sponsor" href="'.get_the_permalink().'" target="_blank">';
the_post_thumbnail( 'full' );
echo '</a>';
echo '</div>';
if ($counter <= 2){
echo '</section>'; // closing the section if two posts
$counter = 0;
}
}
}
}
Full query here: http://pastebin.com/e6RzZLR5
if you do if ($counter <= 2){ then it will close it each time it is less or equal 2, which means it will close it twice for each item. You should use if ($counter == 2){ and $counter = 0; should be at the top before the query, otherwise you will set it to 0 each loop.
See comparison in php

next_posts_link() in the same category

I was wondering if it's possible to limit next_posts_link() and previous_posts_link() to a specific category in Wordpress.
I've got a website where I display one post every page. Clicking on next and previous arrow, it goes to next or previous post. However I would like to detect if the next or previous post is in the same category BEFORE loading it and remove prev/next arrow in case the post is not in the same category.
Here my partial code:
<?php if(in_category( '1' )) {
echo '<section class="next_nav">';
next_post_link('%link', '<img src="http://www.mywebsite.it/wp-content/uploads/2014/11/next_post.png">', TRUE);
echo '</section>';
echo '<section class="previous_nav">';
previous_post_link('%link', '<img src="http://www.mywebsite.it/wp-content/uploads/2014/11/previous_post.png">', TRUE);
echo '</section>';
}
else {
echo '';
}
?>
When I click on next button, I get the next post but it's not in the same category. I was wondering if it was possible to use get_next_post() or get_adjacent_post() together with the previous function.
How can I get rid of it ?

PHP Generated product pages e.g: Page: [1] 2 3 4

I have a product page that is generated using WordPress to get the products and display them, however I want to be able to limit the amount visible at one time.
WordPress/PHP generation code:
$pages = get_pages(array('parent' => $post->ID, 'hierarchical' => 0, 'number' => $postsPerPage, 'sort_order' => 'ASC', 'sort_column' => 'menu_order'));
foreach ($pages as $page) {
$content = '<div class="pcp-product-display">
<div class="pcp-left-product-display-container">
<h2>' . $page->post_title . '</h2>';
$content .= '<div class="pcp-img-container">'. get_the_post_thumbnail($page->ID, 'thumbnail') .'</div>
</div>
<div class="pcp-right-product-display-container">
<div class="pcp-product-excerpt">' . get_field('page_excerpt', $page->ID) . '</div>';
$content .= '<div class="price"><p> £' . get_field('product_price', $page->ID) . '</p></div>
<div class="pcp-product-display-cta">
<h3>More Information</h3>
</div>
</div>
</div><!--
-->';
echo $content;
}
Here is what the page looks like:
I have got the "Items per page" at the top working, but I'd like that to work in conjunction with the pages.
I would like to do this using "product pages", for example you can see products 1-10 and there are 3 further pages to cycle through. (Page: [1] 2 3 4)
I'm not sure how I would go about getting the "product pages" to work.
Would it be best to use AJAX and grab, say, products 11-20 and then display them?
Or should I use PHP and WordPress?
If you need more information I should be able to provide it.
get_pages() also has "offset" parameter which you should use to skip some content if you are not displaying first page. AJAX or not - depends on what you need.
You can pass page parameter trough url (those pager links) and in that case URL containing page number can be shared. But if you don't want that you can use AJAX to. It maybe gives nicer results, but is a bit hard to achieve. I vote for non-ajax solution.
Check out the functions I'm using for generating page links. You should call get_paging().
- prefix is part of links that should be added before the part pager creates,
- curr is the number of current page
- cnt is the total number of pages.
It's not perfect but it works for me. Feel free to adjust it to your needs.
function get_paging ($prefix, $curr, $cnt){
if($cnt<2) return ''; // no paging needed
$prev = '« PREV';
$next = 'NEXT »';
$ret='<div id="pager">';
if ($curr > 1) $ret.='<a class="pagingPrev" href="'.$prefix.''.($curr-1).'">'.$prev.'</a> '; // prev: -1 leap
else $ret.='<span class="pagingPrevDisabled">'.$prev.'</span> ';
$start = 1; // middle range links
$end = $cnt;
for ($i=$start; $i <= $end ; $i++){
$ret.=paging_number($i,$curr,$prefix); // middle range
}
if ($cnt > $curr) $ret.='<a class="pagingNext" href="'.$prefix.''.($curr+1).'">'.$next.'</a>'; // next: +1 leap
else $ret.='<span class="pagingNextDisabled">'.$next.'</span>';
$ret.='</div>';
return $ret;
}
function paging_number($num, $curr,$prefix){
if ($num == (int)$curr)
return '<span class="currPage">'.$num.'</span> '; // not a link
else
return ''.$num.' '; // link
}

Wordpress: Condition Tags PHP

This may be a simple one for you PHP experts out there. I need to give a certain <h1> to a post else show the page/post title.
I have this so far, it works if it is on a single post page, but when I am on a different page it just shows 'the_title' instead of the page title. I think its basically about calling a php function inside an already open php tag, if that makes sense. Here is the code:
<?php
if ( is_single() ) {
echo 'News';
} else {
echo the_title();
}
?>
The Wordpress tag for the page title is <?php the_title ?>
You are echoing 'the_title' as a string, you need to actually execute the function like so:
if ( is_single() ) {
echo '<h1>News</h1>';
} else {
echo '<h1>' . the_title() . '</h1>';
}
Note the closing quote to halt the string, and the . to concatenate the WordPress function the_title(), and then another to join the ending <h1> tag.
A cleaner way is to add the tags inside the function itself, like this:
<?php the_title('<h1>', '</h1>'); ?>
No need for 'echo'.

Categories