Get Category name in Wordpress Walker Nav Menu - php

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;
}

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

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.

How to query the first 2 posts in this function but call them separately?

Can someone show me how to add 2 posts separately using a single query? For example when calling the code: { $content_block[2] .= (fphp_get_related_posts() ); show a post, then { $content_block[4] .= (fphp_get_related_posts() ); show the next post (offset by 1). How can I do this?
At the moment I know I have set 'max' => '1' I'm no php warrior, I was thinking you could 'max' => '2' and call each post like this: (fphp_get_related_posts(1) or (fphp_get_related_posts(2) but that is not right..
//Insert Related Posts Into Content Function
add_filter('the_content', 'mte_add_incontent_post');
function mte_add_incontent_post($content)
{ if(is_single()){
$content_block = explode('<p>',$content);
if(!empty($content_block[2]))
{ $content_block[2] .= (fphp_get_related_posts() );
}
if(!empty($content_block[4]))
{ $content_block[4] .= (fphp_get_related_posts() );
}
for($i=1;$i<count($content_block);$i++)
{ $content_block[$i] = '<p>'.$content_block[$i];
}
$content = implode('',$content_block);
}
return $content;
}
function fphp_get_related_posts($atts) {
$atts = shortcode_atts( array(
'max' => '1',
), $atts, 'relatedposts' );
$reset_post = $post;
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
$post_tag_ids = array();
foreach($post_tags as $post_tag) $post_tag_ids[] = $post_tag->term_id;
$args=array(
'tag__in' => $post_tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page' => $atts['max'],
'orderby' => 'DESC'
);
$related_query = new wp_query( $args );
if (intval($related_query->post_count) > 0) {
$html = '<div class="related_post"><b>SEE ALSO:</b>';
while( $related_query->have_posts() ) {
$related_query->the_post();
$html .= '<a rel="external" href="'. get_the_permalink(). '">';
$html .= get_the_title() . '</a>';
}
}
$post = $reset_post;
wp_reset_query();
$html .= '</div>';
return $html;
}
}
Would appreciate your help so I can understand how to do this.

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

Not able to have more than 1 post per column

Hi I am working within wordpress and have so far been able to display different sizes for my post loop but I keep getting only 1 post in each column. I have tried adding the multiple post loop into our php and have also set the css for the size but still only 1 post per column.
/* returns carousel html and js embed code */
function get_carousel($id, $get_first_post)
{
$id = intval($id);
if ($id <= 0)
die ('tc-oops7');
global $wpdb;
$carousels_table = $wpdb->prefix . 'touchcarousels';
$slider_row = $wpdb->get_row("SELECT * FROM $carousels_table WHERE id = $id", ARRAY_A);
if(!$slider_row) {
return "<p>Oops, TouchCarousel with ID $id not found.</p>";
}
$carousel_html = '';
$skin_name = $slider_row['skin'];
$css_classes = $slider_row['css_classes'];
$carousel_html .= "<div id=\"touchcarousel-$id\" class=\"touchcarousel $skin_name $css_classes\" style=\"width:{$slider_row['width']}; height:{$slider_row['height']}; \">\n";
$carousel_html .= "\t<ul class=\"touchcarousel-container\">\n";
global $post;
$args = array(
'numberposts' => intval($slider_row['max_posts']),
'posts_per_page' => intval($slider_row['max_posts']),
'offset' => 0,
'cat' => $slider_row['post_categories'],
'orderby' => $slider_row['post_orderby'],
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => $slider_row['post_type'],
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish' );
$post_taxonomies_arr = (array)json_decode (stripslashes($slider_row['post_categories']));
$taxonomies_query_arr = array();
$taxonomies_query_arr['relation'] = $slider_row['post_relation'];
$count = 0;
foreach ($post_taxonomies_arr as $key => $taxonomy ) {
$taxonomies_query_arr[$count]['taxonomy'] = $key;
$taxonomies_query_arr[$count]['terms'] = $taxonomy;
$taxonomies_query_arr[$count]['field'] = 'slug';
$count++;
}
$args['tax_query'] = $taxonomies_query_arr;
$the_query = new WP_Query( $args );
if($get_first_post) {
$the_query->have_posts();
$the_query->the_post();
$this->current_loop_post = $post;
return $post;
}
if($the_query->have_posts()) {
$layout_code = stripslashes($slider_row['layout_code']);
while ($the_query->have_posts()) : $the_query->the_post();$this->current_loop_post = $post;
$do_not_duplicate[] = $post->ID;
$carousel_html .= "\t\t<li class=\"touchcarousel-item\">\n";
$carousel_html .= preg_replace_callback ("/\[tco.*?\](.*?)\[\/tco\]/", array($this, 'format_variables'), $layout_code);
$carousel_html .= "\t\t</li>\n";
endwhile;
$carousel_html .= "\t</ul>\n";
$carousel_html .= "</div>";
$slider_settings = stripslashes($slider_row['js_settings']);
$carousel_js = "";
$carousel_js .= "<script type=\"text/javascript\">\n";
$carousel_js .= "jQuery(document).ready(function($) {";
$carousel_js .= "$(\"#touchcarousel-$id\").touchCarousel(";
$carousel_js .= $slider_settings;
$carousel_js .= ");";
$carousel_js .= "});\n";
$carousel_js .= "</script>";
$carousel_html .= $carousel_js;
} else {
$carousel_html = "<p class=\"tc-posts-not-found\">". __('TouchCarousel Warning: No posts found with selected settings.', 'touchcarousel') ."</p>";
}
wp_reset_postdata();
wp_reset_query();
return stripslashes($carousel_html);
}
The example location can be viewed at http://demo.campaignready.com
So far I have tried calling the posts and divs by their ids and classes but it is still only showing 1 post per column after running the "the_query"

Categories