Used rewrite rules to achieve the current url setup but I need these combinations also added to the sitemap. 'collection' is just a product attribute with the taxonomy of pa_collection
Current URL Setup
https://*****.com/collection/%COLLECTION-TERM%/product-tag/%PRODUCT-TAG-TERM%
<?php
/**
* Create a new custom yoast seo sitemap
*/
add_filter( 'wpseo_sitemap_index', 'ex_add_sitemap_custom_items' );
add_action( 'init', 'init_wpseo_do_sitemap_actions' );
// Add custom index
function ex_add_sitemap_custom_items(){
global $wpseo_sitemaps;
$date = $wpseo_sitemaps->get_last_modified('CUSTOM_POST_TYPE');
$smp ='';
$smp .= '<sitemap>' . "\n";
$smp .= '<loc>' . site_url() .'/CUSTOM_KEY-sitemap.xml</loc>' . "\n";
$smp .= '<lastmod>' . htmlspecialchars( $date ) . '</lastmod>' . "\n";
$smp .= '</sitemap>' . "\n";
return $smp;
}
function init_wpseo_do_sitemap_actions(){
add_action( "wpseo_do_sitemap_CUSTOM_KEY", 'ex_generate_origin_combo_sitemap');
}
function ex_generate_origin_combo_sitemap(){
global $wpdb;
global $wp_query;
global $wpseo_sitemaps;
$post_type = 'archive';
wp_reset_query();
$args = array(
'posts_per_page' => -1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => $post_type,
'post_status' => 'publish',
'suppress_filters' => true
);
query_posts( $args );
wp_reset_postdata();
//echo '<pre>';print_r($url);echo '</pre>';
$posts_array = get_posts( $args );
$output = '';
if( !empty( $posts_array ) ){
$chf = 'weekly';
$pri = 1.0;
foreach ( $posts_array as $p ) {
$p->post_type = $post_type;
$p->post_status = 'publish';
$p->filter = 'sample';
$url = array();
if ( isset( $p->post_modified_gmt ) && $p->post_modified_gmt != '0000-00-00 00:00:00' && $p->post_modified_gmt > $p->post_date_gmt ) {
$url['mod'] = $p->post_modified_gmt;
} else {
if ( '0000-00-00 00:00:00' != $p->post_date_gmt ) {
$url['mod'] = $p->post_date_gmt;
} else {
$url['mod'] = $p->post_date;
}
}
$url['loc'] = site_url().'/sample/all/'.$p->post_name;
$url['chf'] = $chf;
$url['pri'] = $pri;
$output .= $wpseo_sitemaps->sitemap_url( $url );
// Clear the post_meta and the term cache for the post, as we no longer need it now.
// wp_cache_delete( $p->ID, 'post_meta' );
// clean_object_term_cache( $p->ID, $post_type );
}
}
// Grab last modified date
$sql = $wpdb->prepare(" SELECT MAX(p.post_modified_gmt) AS lastmod
FROM $wpdb->posts AS p
WHERE post_status IN ('publish') AND post_type = %s ", $post_type );
$mod = $wpdb->get_var( $sql );
// Generate terms URLs
$practitioner_terms = get_terms( 'pa_collection', 'orderby=count&hide_empty=0' );
$practitioner_termss = get_terms( 'product_tag', 'orderby=count&hide_empty=0' );
if( !empty( $practitioner_terms ) ){
$pri = 1;
$chf = 'weekly';
foreach ($practitioner_terms as $key => $term ){
$url = array();
$url['loc'] = site_url().'/collection/'.$term->slug.'/product-tag/';
$url['pri'] = $pri;
$url['mod'] = $mod;
$url['chf'] = $chf;
$output .= $wpseo_sitemaps->sitemap_url( $url );
}
foreach ($practitioner_termss as $key => $term ){
$url = array();
$url['loc'] = site_url().'/product-tag/'.$term->slug;
$url['pri'] = $pri;
$url['mod'] = $mod;
$url['chf'] = $chf;
$output .= $wpseo_sitemaps->sitemap_url( $url );
}
}
This is the section is where I need to combine the taxonomies. So far I have all the terms for both taxonomies on the same page but I need to have them combined to create all the possible combinations.
$practitioner_terms = get_terms( 'pa_collection', 'orderby=count&hide_empty=0' );
$practitioner_termss = get_terms( 'product_tag', 'orderby=count&hide_empty=0' );
if( !empty( $practitioner_terms ) ){
$pri = 1;
$chf = 'weekly';
foreach ($practitioner_terms as $key => $term ){
$url = array();
$url['loc'] =site_url().'/collection/'.$term>slug.'/product-tag/';
$url['pri'] = $pri;
$url['mod'] = $mod;
$url['chf'] = $chf;
$output .= $wpseo_sitemaps->sitemap_url( $url );
}
foreach ($practitioner_termss as $key => $term ){
$url = array();
$url['loc'] = site_url().'/product-tag/'.$term->slug;
$url['pri'] = $pri;
$url['mod'] = $mod;
$url['chf'] = $chf;
$output .= $wpseo_sitemaps->sitemap_url( $url );
}
}
Figured out a solution that works, could possibly be useful to others...
$practitioner_terms = get_terms( 'pa_collection', 'orderby=count&hide_empty=0' );
$practitioner_termss = get_terms( 'product_tag', 'orderby=count&hide_empty=0' );
if( !empty( $practitioner_terms ) ){
$pri = 1;
$chf = 'weekly';
foreach ($practitioner_terms as $key => $term ){
foreach ($practitioner_termss as $key => $terms ){
$url = array();
$url['loc'] = site_url().'/collection/'.$term->slug.'/product-tag/'.$terms->slug;
$url['pri'] = $pri;
$url['mod'] = $mod;
$url['chf'] = $chf;
$output .= $wpseo_sitemaps->sitemap_url( $url );
}
}
}
Related
With a form that is displayed via a shortcode, we want to add parameters to the URL and then read it out again in another shortcode. These functionalities are added via a custom plugin.
This works fine on a standard WordPress installation.
However, if we add the plugin to a multisite installation, it does not work. The parameters are added somehow, but an 'site not found' error occurs. Feel free to have a look at http://fortbildungen.mgo-fachverlage.de/, our multisite installation.
Here are parts of our code:
Shortcode that sets the parameters
This is the filter in the sidebar
function add_shortcode_themen() {
$themen = get_terms( array(
'taxonomy' => 'thema', //Custom Taxonomie 'thema'
'hide_empty' => true,
'parent' => 0,
) );
$arten = get_terms( array(
'taxonomy' => 'art', //Custom Taxonomie 'art'
'hide_empty' => true,
'parent' => 0,
) );
$s = '';
$s .= '<div class="fk-sc">';
$s .= '<form action="" method="GET">';
//### Themen
if( $themen ){
$s .= '<h3>Themenauswahl</h3>';
$themen_gefiltert = Fbk_WPQuery::get_ids('thema');
foreach($themen as $wp_term_object){
$id = $wp_term_object->term_id;
$color = get_term_meta( $id, 'fk-farbe', true );
$slug = $wp_term_object->slug;
$name = $wp_term_object->name;
$s .= '<div style="background-color: ' . $color . '">';
$s .= '<input
type="checkbox"
name="filter_thema[]"
id="fk-sc-' . $slug .'"
value="' . $id . '"';
if(!empty($themen_gefiltert) && in_array($id, $themen_gefiltert)){
$s .= ' checked';
}
$s .= '>';
$s .= '<label for="fk-sc-' . $slug .'">' . $name .'</label>';
$s .= '</div>';
}
echo '</select>';
}
//### Arten
if( $arten ){
$s .= '<br />';
$s .= '<h3>Veranstaltungsart</h3>';
$art_gefiltert = Fbk_WPQuery::get_ids('art');
foreach($arten as $wp_term_object){
$id = $wp_term_object->term_id;
$slug = $wp_term_object->slug;
$name = $wp_term_object->name;
$s .= '<div><input
type="checkbox"
name="filter_art[]"
id="fk-sc-' . $slug .'"
value="' . $id . '"';
if(!empty($art_gefiltert) && in_array($id, $art_gefiltert)){
$s .= ' checked';
}
$s .= '>';
$s .= '<label for="fk-sc-' . $slug .'">' . $name .'</label></div>';
}
}
$s .= '<input type="submit" value="Filtern"/>';
$s .= '</form>';
$s .= '</div>';
return $s;
}
add_shortcode( 'fk-filter', 'add_shortcode_themen' );
Shortcode that gets the parameters
This is the content of the page
add_shortcode( 'fk-kalender', 'add_shortcode_kalender' );
function add_shortcode_kalender() {
$query = new WP_Query(array(
'post_type' => 'fortbildung', //Custom Post Type 'fortbildung'
'post_status' => 'publish',
'tax_query' => Fbk_WPQuery::get_tax_query(), //gets the tax-query from URL-PARAMETERS
'orderby' => 'mgofv_fk_date',
'meta_key' => 'mgofv_fk_date',
'order' => 'ASC',
));
$html .= Fbk_SC_HTML_Builder::get_html_fortbildungen($query); //gets the html for displaying the posts
wp_reset_query();
return $html;
}
And the class Fbk_WPQuery
class Fbk_WPQuery {
public function get_tax_query(){
$tax_query = array();
if(isset($_GET['filter_thema']) && isset($_GET['filter_art'])){
$tax_query['relation'] = 'AND';
}
if(isset($_GET['filter_thema'])){
$tax_query[] = Fbk_WPQuery::get_thema();
}
if(isset($_GET['filter_art'])){
$tax_query[] = Fbk_WPQuery::get_art();
}
return $tax_query;
}
public function get_thema(){
if(isset($_GET['filter_thema'])){
var_dump($_GET['filter_thema']);
$selected = $_GET['filter_thema'];
$count = count($selected);
$arr = array();
$arr['relation'] = 'OR';
for($i = 0; $i < $count; $i++){
$arr[] = array(
'taxonomy' => 'thema',
'terms' => $selected[$i],
);
}
}
return $arr;
}
public function get_art(){
if(isset($_GET['filter_art'])){
$selected = $_GET['filter_art'];
$count = count($selected);
$arr = array();
$arr['relation'] = 'OR';
for($i = 0; $i < $count; $i++){
$arr[] = array(
'taxonomy' => 'art',
'terms' => $selected[$i],
'operator' => 'AND'
);
}
}
return $arr;
}
public function get_ids($get_param){
if($get_param == "thema"){
if(isset($_GET['filter_thema'])){
$selected = $_GET['filter_thema'];
$count = count($selected);
$id_arr = array();
for($i = 0; $i < $count; $i++){
$id_arr[] = $selected[$i];
}
}
}
else if($get_param == "art"){
if(isset($_GET['filter_art'])){
$selected = $_GET['filter_art'];
$count = count($selected);
$id_arr = array();
for($i = 0; $i < $count; $i++){
$id_arr[] = $selected[$i];
}
}
}
return $id_arr;
}
}
Thanks for any help :)
try to change
action="" to
action="http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '".
Worked for me
I'm new to PHP, still learning, so please excuse and kindly tell me what would be the appropriate fix. I used the breadcrumb script from here answered by Pieter Goosen. I want the breadcrumbs to work with google and schema, the part that isn't working is the <meta itemprop="position" content="number" /> tag. The content="number" needs to be set for each crumb, in order (1,2,3...). On line 8 I added $i = 1;, then changed $link_after = '<meta itemprop="position" content="' . $i++ . '" /></li>';. But $i++ is not printing a new sequential number, it is just repeating itself as if there was no other $i++. My guess is to apply the rule globally and to search for $i++ used, as it's probably being applied closed within the rule, but wouldn't know how to do this.
Code:
function get_hansel_and_gretel_breadcrumbs()
{
// Set variables for later use
$here_text = __( 'You are currently here!' );
$home_link = home_url('/');
$home_text = __( 'Home' );
$link_before = '<span typeof="v:Breadcrumb">';
$i = 1;
$link_after = '<meta itemprop="position" content="' . $i++ . '" /></span>'
$link_attr = ' rel="v:url" property="v:title"';
$link = $link_before . '<a' . $link_attr . ' href="%1$s">%2$s</a>' . $link_after;
$delimiter = ' » '; // Delimiter between crumbs
$before = '<span class="current">'; // Tag before the current crumb
$after = '</span>'; // Tag after the current crumb
$page_addon = ''; // Adds the page number if the query is paged
$breadcrumb_trail = '';
$category_links = '';
/**
* Set our own $wp_the_query variable. Do not use the global variable version due to
* reliability
*/
$wp_the_query = $GLOBALS['wp_the_query'];
$queried_object = $wp_the_query->get_queried_object();
// Handle single post requests which includes single pages, posts and attatchments
if ( is_singular() )
{
/**
* Set our own $post variable. Do not use the global variable version due to
* reliability. We will set $post_object variable to $GLOBALS['wp_the_query']
*/
$post_object = sanitize_post( $queried_object );
// Set variables
$title = apply_filters( 'the_title', $post_object->post_title );
$parent = $post_object->post_parent;
$post_type = $post_object->post_type;
$post_id = $post_object->ID;
$post_link = $before . $title . $after;
$parent_string = '';
$post_type_link = '';
if ( 'post' === $post_type )
{
// Get the post categories
$categories = get_the_category( $post_id );
if ( $categories ) {
// Lets grab the first category
$category = $categories[0];
$category_links = get_category_parents( $category, true, $delimiter );
$category_links = str_replace( '<a', $link_before . '<a' . $link_attr, $category_links );
$category_links = str_replace( '</a>', '</a>' . $link_after, $category_links );
}
}
if ( !in_array( $post_type, ['post', 'page', 'attachment'] ) )
{
$post_type_object = get_post_type_object( $post_type );
$archive_link = esc_url( get_post_type_archive_link( $post_type ) );
$post_type_link = sprintf( $link, $archive_link, $post_type_object->labels->singular_name );
}
// Get post parents if $parent !== 0
if ( 0 !== $parent )
{
$parent_links = [];
while ( $parent ) {
$post_parent = get_post( $parent );
$parent_links[] = sprintf( $link, esc_url( get_permalink( $post_parent->ID ) ), get_the_title( $post_parent->ID ) );
$parent = $post_parent->post_parent;
}
$parent_links = array_reverse( $parent_links );
$parent_string = implode( $delimiter, $parent_links );
}
// Lets build the breadcrumb trail
if ( $parent_string ) {
$breadcrumb_trail = $parent_string . $delimiter . $post_link;
} else {
$breadcrumb_trail = $post_link;
}
if ( $post_type_link )
$breadcrumb_trail = $post_type_link . $delimiter . $breadcrumb_trail;
if ( $category_links )
$breadcrumb_trail = $category_links . $breadcrumb_trail;
}
// Handle archives which includes category-, tag-, taxonomy-, date-, custom post type archives and author archives
if( is_archive() )
{
if ( is_category()
|| is_tag()
|| is_tax()
) {
// Set the variables for this section
$term_object = get_term( $queried_object );
$taxonomy = $term_object->taxonomy;
$term_id = $term_object->term_id;
$term_name = $term_object->name;
$term_parent = $term_object->parent;
$taxonomy_object = get_taxonomy( $taxonomy );
$current_term_link = $before->labels->singular_name . '<li class="curr-cat" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . $term_name . '</span><meta itemprop="position" content="' . $i++ . '" /></li>';
$parent_term_string = '';
if ( 0 !== $term_parent )
{
// Get all the current term ancestors
$parent_term_links = [];
while ( $term_parent ) {
$term = get_term( $term_parent, $taxonomy );
$parent_term_links[] = sprintf( $link, esc_url( get_term_link( $term ) ), $term->name );
$term_parent = $term->parent;
}
$parent_term_links = array_reverse( $parent_term_links );
$parent_term_string = implode( $delimiter, $parent_term_links );
}
if ( $parent_term_string ) {
$breadcrumb_trail = $parent_term_string . $delimiter . $current_term_link;
} else {
$breadcrumb_trail = $current_term_link;
}
} elseif ( is_author() ) {
$breadcrumb_trail = __( 'Author archive for ') . $before . $queried_object->data->display_name . $after;
} elseif ( is_date() ) {
// Set default variables
$year = $wp_the_query->query_vars['year'];
$monthnum = $wp_the_query->query_vars['monthnum'];
$day = $wp_the_query->query_vars['day'];
// Get the month name if $monthnum has a value
if ( $monthnum ) {
$date_time = DateTime::createFromFormat( '!m', $monthnum );
$month_name = $date_time->format( 'F' );
}
if ( is_year() ) {
$breadcrumb_trail = $before . $year . $after;
} elseif( is_month() ) {
$year_link = sprintf( $link, esc_url( get_year_link( $year ) ), $year );
$breadcrumb_trail = $year_link . $delimiter . $before . $month_name . $after;
} elseif( is_day() ) {
$year_link = sprintf( $link, esc_url( get_year_link( $year ) ), $year );
$month_link = sprintf( $link, esc_url( get_month_link( $year, $monthnum ) ), $month_name );
$breadcrumb_trail = $year_link . $delimiter . $month_link . $delimiter . $before . $day . $after;
}
} elseif ( is_post_type_archive() ) {
$post_type = $wp_the_query->query_vars['post_type'];
$post_type_object = get_post_type_object( $post_type );
$breadcrumb_trail = $before . $post_type_object->labels->singular_name . $after;
}
}
// Handle the search page
if ( is_search() ) {
$breadcrumb_trail = __( 'Search query for: ' ) . $before . get_search_query() . $after;
}
// Handle 404's
if ( is_404() ) {
$breadcrumb_trail = $before . __( 'Error 404' ) . $after;
}
// Handle paged pages
if ( is_paged() ) {
$current_page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
$page_addon = $before . sprintf( __( ' ( Page %s )' ), number_format_i18n( $current_page ) ) . $after;
}
$breadcrumb_output_link = '';
$breadcrumb_output_link .= '<div class="breadcrumb">';
if ( is_home()
|| is_front_page()
) {
// Do not show breadcrumbs on page one of home and frontpage
if ( is_paged() ) {
$breadcrumb_output_link .= $here_text . $delimiter;
$breadcrumb_output_link .= '' . $home_text . '';
$breadcrumb_output_link .= $page_addon;
}
} else {
$breadcrumb_output_link .= $here_text . $delimiter;
$breadcrumb_output_link .= '' . $home_text . '';
$breadcrumb_output_link .= $delimiter;
$breadcrumb_output_link .= $breadcrumb_trail;
$breadcrumb_output_link .= $page_addon;
}
$breadcrumb_output_link .= '</div><!-- .breadcrumbs -->';
return $breadcrumb_output_link;
}
I have used this code to add custom content to all my wordpress posts.
function add_after_post_content($content) {
if(!is_feed() && !is_home() && is_singular() && is_main_query()) {
$content .= '<strong>'. $title . '</strong> is a <strong>wallpaper</strong> posted in the ' . $cat_name . ' category.';
}
return $content;
}
add_filter('the_content', 'add_after_post_content');
The problem is that the post title and category are not showing so all I am getting is basically "is a wallpaper posted in the category".
How could I modify the code so that the post title and category are pulled to be added to the description?
Here is the code which works for some posts created using a specific plugin, but I would like it globalized to all posts on the site
//Create Post
$user_id = get_current_user_id();
$imagePoster->dirName = time();
$wp_upload_dir = wp_upload_dir();
if(!empty($_FILES["file"]["tmp_name"]))
{
$filename = $_FILES["file"]["tmp_name"];
$originalFilename = $_FILES["file"]["name"];
$zipMessage = $imagePoster->unzip($filename , $originalFilename);
$images = $imagePoster->iterateDir($wp_upload_dir['basedir'].'/bulkimages-'.$imagePoster->dirName.'/');
}
else
{
$filename = $_POST['manualfile'];
$images = $imagePoster->iterateDir($wp_upload_dir['basedir'].'/'.$filename.'/');
$zipMessage = '';
}
$postCount = 0;
$titleExploded = explode(",", $titleList);
$linkExploded = explode(",", $linkList);
$initialInterval = $statusSplit[1];
$interval = $statusSplit[1];
foreach($images as $image)
{
if(get_option('create-posts-from-images-useimagename') == true)
{
if(get_option('create-posts-from-images-delimiter') != '')
{
$path_parts = pathinfo($image->getFilename());
$title = str_replace(get_option('create-posts-from-images-delimiter')," ",$path_parts['filename'] );
}
else
{
$path_parts = pathinfo($image->getFilename());
$title = $path_parts['filename'];
}
}
else
{
$title = $imagePoster->loopTitles($titleExploded, $postCount );
}
$link = $imagePoster->loopTitles($linkExploded, $postCount );
$cat_name = get_cat_name( $category );
$content = '<strong>'. $title . '</strong> is a <strong>Wallpaper</strong> posted in the ' . $cat_name . ' category.<br /><br />';
It's been a while since I worked in wordpress but try var dumping out the following in the shortcode.
$GLOBALS['post']
You should be able to get what you want doing something like...
$GLOBALS['post']->post_name
to test this in your code above do the following.
function add_after_post_content($content) {
global $post;
if(!is_feed() && !is_home() && is_singular() && is_main_query()) {
$post_categories = wp_get_post_categories( $post->ID );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}
$content .= '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';
foreach($cats as $c){
$content .= $c['name'];
}
$content .= 'categories';
}
return $content;
}
add_filter('the_content', 'add_after_post_content');
I am using top 10 -popular post plugin.
I added the following lines but it wont display...
$output .= '<li>' .wpautop(wp_trim_words($result["post_content"], 15)). '</li>';
code:
function tptn_pop_posts( $args ) {
global $wpdb, $siteurl, $tableposts, $id, $tptn_settings;
$defaults = array(
'is_widget' => FALSE,
'daily' => FALSE,
'echo' => FALSE,
'strict_limit' => FALSE,
'posts_only' => FALSE,
'is_shortcode' => FALSE,
'heading' => 1,
);
$defaults = array_merge( $defaults, $tptn_settings );
// Parse incomming $args into an array and merge it with $defaults
$args = wp_parse_args( $args, $defaults );
// OPTIONAL: Declare each item in $args as its own variable i.e. $type, $before.
extract( $args, EXTR_SKIP );
if ($daily) {
$table_name = $wpdb->prefix . "top_ten_daily";
} else {
$table_name = $wpdb->prefix . "top_ten";
}
$limit = ( $strict_limit ) ? $limit : ( $limit * 5 );
$exclude_categories = explode( ',', $exclude_categories );
$target_attribute = ( $link_new_window ) ? ' target="_blank" ' : ' '; // Set Target attribute
$rel_attribute = ( $link_nofollow ) ? ' nofollow' : ''; // Set nofollow attribute
parse_str( $post_types, $post_types ); // Save post types in $post_types variable
if ( ! $daily ) {
$args = array();
$sql = "SELECT postnumber, cntaccess as sumCount, ID, post_type, post_status ";
$sql .= "FROM {$table_name} INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
$sql .= "AND post_status = 'publish' ";
if ( '' != $exclude_post_ids ) {
$sql .= "AND ID NOT IN ({$exclude_post_ids}) ";
}
$sql .= "AND ( ";
$multiple = false;
foreach ( $post_types as $post_type ) {
if ( $multiple ) $sql .= ' OR ';
$sql .= " post_type = '%s' ";
$multiple = true;
$args[] = $post_type; // Add the post types to the $args array
}
$sql .= " ) ";
$sql .= "ORDER BY sumCount DESC LIMIT %d";
$args[] = $limit;
} else {
$current_time = current_time( 'timestamp', 0 );
$current_time = $current_time - ( $daily_range - 1 ) * 3600 * 24;
$current_date = date( 'Y-m-j', $current_time );
$args = array(
$current_date,
);
$sql = "SELECT postnumber, SUM(cntaccess) as sumCount, dp_date, ID, post_type, post_status ";
$sql .= "FROM {$table_name} INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
$sql .= "AND post_status = 'publish' AND dp_date >= '%s' ";
if ( '' != $exclude_post_ids ) {
$sql .= "AND ID NOT IN ({$exclude_post_ids}) ";
}
$sql .= "AND ( ";
$multiple = false;
foreach ( $post_types as $post_type ) {
if ( $multiple ) $sql .= ' OR ';
$sql .= " post_type = '%s' ";
$multiple = true;
$args[] = $post_type; // Add the post types to the $args array
}
$sql .= " ) ";
$sql .= "GROUP BY postnumber ";
$sql .= "ORDER BY sumCount DESC LIMIT %d";
$args[] = $limit;
}
if ( $posts_only ) { // Return the array of posts only if the variable is set
return apply_filters( 'tptn_pop_posts_array', $wpdb->get_results( $wpdb->prepare( $sql , $args ) , ARRAY_A ) );
}
$results = $wpdb->get_results( $wpdb->prepare( $sql , $args ) );
$counter = 0;
$output = '';
$shortcode_class = $is_shortcode ? ' tptn_posts_shortcode' : '';
$widget_class = $is_widget ? ' tptn_posts_widget' : '';
if ( $heading ) {
if ( ! $daily ) {
$output .= '<div id="tptn_related" class="tptn_posts ' . $widget_class . $shortcode_class . '">' . apply_filters( 'tptn_heading_title', $title );
} else {
$output .= '<div id="tptn_related_daily" class="tptn_posts_daily' . $shortcode_class . '">' . apply_filters( 'tptn_heading_title', $title_daily );
}
} else {
if ( ! $daily ) {
$output .= '<div class="tptn_posts' . $widget_class . $shortcode_class . '">';
} else {
$output .= '<div class="tptn_posts_daily' . $widget_class . $shortcode_class . '">';
}
}
if ( $results ) {
$output .= apply_filters( 'tptn_before_list', $before_list );
foreach ( $results as $result ) {
$sumcount = $result->sumCount;
$result = get_post( apply_filters( 'tptn_post_id', $result->ID ) ); // Let's get the Post using the ID
$categorys = get_the_category( apply_filters( 'tptn_post_cat_id', $result->ID ) ); //Fetch categories of the plugin
$p_in_c = false; // Variable to check if post exists in a particular category
foreach ( $categorys as $cat ) { // Loop to check if post exists in excluded category
$p_in_c = ( in_array( $cat->cat_ID, $exclude_categories ) ) ? true : false;
if ( $p_in_c ) break; // End loop if post found in category
}
$title = tptn_max_formatted_content( get_the_title( $result->ID ), $title_length );
if ( ! $p_in_c ) {
$output .= apply_filters( 'tptn_before_list_item', $before_list_item, $result->ID );
if ( 'after' == $post_thumb_op ) {
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= '<span class="tptn_title">' . $title . '</span>'; // Add title if post thumbnail is to be displayed after
$output .= '</a>'; // Close the link
}
if ( 'inline' == $post_thumb_op || 'after' == $post_thumb_op || 'thumbs_only' == $post_thumb_op ) {
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= tptn_get_the_post_thumbnail( array(
'postid' => $result->ID,
'thumb_height' => $thumb_height,
'thumb_width' => $thumb_width,
'thumb_meta' => $thumb_meta,
'thumb_html' => $thumb_html,
'thumb_default' => $thumb_default,
'thumb_default_show' => $thumb_default_show,
'thumb_timthumb' => $thumb_timthumb,
'thumb_timthumb_q' => $thumb_timthumb_q,
'scan_images' => $scan_images,
'class' => "tptn_thumb",
'filter' => "tptn_postimage",
) );
$output .= '</a>'; // Close the link
}
if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
$output .= '<span class="tptn_after_thumb">';
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= '<span class="tptn_title">' . $title . '</span>'; // Add title when required by settings
// $output .= '<li>' .wpautop(wp_trim_words($result["post_content"], 15)). '</li>';
$output .= '</a>'; // Close the link
}
if ( $show_author ) {
$author_info = get_userdata( $result->post_author );
$author_name = ucwords( trim( stripslashes( $author_info->display_name ) ) );
$author_link = get_author_posts_url( $author_info->ID );
$output .= '<span class="tptn_author"> ' . __( ' by ', TPTN_LOCAL_NAME ).'' . $author_name . '</span> ';
}
if ( $show_date ) {
$output .= '<span class="tptn_date"> ' . mysql2date( get_option( 'date_format', 'd/m/y' ), $result->post_date ).'</span> ';
}
if ( $show_excerpt ) {
$output .= '<span class="tptn_excerpt"> ' . tptn_excerpt( $result->ID, $excerpt_length ).'</span>';
}
if ( $disp_list_count ) $output .= ' <span class="tptn_list_count">(' . number_format_i18n( $sumcount ) . ')</span>';
if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
$output .= '</span>';
}
$output .= apply_filters( 'tptn_after_list_item', $after_list_item, $result->ID );
$counter++;
}
if ( $counter == $limit/5 ) break; // End loop when related posts limit is reached
}
if ( $show_credit ) {
$output .= apply_filters( 'tptn_before_list_item', $before_list_item, $result->ID );
$output .= sprintf( 'Popular posts by Top 10 plugin', TPTN_LOCAL_NAME );
$output .= apply_filters( 'tptn_after_list_item', $after_list_item, $result->ID );
}
$output .= apply_filters( 'tptn_after_list', $after_list );
} else {
$output .= ( $blank_output ) ? '' : $blank_output_text;
}
$output .= '</div>';
return apply_filters( 'tptn_pop_posts', $output );
}
You seem to be returning $output. You will need to use echo in order for your HTML to display.
Ref: http://php.net/manual/en/function.echo.php
to show content in popular post by adding the following line below the code.
$output .= '<li><p>' .wp_trim_words(get_post($result->ID)->post_content,15). '</p></li>';
top10.php (top10 popular post plugin)
code:
if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
$output .= '<span class="tptn_after_thumb">';
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= '<span class="tptn_title">' . $title . '</span>'; // Add title when required by settings
$output .= '</a>'; // Close the link
$output .= '<li><p>' .wp_trim_words(get_post($result->ID)->post_content,15). '</p></li>';
}
I'm using a wordpress plugin to make a football pool. A really nice plugin but there is nog pagination in the ranking. So when you have over 1000 participants you will get a very long list.
The plugin author provided a class wich you can use to create pagination. My php knowledge is unfortunately to weak for this.
This is the pagination class:
<?php
class Football_Pool_Pagination {
public $show_total = true;
public $page_param = 'paged';
public $current_page = 1;
public $wrap = false;
private $total_pages = 0;
private $total_items = 0;
private $page_size = 20;
public function __construct( $num_items, $wrap = false ) {
$this->total_items = $num_items;
$this->total_pages = $this->calc_total_pages( $num_items, $this->page_size );
$this->current_page = $this->get_pagenum();
$this->wrap = $wrap;
}
public function get_page_size() {
return $this->page_size;
}
public function set_page_size( $size ) {
$this->page_size = $size;
$this->total_pages = $this->calc_total_pages( $this->total_items, $this->page_size );
$this->current_page = $this->get_pagenum();
}
public function show( $return = 'echo' ) {
$current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
if ( $this->total_pages ) {
$page_class = $this->total_pages < 2 ? ' one-page' : '';
} else {
$page_class = ' no-pages';
}
$output = '';
if ( $this->wrap ) $output .= sprintf( '<div class="tablenav top%s">', $page_class );
$output .= sprintf( '<div class="tablenav-pages%s">', $page_class );
if ( $this->show_total ) {
$output .= sprintf( '<span class="displaying-num">%s</span>'
, sprintf( _n( '1 item', '%s items', $this->total_items, FOOTBALLPOOL_TEXT_DOMAIN )
, $this->total_items
)
);
}
$disable_first = $disable_last = '';
if ( $this->current_page == 1 ) {
$disable_first = ' disabled';
}
if ( $this->current_page == $this->total_pages ) {
$disable_last = ' disabled';
}
$output .= '<span class="pagination-links">';
$output .= sprintf( '<a class="first-page%s" title="%s" href="%s">«</a>'
, $disable_first
, esc_attr__( 'Go to the first page' )
, esc_url( remove_query_arg( $this->page_param, $current_url ) )
);
$output .= sprintf( '<a class="prev-page%s" title="%s" href="%s">‹</a>'
, $disable_first
, esc_attr__( 'Go to the previous page' )
, esc_url( add_query_arg(
$this->page_param, max( 1, $this->current_page - 1 ),
$current_url ) )
);
$output .= sprintf( '<span class="paging-input"><input class="current-page" title="%s" type="text" name="%s" value="%d" size="%d"> of <span class="total-pages">%d</span></span>'
, esc_attr__( 'Current page' )
, $this->page_param
, $this->current_page
, strlen( $this->total_pages )
, $this->total_pages
);
$output .= sprintf( '<a class="next-page%s" title="%s" href="%s">›</a>'
, $disable_last
, esc_attr__( 'Go to the next page' )
, esc_url( add_query_arg(
$this->page_param, min( $this->total_pages, $this->current_page + 1 ),
$current_url ) )
);
$output .= sprintf( '<a class="last-page%s" title="%s" href="%s">»</a>'
, $disable_last
, esc_attr__( 'Go to the last page' )
, esc_url( add_query_arg( $this->page_param, $this->total_pages, $current_url ) )
);
$output .= '</span></div>';
if ( $this->wrap ) $output .= '</div>';
if ( $return == 'echo' ) {
echo $output;
} else {
return $output;
}
}
private function calc_total_pages( $num_items, $page_size ) {
return ceil( $num_items / $page_size );
}
private function get_pagenum() {
$page_num = Football_Pool_Utils::request_int( $this->page_param, 0 );
if( $page_num > $this->total_pages ) {
$page_num = $this->total_pages;
}
return max( 1, $page_num );
}
}
This is the class for ranking:
<?php
class Football_Pool_Ranking_Page {
public function page_content()
{
global $current_user;
get_currentuserinfo();
$output = '';
$pool = new Football_Pool_Pool;
// $userleague = get_the_author_meta( 'footballpool_league', $current_user->ID );
$userleague = $pool->get_league_for_user( $current_user->ID );
$userleague = ( isset( $userleague ) && is_integer( $userleague ) ) ? $userleague : FOOTBALLPOOL_LEAGUE_ALL;
$league = Football_Pool_Utils::post_string( 'league', $userleague );
$ranking_display = Football_Pool_Utils::get_fp_option( 'ranking_display', 0 );
if ( $ranking_display == 1 ) {
$ranking = Football_Pool_Utils::post_int( 'ranking', FOOTBALLPOOL_RANKING_DEFAULT );
} elseif ( $ranking_display == 2 ) {
$ranking = Football_Pool_Utils::get_fp_option( 'show_ranking', FOOTBALLPOOL_RANKING_DEFAULT );
} else {
$ranking = FOOTBALLPOOL_RANKING_DEFAULT;
}
$user_defined_rankings = $pool->get_rankings( 'user defined' );
if ( $pool->has_leagues || ( $ranking_display == 1 && count( $user_defined_rankings ) > 0 ) ) {
$output .= sprintf( '<form action="%s" method="post"><div style="margin-bottom: 1em;">'
, get_page_link()
);
if ( $pool->has_leagues ) {
$output .= sprintf( '%s: %s',
__( 'Choose league', FOOTBALLPOOL_TEXT_DOMAIN ),
$pool->league_filter( $league )
);
}
if ( $ranking_display == 1 && count( $user_defined_rankings ) > 0 ) {
$options = array();
$options[FOOTBALLPOOL_RANKING_DEFAULT] = '';
foreach( $user_defined_rankings as $user_defined_ranking ) {
$options[$user_defined_ranking['id']] = $user_defined_ranking['name'];
}
$output .= sprintf( '<br />%s: %s'
, __( 'Choose ranking', FOOTBALLPOOL_TEXT_DOMAIN )
, Football_Pool_Utils::select(
'ranking', $options, $ranking )
);
}
$output .= sprintf( '<input type="submit" name="_submit" value="%s" />'
, __( 'go', FOOTBALLPOOL_TEXT_DOMAIN )
);
$output .= '</div></form>';
}
$output .= $pool->print_pool_ranking( $league, $current_user->ID, $ranking );
return $output;
}
}
In another class I found a function that is printing the ranking:
public function print_pool_ranking( $league, $user, $ranking_id = FOOTBALLPOOL_RANKING_DEFAULT ) {
$output = '';
$rows = $this->get_pool_ranking( $league, $ranking_id );
$ranking = $users = array();
if ( count( $rows ) > 0 ) {
// there are results in the database, so get the ranking
foreach ( $rows as $row ) {
$ranking[] = $row;
$users[] = $row['user_id'];
}
} else {
// no results, show a list of users
$rows = $this->get_users( $league );
if ( count( $rows ) > 0 ) {
$output .= '<p>' . __( 'No results yet. Below is a list of all users.', FOOTBALLPOOL_TEXT_DOMAIN ) . '</p>';
foreach ( $rows as $row ) {
$ranking[] = $row;
$users[] = $row['user_id'];
}
} else {
$output .= '<p>'. __( 'No users have registered for this pool (yet).', FOOTBALLPOOL_TEXT_DOMAIN ) . '</p>';
}
}
if ( count( $ranking ) > 0 ) {
// get number of predictions per user if option is set
$show_num_predictions = ( Football_Pool_Utils::get_fp_option( 'show_num_predictions_in_ranking' ) == 1 );
if ( $show_num_predictions ) {
$predictions = $this->get_prediction_count_per_user( $users, $ranking_id );
}
$userpage = Football_Pool::get_page_link( 'user' );
$all_user_view = ( $league == FOOTBALLPOOL_LEAGUE_ALL && $this->has_leagues );
$i = 1;
$output .= '<table class="standen">';
if ( $show_num_predictions ) {
$output .= sprintf( '<tr>
<th>positie</th>
<th>%s</th>
<th>%s</th>
<th>%s</th>
%s</tr>'
, __( 'user', FOOTBALLPOOL_TEXT_DOMAIN )
, __( 'predictions', FOOTBALLPOOL_TEXT_DOMAIN )
, __( 'points', FOOTBALLPOOL_TEXT_DOMAIN )
, ( $all_user_view ? '<th></th>' : '' )
);
}
foreach ( $ranking as $row ) {
$class = ( $i % 2 != 0 ? 'even' : 'odd' );
if ( $all_user_view ) $class .= ' league-' . $row['league_id'];
if ( $row['user_id'] == $user ) $class .= ' currentuser';
if ( $show_num_predictions ) {
if ( array_key_exists( $row['user_id'], $predictions ) ) {
$num_predictions = $predictions[$row['user_id']];
} else {
$num_predictions = 0;
}
$num_predictions = sprintf( '<td>%d</td>', $num_predictions );
} else {
$num_predictions = '';
}
$output .= sprintf( '<tr class="%s"><td style="width:100px; text-align: center;">%d.</td>
<td>%s%s%s</td>
%s<td>%d</td>%s
</tr>',
$class,
$i++,
esc_url( add_query_arg( array( 'user' => $row['user_id'] ), $userpage ) ),
$this->get_avatar( $row['user_id'], 'medium' ),
$row['user_name'],
Football_Pool::user_name( $row['user_id'], 'label' ),
$num_predictions,
$row['points'],
( $all_user_view ? $this->league_image( $row['league_id'] ) : '' )
);
$output .= "\n";
}
$output .= '</table>';
}
return $output;
}
I know this is a lot of info and probably nobody can do something with this information but if somebody can help me that would be great.
I think you should work with this plugin:
it must save your time, and decrease complexity of your project.
Page Navigation
Thanks