List categories order by Random - php

I'm not good with PHP.
I use "Advanced Categories Widget" to list categories on Sidebar.
I used this plugin because it offers the ability to display images categories.
But I need to order categories by random.
I find this code on the plugin:
function advanced_categories_widget_html( $args = array() ) {
$args = wp_parse_args( $args );
$args['walker'] = new Walker_Advance_Category_Widget;
$output = wp_list_categories( $args );
if ( $output ) return $output;
}
and i Find another code on forum which displays correctly categories by random order:
wp_list_categories
how I can exploit the second code to hack the first code to list my categories with random order?
The PHP file for the plugin: http://codepad.org/a3yU7Xny

Accordion to the documentation on the Advanced Categories Widget plugin you're using -- -- you can specify 'orderby' in your plugin settings. See this screenshot.
You should have a random or rand option in the drop down.
Can't confirm cuz it's a paid plugin.

Just add the "hack" the function in the plugin file :
function advanced_categories_widget_html( $args = array() ) {
$args = wp_parse_args( $args );
$args['walker'] = new Walker_Advance_Category_Widget;
$cats ='';
$categories=get_categories();
$rand_keys = array_rand($categories, 5); // 5 is the number of categories you want
foreach ($rand_keys as $key) {
$cats .= $categories[$key]->term_id .',';
}
$output = wp_list_categories($args.'&include='.$cats);
if ( $output ) return $output;
}
Or by a cleaner way, add in your functions.php file :
function random_advanced_categories_widget_html( $args = array() ) {
$args = wp_parse_args( $args );
$args['walker'] = new Walker_Advance_Category_Widget;
$cats ='';
$categories=get_categories();
$rand_keys = array_rand($categories, 5); // 5 is the number of categories you want
foreach ($rand_keys as $key) {
$cats .= $categories[$key]->term_id .',';
}
$output = wp_list_categories($args.'&include='.$cats);
if ( $output ) return $output;
}

Related

Wordpress - Showing custom menu links while showing child pages

I have this loop that simply shows all child pages of the current page:
<?php
$args = array(
'parent' => $post->ID,
'post_type' => 'page',
'sort_order' => 'ASC'
);
$pages = get_pages($args); ?>
<?php foreach( $pages as $page ) { ?>
<div>
<p><?php echo $page->post_title; ?></p>
</div>
<?php } ?>
The Nav for this page looks like this:
Parent Page
- Child page
- Child page
- Child page
- Custom Link (added in appearance > menus)
- Custom link (added in appearance > menus)
- Page which has another parent (added in appearance > menus)
The code above correctly shows all of the direct child pages, but I would like it to show the custom links and other page I have added to the menu dropdown.
Ive tried playing with wp_get_nav_menu_items in place of get_pages and also using 'post_type' => 'page' but I can't seem to get this working correctly. I can either show a full list of all pages or just the direct child pages.
Can anyone tell me where I'm going wrong please? I seems like it should be a really easy thing to do...
Ok, I've found a way to get this to work using a custom walker class inside the functions.php file, like so:
class Selective_Walker extends Walker_Nav_Menu
{
function walk( $elements, $max_depth) {
$args = array_slice(func_get_args(), 2);
$output = '';
if ($max_depth < -1) //invalid parameter
return $output;
if (empty($elements)) //nothing to walk
return $output;
$id_field = $this->db_fields['id'];
$parent_field = $this->db_fields['parent'];
// flat display
if ( -1 == $max_depth ) {
$empty_array = array();
foreach ( $elements as $e )
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
return $output;
}
/*
* need to display in hierarchical order
* separate elements into two buckets: top level and children elements
* children_elements is two dimensional array, eg.
* children_elements[10][] contains all sub-elements whose parent is 10.
*/
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( 0 == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
/*
* when none of the elements is top level
* assume the first one must be root of the sub elements
*/
if ( empty($top_level_elements) ) {
$first = array_slice( $elements, 0, 1 );
$root = $first[0];
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( $root->$parent_field == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[ $e->$parent_field ][] = $e;
}
}
$current_element_markers = array( 'current-menu-item', 'current-menu-parent', 'current-menu-ancestor' ); //added by continent7
foreach ( $top_level_elements as $e ){ //changed by continent7
// descend only on current tree
$descend_test = array_intersect( $current_element_markers, $e->classes );
if ( !empty( $descend_test ) )
$this->display_element( $e, $children_elements, 2, 0, $args, $output );
}
/*
* if we are displaying all levels, and remaining children_elements is not empty,
* then we got orphans, which should be displayed regardless
*/
/* removed by continent7
if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
$empty_array = array();
foreach ( $children_elements as $orphans )
foreach( $orphans as $op )
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
}
*/
return $output;
}
}
Then using this to my template page to simply the menu:
<?php
$menuParameters = array(
'theme_location' =>'primary',
'walker'=>new Selective_Walker()
);
echo wp_nav_menu( $menuParameters );
?>
This helpfully shows the current page title as well as ALL of the sub nav items, not just the page items.

Wordpress shortcode php array to text in a text block

I am beginning to make a site in wordpress (so no php or html experience). I want to generate textlist from a mysql column into a text block.
I have made the php code for the shortcode below which returns an array which now displays in the textblock as "Array" instead of a list of strings.
If i just print the values they appear on the head of the page.
What are the next steps I need to do/look for. I can't find it because I probably do not know the correct search terms. My guess is something with HTML.
<?php
function location_marker_shortcode( $atts ) {
$a = shortcode_atts( array(
'mapnumber' => 'world'
), $atts );
global $wpdb;
//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');
foreach ( $marker_labels as $marker_label )
{
//print labels
echo $marker_label;
}
return $marker_labels;
}
//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>
I have now this code which gives me a list exactly what i want but not in the "paragraph block" in wordpress where my shortcode is situated.
<?php
function location_marker_shortcode( $atts ) {
$a = shortcode_atts( array(
'mapnumber' => 'world'
), $atts );
global $wpdb;
//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');
foreach ( $marker_labels as $marker_label )
{
echo '<li>'. $marker_label.'</li>';
}
}
//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>
I'm not 100% sure what you're trying to accomplish, but try this. You don't want to echo out all the values as you're looping through them. Concatenate everything in a variable and then return the entire string at the end of your shortcode. This should generate an unordered list.
<?php
function location_marker_shortcode( $atts ) {
$a = shortcode_atts( array(
'mapnumber' => 'world'
), $atts );
global $wpdb;
//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');
$output = '<ul>';
foreach ( $marker_labels as $marker_label )
{
$output .= '<li>' . $marker_label . '</li>';
}
$output .= '</ul>';
return $ouput;
}
//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>

Get array of items from phpAdmin Wordpress

In my phpAdmin I have a list of arrays that I need to be able to access in php Wordpress.
I need to get these two arrays and the variables associated with them but I can't find anywhere how to access this type of information.
Essentially I would like to loop through all these items and match one of their variables with the ID of specific posts - I have the post part.
wp_learnpress_sections
wp_learnpress_section_items
Basically the item_type is an lp_lesson which is a custom post type. I am able to grab all the posts from wp_posts so I figured I would be able to grab these other arrays?
Edit:
My theme function. This works for all the posts. However, I want to be able to find out which section_id a post belongs to.
add_action( 'wp_enqueue_scripts', 'win_9388244_format_lp_lesson' );
function win_9388244_format_lp_lesson() {
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson',
'numberposts' => 99999
);
$posts = get_posts($args);
foreach($posts as $post) {
update_post_meta( $post->ID, 'wpk_icon_text', 'Test' );
}
}
Full function that does it:
function win_9388244_format_lp_lesson() {
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson',
'numberposts' => 99999
);
$posts = get_posts($args);
global $wpdb;
$sections = $wpdb->get_results( "SELECT section_item_id, section_id, item_id FROM wp_learnpress_section_items", ARRAY_A );
$items = $wpdb->get_results( "SELECT section_course_id, section_id FROM wp_learnpress_sections", ARRAY_A );
foreach($posts as $post) {
$lesson_id = $post->ID;
foreach($sections as $section) {
if($section['item_id'] == $lesson_id) {
$currentSection = $section['section_id'];
foreach($items as $item) {
if ($item['section_id'] == $currentSection) {
$course = $item['section_course_id'];
//switch $course with predefined variables for courses
}
}
}
}
}
}

Count published custom posts in Wordpress

I have the following piece of code in a WordPress site which has custom posts.
This appears in a functions.php file.
It was a purchased template and I need to count the just the PUBLISHED custom posts and i added this in the code below :
'.'('.$option->count.')'.'
It is working just fine at the moment, but it's counting trash as well.
Please could somebody help me, thank you very much.
function dox_get_list_terms( $taxonomy = 'category', $term_id, $number, $orderby = 'name', $order = 'ASC', $hide = '0' ) {
$terms = array();
$terms = explode(',', $term_id);
$count = count( $terms );
$output = '';
foreach( $terms as $term ) {
if ($term >= 0) {
$options = get_terms( $taxonomy, 'number='.$number.'child_of='.$term.'&parent='.$term.'&hide_empty='.$hide.'&hierarchical=1&depth=1&orderby='.$orderby.'&order='.$order );
if (! is_wp_error($options) ) {
foreach ($options as $option) {
$output .= '<li>
<a href="'.get_term_link($option->slug, $taxonomy).'">
'.$option->name.'
</a>
'.'('.$option->count.')'.'
</li>';
}
}
}
}
return $output;
}
If you set hide_empty=1 in get_terms() function, Then you`ll only get terms who are assigned to any published post or custom posts.
Terms with 0 count will be ignored.

Can't get WordPress to display all categories in Multisite

I'm using WordPress Multisite and I'm trying to display all the categories in every site on one page. When I'm on my admin account, the following code works. However, when I switch to any other account, no categories are shown.
$t=get_current_blog_id();
foreach(function_that_gets_blogs() as $k=>$blog){
switch_to_blog($blog['blog_id']);
print_r(get_categories(array('hide_empty'=>true))); // prints "array()"
foreach(get_categories(array('hide_empty'=>true)) as $cat){
...
}
}
switch_to_blog($t);
Why aren't the categories showing?
Like b__ said you should check for:
Where are you using this code? (functions.php, plugin)
You should disable plugins 1 by 1, to check if some are interfering
and in the last case change theme
I've done something like you, and here is the code, in case you wanna try it:
// Current Site
$current = get_current_site();
// All Sites
$blogs = get_blog_list( 0, 'all' );
foreach ( $blogs as $blog ) {
// switch to the blog
switch_to_blog( $blog['blog_id'] );
// get_categories args
$args = array(
'hide_empty' => true
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
$link = get_category_link( $category->term_id );
$name = $category->name;
printf( '%s ', $link, $name, $name );
}
}
// return to the current site
switch_to_blog( $current->id );
Update 2014/06/01
the function get_blog_list(); is deprecated since version 3.0, with that you should change that function within wp_get_sites();
// Current Site
$current = get_current_site();
// All Sites
$blogs = wp_get_sites();
foreach ( $blogs as $blog ) {
// switch to the blog
switch_to_blog( $blog['blog_id'] );
// get_categories args
$args = array(
'hide_empty' => true
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
$link = get_category_link( $category->term_id );
$name = $category->name;
printf( '%s ', $link, $name, $name );
}
}
// return to the current site
switch_to_blog( $current->id );
Simple as that...
Did you try:
<?php wp_list_categories("title_li=");?>

Categories