Wordpress post loop, only shows variable when logged in as admin - php

The problem i have is when I am not logged into wordpress as the admin. It shows me the following thing on an events page.
http://www.productplusconcepts.nl/hiteclife/wp-content/themes/hi-tec/hl-step1.jpg
Now when I log into wordpress backend and go to the same events page it does show me the content in the square of the next picture.
http://www.productplusconcepts.nl/hiteclife/wp-content/themes/hi-tec/hl-step2.jpg
Here is my query for the loop.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args['post_type'] = 'evenementen';
$args['post_status'] = 'publish';
$args['relation'] = 'AND';
$args['order_by'] = 'datum_start';
$args['order'] = 'ASC';
$args['posts_per_page'] = 10;
$args['paged'] = $paged;
$args['meta_query'] = array(
array(
'key' => 'datum_eind',
'value' => date('Ymd'),
'compare' => '>='
)
);
if(!empty($_GET['maand'])){
$args['meta_query'] = array(
array(
'key' => 'datum_eind',
'value' => date('Ymd'),
'compare' => '>='
),
array(
'key' => 'welke_maand',
'value' => $_GET['maand'],
'compare' => '='
)
);
}
if(!empty($_GET['provincie'])){
$args['meta_query'] = array(
array(
'key' => 'datum_eind',
'value' => date('Ymd'),
'compare' => '>='
),
array(
'key'=> 'provincie',
'value'=> $_GET['provincie'],
'compare'=> '='
)
);
}
if(!empty($_GET['afstand'])){
$afstanden = explode(',', urldecode($_GET['afstand']));
$args['meta_query'] = array();
array_push($args['meta_query'],
array(
'key' => 'datum_eind',
'value' => date('Ymd'),
'compare' => '>='
)
);
foreach($afstanden as $afstand) {
array_push($args['meta_query'],
array(
'key'=>'afstand',
'value'=> $afstand,
'compare'=>'LIKE'
)
);
}
}
$overzicht = new WP_Query($args);
Now i work with 3 filters for the content it loads they work perfect but ill show the code within the loop.
if( $overzicht->have_posts() ) :
while( $overzicht->have_posts() ) : $overzicht->the_post(); ?>
<li>
<a href="<?php the_permalink();?>"><?php the_title();?>
<div class="click"></div>
<div class="extrainfo">
<span class="pull-right">
<?php echo '<span class="label label-info">' . get_field('plaats') . '</span>'; ?>
</span>
<span class="pull-left">
<?php
$afstanden = get_field('afstand');
//echo "<pre>";
//print_r($afstanden);
//echo "</pre>";
if(!empty($afstanden)) {
if(is_array($afstanden)) {
$items = array();
foreach($afstanden as $afstand) {
$fields = get_fields($afstand->ID);
$items[] = format_distance($fields['minimaal'], $fields['maximaal']);
}
if(count($items) > 1)
echo '<strong>Afstanden: </strong>';
elseif(count($items === 1))
echo '<strong>Afstand: </strong>';
echo fancify($items);
} else {
echo '<em>Geen afstand(en) beschikbaar.</em>';
}
} else {
echo '<em>Geen afstand(en) beschikbaar.</em>';
}
echo '<br>';
if(get_field('datum_start') && get_field('datum_eind')){
$date_start = datum_omzetten(get_field('datum_start'));
$date_eind = datum_omzetten(get_field('datum_eind'));
echo 'Van: '. $date_start . ' t/m: '. $date_eind;
}else{
echo datum_omzetten(get_field('datum'));
}
?>
</span>
</div>
</a>
<div class="clearfix"></div>
</li>
<?php
endwhile;
wp_reset_postdata();
else:
?>
<p>Helaas, er zijn geen evenementen gevonden. <br /><br>Wilt u een evenement aanmelden? Neem dan contact met ons op</p>
<?php
endif;
The only functions being called within the loop is format_distance and fancify within the template functions.php
Format Distance:
function format_distance($min, $max) {
if($min < $max)
return sprintf('%s-%skm', $min, $max);
elseif($min > $max)
return sprintf('>%skm', $min);
else
return '?';}
Fancify:
function fancify($items) {
if(is_array($items)) {
$str = '';
$size = count($items);
for($i = 0; $i < $size; $i++) {
$str .= $items[$i];
if($i < $size - 2)
$str .= ', ';
elseif($i < $size - 1)
$str .= ' of ';
}
return $str;
} else
return '?';}
I cannot seem to find the problem why the loop is only showing me the afstanden (distances) when I'm logged in.

Turns out this is a conflict between Wordpress Plugin Custom Advanced Fields and BBPress Forum. They both make use of get_field relation post object. Thats why i wasnt able to see the content when i wasnt logged in. To fix this problem add this to your theme's function file.
add_filter('acf/get_post_types', 'my_acf_post_type_filter', 20, 3);
function my_acf_post_type_filter ($post_types) {
$exclude = array('topic', 'reply');
// exclude
foreach( $exclude as $p )
{
unset( $post_types[ $p ] );
}
return $post_types; }

Related

How to display an alphabetically sorted list of post withe Php (WordPress )

I would like to create a glossary overview page on wordpress via PHP, which can be used via a shortcode. I would like that always the initial letter is displayed and then the individual topics (posts) which begin with this.
Example:
A
Apple
Apricot
B
Banana
Blackberry
and so on...
To implement this I use the following code:
// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'glossar',
'orderby' => 'title',
'order' => 'ASC',
));
echo '<ul>';
if( $all_posts->have_posts()){
foreach( range( 'A', 'Z' ) as $letter ) {
echo '<div class="group_letter"><div class="letter">' . $letter. '</div>';
while( $all_posts->have_posts() ){
$all_posts->the_post();
$title = get_the_title();
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper( substr( $title, 0, 1 ) );
if( $initial == $letter ){
echo '<li><a class="glossary-listing" href="/glossar/'. $name . '">' . $title . '</a></li>';
}
}
$all_posts->rewind_posts();
}
}
echo '</ul>';
}
add_shortcode( 'glossary', 'glossary' );
So far it works, but now it shows letters for which there are no posts. This is how it looks now
I have tried to do it with an if query, but so far, I am stuck. Can someone help me?
Best regards and thank you!
Sort the array using PHP sort() function then loop through the result
<?PHP
$list=['apples','popsicles','Zinger','donkeys','bananas','joe',
'Locusts','gazelles','Angels','Popsicle','Dongle','jump','cocoa'
];
//convert all elements to same case
//sorting will sort by case
$list =array_map('strtolower', $list);
//sort the array
sort($list);
$last_letter=null;
foreach($list as $item){
$current_letter=substr($item,0,1);
if($last_letter!=$current_letter){
?>
<div style="margin:1rem;padding:1rem;background:#f5f5f5;">
<?=$current_letter?>
</div>
<?php
$last_letter=$current_letter;
}
?>
<div style="margin:1rem;padding:1rem;background:#f5f5f5;">
<?=$item?>
</div>
<?PHP
}
?>
I am sure there is a better solution besides running 26 times through the while loop. Anyway, here is what you are looking for.
// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
[
'posts_per_page' => -1,
'post_type' => 'glossar',
'orderby' => 'title',
'order' => 'ASC',
]
);
echo '<ul>';
if ($all_posts->have_posts()) {
foreach (range('A', 'Z') as $letter) {
$foundPostable = false;
while ($all_posts->have_posts()) {
$all_posts->the_post();
$title = get_the_title();
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper(substr($title, 0, 1));
if ($initial === $letter) {
if ($foundPostable === false) {
$foundPostable = true;
echo '<div class="group_letter"><div class="letter">' . $letter. '</div>';
}
echo '<li><a class="glossary-listing" href="/glossar/'. $name . '">' . $title . '</a></li>';
}
}
$all_posts->rewind_posts();
}
}
echo '</ul>';
}
add_shortcode( 'glossary', 'glossary' );
As for improvement, something like this might work as well.
// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
[
'posts_per_page' => -1,
'post_type' => 'glossar',
'orderby' => 'title',
'order' => 'ASC',
]
);
echo '<ul>';
$startLetter = '';
while ($all_posts->have_posts()) {
$all_posts->the_post();
$title = get_the_title();
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper(substr($title, 0, 1));
if ($initial !== $startLetter) {
$startLetter = $initial
echo '<div class="group_letter"><div class="letter">' . $letter . '</div>';
}
echo '<li><a class="glossary-listing" href="/glossar/'. $name . '">' . $title . '</a></li>';
}
echo '</ul>';
}
add_shortcode('glossary', 'glossary');

Adding pagination to my custom blog page using a foreach post loop

I built a custom blog page in my custom wordpress theme, and I'm trying to add pagination to the blog page. Im' using a foreach loop, instead your standard "if while post" loop.
Everything is working find, however I'm not sure where to add "paged" as an argument.
Here is my code:
<?php if (is_page( 'Blog' )) : ?>
<?php
//Get the Posts
$posts = get_posts();
foreach ($posts as $post) :
setup_postdata( $post );
//Setup Post data
$haystack = get_the_category($post->ID);
$i = count($haystack);
$string = "";
for ($j=0; $j < $i; $j++) {
$string .= " ";
$string .= $haystack[$j]->slug;
}
$link = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large', false );
$href = get_the_permalink();
$theCat = wp_get_post_categories($post->ID);
if (has_post_thumbnail($post->ID)){
$theCols = 'span12';
$imgWidth = 'span4';
$contentWidth = 'span8';
} else {
$theCols = 'span12';
$imgContainer ='display: none;';
$contentWidth = 'width: 100%;';
}
?>
<div class="<?php echo $string;?>">
<div id="post-<?php the_ID(); ?>" class="post-content <?php echo $theCols;?> group nopad">
<div class="post-content--image <?php echo $imgWidth;?> <?php echo $imgContainer;?>">
<img src="<?php echo $link[0]; ?>">
</div>
<!-- Post Content -->
<div class="post-content--container <?php echo $contentWidth;?>">
<?php
$post_title = get_the_title();
$post_title = explode(' ', $post_title);
$title = '';
for ($i=0; $i < 5 ; $i++) {
$title .= $post_title[$i];
$title .= ($i == 50) ? "..." : " ";
}
?>
<p class="post-content--date"><?php echo get_the_date('d M Y'); ?></p>
<h4 class="post-content--heading"><?php echo $title;?></h4>
<p class="post-content--cat"><?php echo $string;?></p>
<div class="post-content--text">
<?php
if ($theCat){
$str = substr(get_the_excerpt(), 0,255);
} else {
$str = get_the_excerpt();
}
$n = strpos($str, '<a');
if ($n > 0){
$rest = substr($str, 0, $n);
echo $rest;
} else {
echo $str;
}
?> ...
</div>
<button class="see-more-btn">Read More</button>
</div>
</div>
</div>
<?php endforeach;
wp_reset_postdata();?>
<?php else : ?>
<p>Critiria Not Found</p>
<?php endif; ?>
what am I missing? Thanks for the help in advance.
If you are going to use the get_posts function you need to set the posts_per_page and offset parameters. You need to check the current page and set the offset according to how many posts you are showing per page and the current page. For eg. On Page 2 and showing 5 posts per page you need to set the offset to 5 in order to skip the first 5 posts.
Note: The posts_per_page parameter does NOT work without setting the offset parameter.
$args = array(
'posts_per_page' => 5,
'offset' => 0
);
$posts_array = get_posts( $args );
One other way is to use the WP_Query and instead of passing the offset argument you just pass the page argument only like the example below where get_query_var('paged') get the value of the ?paged=x and if it its not set will default to '1'.
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query = new WP_Query( array( 'paged' => $paged ) );
If you are going to use WP_Query you need to change from foreach to:
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$post_id = get_the_ID();
$haystack = get_the_category($post_id);
$i = count($haystack);
}
}
To output the pagination links after the WP_Query you can use the paginate_links function as below. The advantage of using WP_Query is that you will also get the total number of posts found matching the current query parameters in found_posts and other values which you might need like max_num_pages.
echo paginate_links( array(
'base' => '%_%',
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'prev_next' => true,
'prev_text' => __('« Previous'),,
'next_text' => __('Next »'),
'add_args' => false,
'add_fragment' => '',
) );
get_posts: https://codex.wordpress.org/Template_Tags/get_posts
WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
paginate_links: https://codex.wordpress.org/Function_Reference/paginate_links

Custom Pagination for Posts not working after page 11

I have created a section on home where i am showing 6 post starting from offset 3 and below that i add a custom pagination code.
I have total 93 published post and as i set offset 3 so the pagination is set for rest 90 posts.
For 90 posts, total number of links is 15 in pagination.
Pagination works fine till 11 page i.e mysiteurl/page/11. when i click 12 in the pagination links, it show me 404. It should show me next 6 posts.
Here is my code:
<?php
// function to get page number
function get_url_var($name){
$strURL = $_SERVER['REQUEST_URI'];
$arrVals = split("/",$strURL);
$found = 0;
foreach ($arrVals as $index => $value){
if($value == $name) $found = $index;
}
$place = $found + 1;
return $arrVals[$place];
}
function myprefix_query_offset(&$query) {
//Before anything else, make sure this is the right query...
if ( ! $query->is_home() ) {
return;
}
//First, define your desired offset...
$offset = 3;
//Next, determine how many posts per page you want (we'll use WordPress's settings)
$ppp = 6;
$page = get_url_var('page');
//Next, detect and handle pagination...
if ( $page ) {
//Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($page-1) * $ppp );
//Apply adjust page offset
$query->set('offset', $page_offset );
}
else {
//This is the first page. Just use the offset...
$query->set('offset',$offset);
}
}
add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
//Define our offset again...
$offset = 3;
//Ensure we're modifying the right query object...
if ( $query->is_home() ) {
//Reduce WordPress's found_posts count by the offset...
return $found_posts - $offset;
}
return $found_posts;
}
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
$page = get_url_var('page');
$paged = ( $page ) ? $page : 1;
if($paged == 1){
$custom_args = array(
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => $paged,
'offset' => 3,
'post_status' => 'publish'
);
}else{
$custom_args = array(
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => $paged,
'offset' => 3 + ( ($paged-1) * 6),
'post_status' => 'publish'
);
}
$wpb_ll_query = new WP_Query( $custom_args );
if ( $wpb_ll_query->have_posts() ) : while ( $wpb_ll_query->have_posts() ) : $wpb_ll_query->the_post();
?>
<div class="col-lg-4 col-sm-6 col-md-4 col-xs-6 rec-first1">
<a href="
<?php the_permalink();?>">
<div class="grid-latest-posts">
<?php
if(has_post_thumbnail())
{
the_post_thumbnail();
}
else
{
?>
<img class="no-image" src="/wp-content/uploads/2017/02/no-image.png">
<?php
}
$category_object = get_the_category(get_the_ID());
$category_name = $category_object[0]->name;
?>
<label><?php echo $category_name;?></label>
</div>
</a>
<div class="style-txt-nxt">
<span> <a href="<?php the_permalink();?>">
<?php
$string = get_the_title();
$post_title = (strlen($string) > 30) ? substr($string,0,30)."..." : $string;
?>
<?php
echo $post_title;
?>
</a></span>
<p id="art-date2">
<?php echo get_the_date(); ?></p>
<div class="line1">
</div>
<h3>
<a href="
<?php the_permalink();?>">read more</a>
</h3>
</div>
</div>
<?php
endwhile;
//wp_reset_postdata();
else : ?>
<p>
<?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
</div>
</section>
<!-- Pagination for posts -->
<section>
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<!--<h3><a id="more_posts">Load More</a></h3>-->
<?php
if (function_exists(custom_pagination)) {
custom_pagination($wpb_ll_query->max_num_pages,"",$paged);
}
wp_reset_postdata();
?>
</div>
</div>
</div>
</section>
I defined custom_pagination() function in function.php
<?php
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It's good because we can now override default pagination
* in our theme, and use this function in default queries
* and custom queries.
*/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}?>
Can anyone suggest me some solution to fix it.

Notice: is_main_query was called incorrectly

Was searching for two days what makes problem in my WordPress theme, and using debug I found this error, and that what I have in functions about that error code.
It displays notice on place where section box should be shown.
How can I fix this?
Notice: is_main_query was called incorrectly. In pre_get_posts, use the WP_Query->is_main_query() method,
// Filter to "pre_get_posts" to change query vars
add_action( 'pre_get_posts', 'dp_custom_get_posts' );
function dp_custom_get_posts( $query ) {
if(is_admin())
return;
$orderby = $query->get('orderby');
$order = $query->get('order');
// If no 'orderby' specified, get first sort type from selected sort types
$selected_sort_types = dp_selected_sort_types();
if(is_main_query() && !empty($selected_sort_types) && empty($orderby)) {
$_sort_types = array_keys($selected_sort_types);
$orderby = $_sort_types[0];
$query->set('orderby', $orderby);
}
// Reset query vars based orderby parameter
if($orderby == 'comments') {
$query->set('orderby', 'comment_count');
}
elseif($orderby == 'views') {
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'views');
// The arguments for BAW Post Views Count plugin
if(function_exists('baw_pvc_main')) {
global $timings;
$views_timing = $query->get('views_timing') ? $query->get('views_timing') : 'all';
$date = $views_timing == 'all' ? '' : '-'. date( $timings[$views_timing] );
$meta_key = apply_filters( 'baw_count_views_meta_key', '_count-views_' . $views_timing . $date, $views_timing, $date );
$query->set('meta_key', $meta_key);
}
}
elseif($orderby == 'likes') {
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'likes');
}
elseif($orderby == 'title' && !$order) {
// If order by title, and no order specified, set "ASC" as default order.
$query->set('order', 'ASC');
}
// Only display posts on search results page
if (is_search() && $query->is_main_query())
$query->set('post_type', 'post');
// Make tax_query support "post-format-standard"
$tax_query = $query->get('tax_query');
if(!empty($tax_query)) {
foreach($tax_query as $index => $single_tax_query) {
if(empty($single_tax_query['terms']))
continue;
$in_post_formats = (array)$single_tax_query['terms'];
if($single_tax_query['taxonomy'] == 'post_format'
&& $single_tax_query['field'] == 'slug'
&& in_array('post-format-standard', $in_post_formats)) {
// Get reverse operator
$reverse_operator = 'IN';
if(empty($single_tax_query['operator']) || $single_tax_query['operator'] == 'IN')
$reverse_operator = 'NOT IN';
elseif($single_tax_query['operator'] == 'AND')
break;
// Get "not in post formats"
$post_formats = get_theme_support('post-formats');
$all_post_formats = array();
if(is_array( $post_formats[0])) {
$all_post_formats = array();
foreach($post_formats[0] as $post_format)
$all_post_formats[] = 'post-format-'.$post_format;
}
$not_in_post_formats = array_diff($all_post_formats, $in_post_formats);
// Reset post_format in tax_query
$query->query_vars['tax_query'][$index] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $not_in_post_formats,
'operator' => $reverse_operator
);
}
}
}
return $query;
}
This is the code of an section box, or at least part of it, I'm not sure what I'm doing anymore:
function dp_section_box($args = array()) {
$defaults = array(
'post_type' => 'post',
'cat' => '',
'taxonomies' => array(),
'view' => 'grid-small',
'title' => '',
'link' => '',
'post__in' => '',
'posts_per_page' => '',
'hide_if_empty' => false
);
$args = wp_parse_args($args, $defaults);
extract($args);
$posts_per_page = absint($posts_per_page);
// Set default posts number if no specified
if(empty($posts_per_page)) {
if($view == 'grid-mini')
$posts_per_page = 8;
elseif($view == 'grid-small')
$posts_per_page = 6;
elseif($view == 'grid-medium')
$posts_per_page = 4;
elseif($view == 'list-small')
$posts_per_page = 3;
elseif($view == 'list-medium')
$posts_per_page = 2;
elseif($view == 'list-large')
$posts_per_page = 1;
}
$args['posts_per_page'] = $posts_per_page;
$args = dp_parse_query_args($args);
$query = new WP_Query($args);
// Output nothing if there is no posts
if(!$query->have_posts() && $hide_if_empty)
return;
// Output content before section
if(!empty($before))
echo '<div class="section-box section-before rich-content">'. do_shortcode(wp_kses_stripslashes($before)).'</div><!-- end .section-box -->';
// Section box begin
echo '<div class="section-box">';
global $section_view;
$section_view = $view;
// Get term name as title
$term = '';
$cat = '';
if(!empty($taxonomies['category']))
$cat = $taxonomies['category'];
if($cat)
$term = get_term($cat, 'category');
if(empty($title) && $term)
$title = $term->name;
if(empty($link) && $term)
$link = get_term_link($term, 'category');
$title = '<span class="name">'.$title.'</span>';
// Add link to title and more
$more = '';
if($link) {
$title = '<a class="name-link" href="'.$link.'">'.$title.'</a>';
$more = '<a class="more-link" href="'.$link.'"><span>'.__('More', 'dp').' <i class="mini-arrow-right"></i></span></a>';
}
// Output section header
echo '<div class="section-header"><h2 class="section-title">'.$title.'</h2>'.$more.'</div>';
// Output section content
echo '<div class="section-content '.$view.'"><div class="nag cf">';
while ($query->have_posts()) : $query->the_post();
get_template_part('item-video');
endwhile;
wp_reset_postdata();
echo '</div></div><!-- end .section-content -->';
the solution is given in your error itself "use the WP_Query->is_main_query() method"-
try this -
if($query->is_main_query() && !empty($selected_sort_types) && empty($orderby)) {
$_sort_types = array_keys($selected_sort_types);
$orderby = $_sort_types[0];
$query->set('orderby', $orderby);
}
instead of this -
if(is_main_query() && !empty($selected_sort_types) && empty($orderby)) {
$_sort_types = array_keys($selected_sort_types);
$orderby = $_sort_types[0];
$query->set('orderby', $orderby);
}

cycle next page wordpress order by post title

I'm using Cycle jquery for my website.
I've added a php code do get a link at the end of my slideshow to go to the next page.
I want my pages to be sorted alphabetically but it's not working...
I've added "orderby=post_title" but still not working...
anybody can help me with this ?
here is my PHP code :
function dbdb_next_page_link() {
global $post;
if ( isset($post->post_parent) && $post->post_parent > 0 ) {
$children = get_pages('&orderby=menu_order&order=ASC&child_of='.$post->post_parent.'&parent='.$post->post_parent);
}
//print_r($children);
// throw the children ids into an array
foreach( $children as $child ) { $child_id_array[] = $child->ID; }
$next_page_id = relative_value_array($child_id_array, $post->ID, 1);
$output = '';
if( '' != $next_page_id ) {
$output .= ''. get_the_title($next_page_id) . ' »';
}
return get_page_link($next_page_id);
}
and a jsfiddle link : http://jsfiddle.net/KvBRV/
....
I have the same problem with a "select type" menu, my pages are not sorted alphabetically, I don't know why, here is my php code :
<div class="styled-select">
<?php
if(!$post->post_parent){
$children = get_pages(array(
'child_of' => $post->ID,
'post_type' => 'page',
'post_status' => 'publish',
'sort_order' => 'ASC',
'sort_column' => 'post_title',
));
}else{
$children = get_pages(array(
'child_of' => $post->post_parent,
'post_type' => 'page',
'post_status' => 'publish',
'sort_order' => 'ASC',
'sort_column' => 'post_title',
));
}
if ($children) {
echo '<select name="" onchange="location = this.options[this.selectedIndex].value;">';
echo '<option>'. 'A - Z' .'</option>';
function getInitials($name){
//split name using spaces
$words=explode(" ",$name);
$inits='';
//loop through array extracting initial letters
foreach($words as $word){
$inits = strtoupper(substr($word,0,1));
break;
}
return $inits;
}
$currval = "";
foreach($children as $child){
//print_r($child);
$permalink = get_permalink($child->ID);
$post_title = strtoupper($child->post_title);
$initial = getInitials($child->post_title);
if($initial!='' && $currval != $initial ) {
$currval = $initial;
echo '<optgroup label="'.$initial.'""></optgroup>';
}
echo '<option value="'.$permalink.'">'.$post_title.'</option>';
}
echo '</select>';
} ?>
<!-- FIN MENU DEROULANT A-Z -->
</div>
and a jsfiddle link : http://jsfiddle.net/Zp3Lt/
thanks a lot for your help !
Mattieu
get_pages doesnt accept any arg called orderby or order instead sort_column and sort_order is used ..
so your code would be
if ( isset($post->post_parent) && $post->post_parent > 0 ) {
$children = get_pages('sort_column=post_title&sort_order=ASC&child_of='.$post->post_parent.'&parent='.$post->post_parent);
}
Refer Here
Get Pages

Categories