Querying Advanced Custom Fields; want to paginate the results - php

Not sure if this pertains specifically to Advanced Custom Fields, but I'll keep this short. I'm relatively new to PHP and want to know the best way of approaching this task. Also, this is a WordPress (4.1.1) environment.
I'm sending a query for all post types with an associated category type to pull in and display. If the query can find posts, it produces an <article> block with additional html for each result. Not to complicated right?
Now, I want to create a pagination element if/when the query produces greater than 8 results.
Environment with the front-end of the query results can be found:
http://test-hdwg.pantheon.io/news/
Template being utilized for this page (news.php) contains the following:
<?php /* Template Name: News */ ?>
<?php get_header(); ?>
<main id="primary" class="content-area">
<div class="jumbotron">
<div class="row">
<div class="intro">
<p><?php the_field('news_heading') ?></p>
</div>
</div>
</div>
<div class="news">
<div class="row news-articles">
<div class="column-wrapper">
<div class="small-4 columns news-left-column">
<div class="select-dropdown">
<button href="#" data-dropdown="dropdown-items" aria-controls="dropdown-items" aria-expanded="false" class="dropdown">Select a news category</button>
<ul id="dropdown-items" data-dropdown-content class="f-dropdown" aria-hidden="true" tabindex="-1">
<?php $args = array(
'exclude' => '',
'title_li' => __( '' ),
'show_option_none' => __( '<li>No categories</li>' ),
'taxonomy' => 'category',
'child_of' => 2,
'current_category' => 0
); ?>
<?php wp_list_categories($args); ?>
<li class="cat-item cat-item-all-news">All News
</ul>
</div>
<div class="news-block">
<span class="news-block-heading">Email Newsletters</span>
<div class="news-inner-block">
<?php if( have_rows('newsletters_list') ): ?>
<?php while( have_rows('newsletters_list') ): the_row(); ?>
<span class="year"><?php the_sub_field('newsletter_year'); ?></span>
<?php if( have_rows('newsletter_resource') ): ?>
<ul>
<?php while( have_rows('newsletter_resource') ): the_row(); ?>
<li><?php the_sub_field('newsletter_title'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
</div>
<div class="small-8 columns news-right-column">
<?php query_posts( 'post_type=post&cat=' ); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article class="large-6 columns ui-article-block">
<h3><?php echo get_the_date( 'd M Y' ); ?></h3>
<hr/>
<h2><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></h2>
<h1><?php the_title(); ?></h1>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 175) . "..."; // set a character cut-off at 175 characters
?>
</p>
Read More
</article>
<?php endwhile; ?>
<?php else : ?>
<div class="large-8 columns">
<p class="lead">Well this is embarrassing. There are currently no news articles.</p>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
</div>
</div>
</main>
<?php get_footer(); ?>
Just to be clear, this is not an issue, but more-so a request from more experience developers on how to approach a task utilizing Advanced Custom Field elements (if that matters).
Any help would be greatly appreciated!

EDIT
This edit is after clarification from OP on nature of the question.
The tutorial here, explains how to handle numeric navigation in posts at a beginner level.

Related

Wordpress Tags and Categories show on backend (post edit page) but not on frontend?

I've created a custom post type 'Projects' and attached a customer taxonomy 'Services' to it. I've set the taxonomy up as categories (I tried tags too and still have the same issue). It shows up in the wordpress admin and I'm able to create tags/categories on a project post. The tags/categories are saved in the database, because they still appear on the post edit page. However, I'm struggling to have them displayed on the front end. Please see code below from my function.php, archive-projects.php and single-projects.php. Please note, that the single-project.php file also includes a custom field called 'project-collection' which iterates through the respective project portfolio images and copy. Also, the tags are not rendered in the HTML at all (when inspecting element in Google Chrome). Thanks
functions.php
// Custom Post Types
function create_projects_post_type() {
$args = array(
'labels' => array(
'name' => 'Projects',
'singular_name' => 'Project'
),
'hierarchical' => false,
'menu_icon' => 'dashicons-portfolio',
'public' => true,
'has_archive' => true
);
register_post_type('projects', $args);
}
add_action('init', 'create_projects_post_type');
// Custom Taxonomy
function create_taxonomy(){
$args = array(
'labels' => array(
'name' => 'Services',
'singular_name' => 'Service'
),
'public' => true,
'has_archive' => true,
'hierarchical' => true
);
register_taxonomy('services', array('projects'), $args);
}
add_action('init', 'create_taxonomy');
archive-projects.php
<?php
get_header();
?>
<div class="row vertical-align d-flex align-items-center pt-0" data-aos="fade-up">
<div class="col-md-8 offset-md-2 col-lg-6 offset-lg-3 text-center z-index">
<h1>Our Projects</h1>
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form.</p>
</div>
</div>
</div>
<div id="particles-js"></div>
</section>
<section id="portfolio" class="container-fluid">
<div class="container-fluid">
<div class="row">
<?php if(have_posts('projects')) : ?>
<?php while(have_posts('projects')) : the_post(); ?>
<div class="col-md-6">
<figure data-aos="fade-up">
<a href="<?php the_permalink(); ?>">
<?php $image = get_field('project_main_image'); ?>
<img class="img-fluid" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
</a>
<figcaption>
<h3>
<a href="<?php the_permalink(); ?>">
<?php echo get_field('project_name'); ?>
</a>
</h3>
<dl>
<?php
$tags = get_the_tags();
if($tags):
foreach($tags as $tag): ?>
<dd>
<?php echo $tag->name; ?>
</dd>
<?php endforeach; endif; ?>
</dl>
</figcaption>
</figure>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
</section>
<?php get_footer(); ?>
single-projects.php
<?php
$GLOBALS['background'] = get_field('gradient_class');
get_header();
?>
<div class="row vertical-align d-flex align-items-center pt-0">
<div class="col-md-8 offset-md-2 text-center z-index">
<h1><?php the_field('project_name'); ?></h1>
<?php the_field('project_description'); ?>
</div>
</div>
</div>
<div id="particles-js"></div>
</section>
<?php if(have_rows('project_collection')): ?>
<?php $i = 1; ?>
<?php while(have_rows('project_collection')): the_row(); ?>
<section class="container-fluid portfolio-item">
<div class="container-fluid">
<div class="row d-flex align-items-center">
<div class="col-md-6 <?php if($i%2 != 0) : ?>order-md-2 <?php endif; ?>portfolio-image">
<?php $image = get_sub_field('image'); ?>
<img class="img-fluid" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
</div>
<div class="col-md-6 portfolio-content">
<h2><?php the_sub_field('sub_title'); ?></h2>
<?php the_sub_field('section_description'); ?>
</div>
</div>
</div>
</section>
<?php $i++; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php
// Get Tage NOT WORKING!
$tags = get_the_tags();
if($tags):
foreach($tags as $tag): ?>
<dd>
<?php echo $tag->name; ?>
</dd>
<?php endforeach; endif; ?>
<?php
// Get Catergories NOT WORKING!
$categories = get_the_category();
foreach($categories as $cat): ?>
<dd>
<?php echo $cat->name; ?>
</dd>
<?php endforeach; ?>
<?php get_template_part('includes/section', 'result-slider'); ?>
<?php get_footer(); ?>
I think instead of using this code block for your tag
$tags = get_the_tags();
if($tags):
foreach($tags as $tag): ?>
<dd>
<?php echo $tag->name; ?>
</dd>
<?php endforeach; endif; ?>
try this code block. I think it will solve your problem
$tags = wp_get_post_terms(get_the_ID(), 'Your tag/taxonomy slug name', array("fields" => "all"));
if($tags):
foreach($tags as $tag): ?>
<dd>
<?php echo $tag->name; ?>
</dd>
<?php endforeach; endif; ?>
explicitly to say instead of using get_the_tags() use wp_get_post_terms() function.
I hope this will solve your tag problem. :)
AR. Arif, thank you a lot for this answer. It helped me to solve the next problem. I added categories for portfolio items. The are ok in admin panel, but the output on frontend was strange. Categories of one portfolio item were output near another item.
So i replaced this
$portfolio_categories = get_the_terms(get_the_ID(), 'portfolio_category');
with this
$portfolio_categories = wp_get_post_terms(get_the_ID(), 'portfolio_category', array("fields" => "all"));
in theme's templates/taxonomy/categories-portfolio.php (in child theme of course)
and it works correctly finally.

Show three full posts on catgegory page, then rest of posts as titles only

I'm building a theme and on my category.php page I want to show several full posts (let's say 3, but need to be able to change this to 2 or 1 easily), and then the rest of the posts in the category as title links.
I have quite a bit of HTML in my loop for styling my posts and adding custom fields so sorry about all the code, but this is what my category.php page looks like now. I've tried a few things that haven't worked so have edited this to show my original code which just has a normal list of posts. I'm somewhat new to editing The Loop so would appreciate as much explanation/clarity as possible.
<?php
/**
* The template for displaying Category Archive pages.
*/
get_header(); ?>
<div id="primary" class="<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>">
<div id="feature-container" class="full-width-container">
<div class="full-width-container content-page" id="tagline-wrapper">
<div id="left-black"></div>
<div class="page-width-container">
<div id="tagline-box">
<h1 class="category-title">Transactions</h1>
</div>
</div>
</div>
</div>
<div id="content-wrapper">
<div id="project-menu" class="page-width-container">
<?php wp_nav_menu( array( 'theme_location' => 'project-types' ) ); ?>
</div>
<div id="content" role="main" >
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="story-container" class="module-container">
<div class="our-story">
<div class="story-image">
<?php
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="story-text">
<article class="post" id="post-<?php the_ID(); ?>">
<div class="entry-container">
<h2><?php the_title(); ?></h2>
<div class="project-details">
<p><span class="details-location"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-location', true);
wp_reset_query();
?></span><br />
<span class="details-funding"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-type', true);
wp_reset_query();
?> | <?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-source', true);
wp_reset_query();
?></span><br />
<span class="details-value"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-value', true);
wp_reset_query();
?></span></p>
</div>
<div class="entry">
<?php the_content(); ?>
<?php wp_link_pages(array('before' => __('Pages: ','html5reset'), 'next_or_number' => 'number')); ?>
</div>
<?php edit_post_link(__('Edit this entry','html5reset'), '<p>', '</p>'); ?>
</div>
</article>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div><!-- #content -->
</div>
</div><!-- #primary -->
<?php get_footer(); ?>
You can achive above thing using following code:
First you have to loop all post and and put counter when it reach more then 2 its stop to print a content.but title will be there always.
<?php $countPost=1;?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2 id="post-<?php the_ID(); ?>">
<?php the_title(); ?></h2>
<?php if($countPost>2) : /*Condition for Content*/
the_content();
endif;
?>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft">
<?php posts_nav_link('','','« Previous Entries') ?>
</div>
<div class="alignright">
<?php posts_nav_link('','Next Entries »','') ?>
</div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
<?php endif; ?>
</div>
For more details please refer :
https://codex.wordpress.org/The_Loop_in_Action
I figured out a bit of a workaround solution on my own, although it relies on using plugins/widgets which isn't what I'd prefer.
I simply set the Reading settings to display 2 posts, and then below the Loop I added a widget area and used the Recent Posts Extended widget to display a list of titles/links. This widget allows you to skip a certain amount of posts in the list, so I set it to start at post #3. There was no option to show posts from the current category only, so I had to use the Widget Context plugin as well and make individual widgets with a specific category to show on each corresponding category page. As I said, a bit of a convoluted solution, but the end result is exactly what I wanted to achieve.

wordpress permalink reload page and not redirect to post

I have WordPress onepage and I displayed posts from specific category in one of page.
When I click to link with permalink href, I am redirecting to home page with added /post-name/ to url, but no to post page. I have index.php and single.php.
I have this index.php:
<?php
query_posts(array(
'post_type' => 'page',
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'menu_order'
));
$tpl_parts = array(
'5' => 'about',
'7' => 'team',
'76' => 'tech',
'81' => 'services',
'101' => 'contact',
);
?>
<?php get_header('home'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if(array_key_exists($post->ID, $tpl_parts)) : ?>
<?php get_template_part('template-parts/'. $tpl_parts[$post->ID], 'template'); ?>
<?php else: ?>
<section id="<?php echo $post->post_name; ?>">
<div class="container">
<div class="row">
<?php the_content(); ?>
</div>
</div>
</section>
<?php endif; ?>
<?php endwhile; else : ?>
<?php endif; ?>
<?php get_footer(); ?>
This code show all pages in index.php by template part, it's working.
When I added to services page, a few posts like this:
<section id="services" class="services-section">
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2 class="text-left">Services</h2>
</div>
</div>
<?php $inner_query = new WP_Query( 'category_name=services' ); ?>
<?php if ( $inner_query->have_posts() ) : while ( $inner_query->have_posts() ) : $inner_query->the_post(); ?>
<div class="row box-service">
<div class="col-sm-6 col-xs-12">
<?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<div class="col-sm-6">
<h3><?php the_title(); ?></h3>
<p class="intro"><?php echo the_field('short_caption'); ?></p>
More
</div>
</div>
<?php endwhile; else: endif; wp_reset_postdata(); ?>
</div>
</section>
This code isn't working, becouse when I want to click on link and go to post, website is refreshing with url localhost/mywebsite/name-of-post/ but I want to redirect to post page. I have a single.php file:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
</div>
</section>
<?php endwhile; else: endif; ?>
<?php get_footer(); ?>
What's wrong ? How can I fix that ? My theme ignore files like page.php or single.php
Thanks for help.
Are You trying to use Wp_Query to single.php too ?
And maybe try to change posts_per_page in top index to number of your pages instead of -1.
you can try this for display single post page.
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
</h2>
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
Ok, after a few hours spending on this problem, I tried to find why page.php and single.php is ignored.
Polylang plugin - after turned it off. Everything working fine.
Thanks everybody for help me.

Wordpress: home page navigation screwed

Here's the website I'm talking about. If you scroll, when you reach the bottom you'll notice a link to go on with the page navigation (Pagina successiva). If you click it you'll get the same posts as the ones in home page! And it goes on. If you visit website.com/page/6/ you still get the home page posts.
Here's the index.php. What's wrong with it? :o
P.S.: Wordpress latest version. No child theme. Custom one I created:
<?php get_header(); ?>
<?php if (is_home() && !is_paged()) { ?>
<!-- editoriale -->
<div id="editoriale">
<?php $my_query = new WP_Query('cat=10&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<h2><?php the_title(); ?></h2>
<div class="postinfo" style="margin-top:-10px">
di <?php the_author(); ?> -
pubblicato il <?php the_time('j F Y') ?> -
<?php comments_popup_link('nessun commento »', '1 commento »', '% commenti »'); ?>
<?php edit_post_link('modifica',' - [ ',' ]'); ?>
</div>
<div id="editoriale-cont" class="entry">
<?php the_content(' Leggi il resto »'); ?>
</div>
<div class="postinfo" style="text-align:right;margin-top:10px">
Tutti gli editoriali »
</div>
<?php edit_post_link('Modifica', ' <div class="edit">', '</div>'); ?>
<?php endwhile; ?>
</div>
<!-- fine editoriale -->
<div class="hotnews" id="bombacalendario">
<div style="padding:10px 0 15px 10px">
<?php $my_query = new WP_Query( 'page_id=18570' );
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate2[] = $post->ID;?>
<h3><?php the_title(); ?></h3>
<div><?php the_content(); ?></div>
<?php edit_post_link('Modifica', ' <div class="edit">', '</div>'); ?>
<?php endwhile; ?>
</div>
</div>
<div style="margin-top:12px;padding-bottom:6px" id="cerca" class="hotnews">
<h3>Cerca nel sito</h3><p>
<form action="<?php echo get_option('home'); ?>/" id="searchform" method="get">
<p><input type="text" style="width:135px;margin-right:10px" value="" name="s" id="s"><input type="submit" value="Vai" id="searchsubmit"></p>
</form>
</div>
<div class="fine-blocco"></div>
<?php } ?>
<?php get_sidebar(); ?>
<!-- inizio contenuto -->
<div id="content">
<!-- post del blog -->
<?php query_posts( 'cat=-7' );
if (have_posts()) : while (have_posts()) : the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="postinfo">
di <?php the_author(); ?> -
pubblicato il <?php the_time('j F Y') ?><?php edit_post_link('modifica',' - [ ',' ]'); ?> -
<?php comments_popup_link('nessun commento »', '1 commento »', '% commenti »'); ?>
</div>
<div class="entry">
<?php the_content('[Continua »]'); ?>
</div>
</div><br style="clear:both">
<!-- fine post -->
<?php endwhile; else: ?>
<p><?php 'Spiacente, nessun risultato.'; ?></p>
<?php endif;?>
<p class="navigation">
<?php posts_nav_link(' - ', '« Pagina Precedente', 'Pagina Successiva »'); ?>
</p>
</div>
<!-- fine contenuto -->
<?php get_footer(); ?>
As you can see visiting the site, in the home page there's an hardcoded page (which remains in every next page, as it should, and then a "featured" post which shouldn't appear in next page). Problem is everything is screwed when you go to page 2, 3, etc :D
The reason the pagination is not working is that you have not added the correct parameters to the query_posts function.
You have this:
query_posts( 'cat=-7' );
But you need to add the paged parameter too. You may also want to add posts_per_page
See the Wordpress codex: http://codex.wordpress.org/Function_Reference/query_posts#Pagination
Also see this for getting the paged parameter: http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
Here is an example:
$args = array(
'cat' => '-7',
'posts_per_page' => 6,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )
);
query_posts($args);

add pagination to "for each" loop in wordpress

Im just wondering is it possible to use the pagination_links() function in a foreach loop in wordpress?
When I try it nothing happens, I have looked around and it seems this is a little trickier than I was expecting...
<?php
$args = array( 'numberposts' => 6, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="events">
<div class="newslistingblock">
<div class="newslistingblockheader"><p><?php the_title(); ?></p>
</div>
<div class="newslistingblockthumbnail">
<?php echo get_the_post_thumbnail( $post_id, 'news-thumb', $attr ); ?> </div>
<div class="newslistingexcerpt">
<?php the_excerpt( ); ?> </div>
</div>
</div>
<?php endforeach; ?>
Im basically looking for basic pagination, with "next", "prev" and numbers.
Any help on this would be great thanks.
EDIT:
I have decided to change the code to this to suit wordpress...
<?php
query_posts( 'posts_per_page=5' );
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<!-- Do suff -->
<div class="events">
<div class="newslistingblock">
<div class="newslistingblockheader"><p><?php the_title(); ?></p>
</div>
<div class="newslistingblockthumbnail">
<?php echo get_the_post_thumbnail( $post_id, 'news-thumb', $attr ); ?> </div>
<div class="newslistingexcerpt">
<?php the_excerpt( ); ?> </div>
</div>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('← Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries →') ?></div>
</div>
<?php endif; ?>
Why are you using foreach instead of while?
The default loop with pagenation should look like this (should work with foreach as well):
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- Do suff -->
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('← Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries →') ?></div>
</div>
<?php endif; ?>
This just shows the next and previous link, but if you want pagination with numbers, I would suggest the great plugin: Wp-Pagenavi.
Good luck!
EDIT:
The error you are experiencing is that you haven't set the paged variable correctly. You need to do the following:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=5&paged=' . $paged);
?>
Then everything should work.
You can find more information in the codex: http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

Categories