cycle next page wordpress order by post title - php

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

Related

Get Category name in Wordpress Walker Nav Menu

I Write a Custom Nav Walker in My Wordpress Custom Child Theme.
I Need To Detect Current Category in my end_lvl Function. Is there Any Solution ?
function end_lvl(&$output, $depth=0, $args=null) {
$indent = str_repeat( "\t", $depth );
$divder_div_start = "<div class=\"megamenu-divder\">";
$divder_div_end = "</div>";
// Get 4 most recent product IDs in date descending order.
$query = new WC_Product_Query( array(
'limit' => 4,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
'category' => array( 'md-o-poshak' ),
) );
$topseller_img_tags = '';
$products = $query->get_products();
foreach($products as $p_id){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $p_id ));
$topseller_img_tags .= "\n<div><img src='$image[0]' width='150' height='150' /></div>";
}
$topseller_div_start = "<div class=\"top-seller-megamenu\">";
$topseller_div_end = "</div>";
if($depth > 0)
{
$output .= "$indent</ul>\n$indent\n$divder_div_start\n$divder_div_end\n$topseller_div_start\n$topseller_img_tags\n$topseller_div_end\n</section>\n";
}
else
{
$output .= "</ul>";
}
}
i need category name.
try to add this:
$cat_name = '';
if (is_category()) {
$category = get_queried_object();
$cat_name = $category->name;
}

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');

how to show all testimonial on single page via shortcode in wordpress?

i have created shortcode to show all testimonial on one page.
i have created custom post type to add each testimonial.
can anybody tell me how to show all testimonial on single page .
this code is used to create sshortcode
below i m mentioning code :-
function fn_testimonials_block()
{
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
$pages = $the_query->posts;
//echo "<pre>";
//print_r($pages);
//echo "</pre>";
//exit;
$output = '';
$count = 1;
foreach($pages as $page) {
//-----------------
$author = $page->post_title;
$testimonial = $page->post_content;
$page_url = get_page_link( $page->ID );
$author_image = get_the_post_thumbnail_url( $page->ID, 'thumbnail' );
if ($count%2 == 1)
{
$output .= '<div>';
}
$output .= '<div>
<p>'.$testimonial.'<br>
<img src="'.$author_image .'"> <span>'.$author.', </span></p>
</div>';
if ($count%2 == 0)
{
$output .= '</div>';
}
$count++;
}
if ($count%4 != 1){
$output .= '</div>';
}
return $output;
}
I think you will have to pass 'posts_per_page' key in $args array. So your $args array will look like this
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC',
'posts_per_page' => '-1'
);
I am not sure about the $count variable you are using. I suppose you are trying to implement logic for pagination there but since your question is about showing all testimonials on single page, I won't get into that part.
Making changes in $args will return all testimonials objects.

Adapting PHP code to split up a list into approximately equal comuns

I have existing PHP code to list product categories as a list of links:
<?php // as per https://phptechnologytutorials.wordpress.com/2015/11/07/get-
list-of-all-woocommerce-categories/
$orderby = 'name';
$order = 'asc';
$hide_empty = false;
$excluded = '32';
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'exclude' => $excluded,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '<ul class="linked-product-category-list">';
foreach ($product_categories as $key => $category) {
echo '<li>';
echo '<a href="'.get_term_link($category).'" >';
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
?>
I found another Stack Overflow solution which demonstrates how to do what I want to achieve - to arrange list of items into 4 equally populated columns.
However, I'm new to PHP and I don't know how to adapt the solution into my own code above. Please help me to adapt my code above using the principles of the solution below to split up my list into columns.
<?php
$col = 3;
$projects = array_chunk($projects, ceil(count($projects) / $col));
foreach ($projects as $i => $project_chunk)
{
echo "<ul class='pcol{$i+1}'>";
foreach ($project_chunk as $project)
{
echo "<li>{$project->name}</li>";
};
echo "</ul>";
}; ?>

Wordpress Wp_query object with 2 posts prints only one

I need to display the latest post from 3 categories and two posts from this last one with another HTML formatting. The problem is the last category prints only one post and stops with a var_dump() on the object I can see the two posts.
Check the functions here:
function destaques( $atts ) {
extract( shortcode_atts( array(
'id' => 0,
), $atts ) );
$id = array(6,16,10,4);
$posts = array();
$nomesCat = array();
foreach ($id as $key => $value) {
if ($value == 4){
//this is the categorie with two posts
$posts[] = new WP_Query( array('posts_per_page' => 2, 'category__in' => array($value)));
} else {
$posts[] = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($value)));
}
$nomesCat[] = get_category($value);
}
$html = '<ul class="destaques">';
foreach ($posts as $key => $value) {
if ($value->have_posts()){
while($value->have_posts()){
$value->the_post();
if ($nomesCat[$key]->cat_name == 'Colunistas') {
// check for the categorie name, then call another
// function, passing the wp_query object
$html .= auxiliarColunistas($value);
break;
} else {
//lots of html formatting code
$html .= '</li>';
}
}
}
}
$html .= '</ul>';
return $html;
This is the helper function:
function auxiliarColunistas ($posts) {
$html = '<li class="last">';
/*var_dump($posts); this returns two posts!
die;*/
$html .= '<h2>Colunistas</h2>';
if ($posts->have_posts()){
while ($posts->have_posts()) {
$posts->the_post();
//more html formatting code
}
}
$html .= '</li>';
return $html; }
Why does the loop print just one post and stops?
Try doing it like this
foreach ($id as $key => $value) {
if ($value == 4){
//this is the categorie with two posts
$posts_query = new WP_Query( array('posts_per_page' => 2, 'category__in' => array($value)));
} else {
$posts_query = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($value)));
}
$nomesCat[] = get_category($value);
}
$html = '<ul class="destaques">';
while($posts_query->have_posts()){
if ($nomesCat[$key]->cat_name == 'Colunistas') {
// check for the categorie name, then call another
// function, passing the wp_query object
$html .= auxiliarColunistas($posts_query->the_post());
break;
} else {
//lots of html formatting code
$html .= '</li>';
}
I could solve it changing this line:
if ($value == 4){
//this is the categorie with two posts
$posts_query = new WP_Query( array('posts_per_page' => 2, 'category__in' => array($value)));
to
if ($value == 4){
//this is the categorie with two posts
$posts_query = new WP_Query( array('posts_per_page' => 3, 'category__in' => array($value)));
but I still don't understand why the while loop is not getting the last post.

Categories