Die Paginierungsfunktion im Shortcode wird immer oben angezeigt

Paginierungsfunktion im Shortcode wird immer oben angezeigt. Bitte schauen Sie unter folgenden Code

/*-------------------------------------------------------------------------*/ /* Custom Pagination */ /*-------------------------------------------------------------------------*/ function suareztheme_pagination($pages = '', $range = 2){ $showitems = ( $range * 2 ) + 1; global $paged; if(empty($paged)) $paged = 1; if($pages == ''){ global $wp_query; $pages = $wp_query->max_num_pages; if(!$pages){ $pages = 1; } } if( 1 != $pages ){ $pagination_html .= ''; return $pagination_html; } } 

Dann habe ich es in der Shortcode-function aufgerufen.

 /*-------------------------------------------------------------------------*/ /* Grid Shortcode */ /*-------------------------------------------------------------------------*/ function RecentBlog($atts, $content = null) { extract(shortcode_atts(array( "comments" => 'true', "date" => 'true', "columns" => '4', "limit" => '-1', "title" => 'true', "description" => 'true', "cat_slug" => '', "post_type" => '', "excerpt_length" => '15', "readmore_text" => '', "pagination" => 'false' ), $atts)); global $post; $postformat = get_post_format(); .... if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; } ...... if (have_posts()) : while (have_posts()) : the_post(); $postformat = get_post_format(); if( $postformat == "" ) $postformat="standard"; $protected = ""; $portfoliogrid .= '
'; .....//// $portfoliogrid .= '
'; ....... $portfoliogrid .='
'; // If either of title and description needs to be displayed. if ( $title == "true" || $description == "true" ) { $portfoliogrid .='
'; ...... $portfoliogrid .='
'; } $portfoliogrid .='
'; endwhile; endif; if ( $pagination == "true" ){ if ( isset( $additional_loop ) ){ echo suareztheme_pagination( $additional_loop->max_num_pages ); } else { echo suareztheme_pagination(); } if ( function_exists("suareztheme_pagination") ) { } else { next_posts_link('«« Older Posts'); previous_posts_link('Newer Posts »»'); } } wp_reset_query(); return $portfoliogrid; } add_shortcode( "recentblog", "RecentBlog" );

Der obige Code funktioniert gut, aber es hat ein Problem, wenn es auf einer bestimmten Seite angezeigt wird, wird es immer oben angezeigt, unabhängig davon, wo es sich befindet.

Solutions Collecting From Web of "Die Paginierungsfunktion im Shortcode wird immer oben angezeigt"

Ihre Ausgabe wird erwartet, aber bevor ich zur Lösung komme und warum, haben Sie hier ein paar Probleme

  • Sie verwenden entweder query_posts oder das Annullieren der Hauptabfrage global, was Sie niemals tun sollten. Das Verschenken ist Ihre Schleife ( if (have_posts()) : while (have_posts()) : the_post(); ) und die Verwendung von wp_reset_query(); . Sie sollten eine geeignete Instanz von WP_Query und die Paginierungsfunktion entsprechend anpassen. Ich habe kürzlich eine Paginierungsfunktion geschrieben, die dasselbe wie Ihre Paginierungsfunktion tut, aber mit benutzerdefinierten Abfragen, Hauptabfragen und statischen Titelseiten funktioniert. Schau es dir hier an

  • Ihr functionsname sollte (bei Verwendung von PSR- Codierungsstandards) camelCase . WordPress verwendet immer noch keine PSR-Codierungsstandards. WordPress-Codierungsstandards besagt, dass die functionsnamen alle Kleinbuchstaben und Wörter durch Unterstriche getrennt sein sollten.

  • Benutze niemals extract() . Diese function ist unzuverlässig und im Falle eines Fehlers extrem schwer zu debuggen. Aus verschiedenen Gründen wurde extract() einiger Zeit komplett aus dem Core entfernt. Sehen Sie dieses Ticket: trac Ticket # 22400

  • global $post; ist unnötig, ich kann die Verwendung in Ihrem Code nicht sehen. Auch $postformat = get_post_format(); gibt eine undefinierte variable Notiz zurück. Aktivieren Sie Debug, um die Debugerrors anzuzeigen

  • Sie geben Ihre Paginierungsfunktion in Ihrem Shortcode wieder. Shortcodes sollten niemals eine Ausgabe ausgeben, sie sollte immer zurückgegeben werden. Inhalt, der wiederholt wird, gibt eine unerwartete Ausgabe, so wie Sie sehen, wo Ihre Seitennummerierung oberhalb Ihrer Posts angezeigt wird und nicht wo Sie sie platziert haben

  • Verwenden Sie keine Syntax wie ; und endwhile oder endif . Obwohl valide PHP, ist es schwer zu debuggen und Code-Editoren unterstützt diese Syntax nicht. Verwenden Sie stattdessen die Old School Curlies {} die von Code-Editoren weitgehend unterstützt werden und sehr einfach zu debuggen sind

BEARBEITEN

Es gibt ein oder zwei zusätzliche Probleme, die ich hier sehe

  • next_posts_link() und previous_posts_link() echo ihre Ausgabe, die hier unpassend ist. Verwenden Sie die functionen get_ prefix und verketten Sie sie zu Ihrer Variablen, die später zurückgegeben wird

  • if ( isset( $additional_loop ) ){ Diese Überprüfung ist nicht notwendig, da $additional_loop Ihre Abfragevariable ist, die immer existiert

Hier ist dein Code. Ich habe ein paar Änderungen vorgenommen, andere habe ich gerade kommentiert. Bitte beachten Sie meine Kommentare im Code für weitere Details

 /* * Your function name is wrong, should be something like recent_blog */ function RecentBlog($atts, $content = null) { /* * This is the proper set up of attributes. * Use your attributes as follow: $attributes['comments'] */ $attributes = shortcode_atts(array( "comments" => 'true', "date" => 'true', "columns" => '4', "limit" => '-1', "title" => 'true', "description" => 'true', "cat_slug" => '', "post_type" => '', "excerpt_length" => '15', "readmore_text" => '', "pagination" => 'false' ), $atts); // I don't know about this, does not make sense global $post; // This is a bug, will produce a undefined variable notice. Turn debug on $postformat = get_post_format(); .... if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; } // Your query here is wrong. Use WP_Query. Something like below $args = array( 'posts_per_page' => $attributes['limit'], 'category_name' => $attributes['cat_slug'], 'paged' => $paged, //etc ); $additional_loop = new WP_Query( $args ); /* * Give each condition its own line. Also use curlies * Do not use `query_posts`. Set your loop to your query variable which seems * to be $additional_loop */ if ($additional_loop->have_posts()) { while ($additional_loop->have_posts()) { $additional_loop->the_post(); $postformat = get_post_format(); if( $postformat == "" ) $postformat="standard"; $protected = ""; $portfoliogrid .= '
'; .....//// $portfoliogrid .= '
'; ....... $portfoliogrid .='
'; /* * Change all your attributes accordingly like I did below * $title becomes $attributes['title'] * $description becomes $attributes['description'] */ // If either of title and description needs to be displayed. if ( $attributes['title'] == "true" || $attributes['description'] == "true" ) { $portfoliogrid .='
'; ...... $portfoliogrid .='
'; } $portfoliogrid .='
'; } } /* * Change attributes accordingly to new attribute syntax without extract() * $pagination becomes $attributes['pagination'] */ if ( $attributes['pagination'] == "true" ){ /* * Do you really need this condition. $additional_loop will always exists as this is your * set query variable */ if ( isset( $additional_loop ) ){ /* * Do not echo your output. Concatenate it to your variable to be returned later */ $portfoliogrid .= suareztheme_pagination( $additional_loop->max_num_pages ); } else { $portfoliogrid .= suareztheme_pagination(); } /* * This seems all wrong. Don't know what is your intention here */ if ( function_exists("suareztheme_pagination") ) { } else { /* * use the get_* prefix functions here and return them */ $portfoliogrid .= get_next_posts_link('«« Older Posts'); $portfoliogrid .= get_previous_posts_link('Newer Posts »»'); } } /* * This should be wp_reset_postdata(). wp_reset_query() is used with query_posts * which you should never use*/ wp_reset_query(); return $portfoliogrid; } /* * Change function call back name according to your new function name */ add_shortcode( "recentblog", "RecentBlog" );

BEARBEITEN 2

Hier ist ein komplettes Beispiel, wie Ihr Shortcode aussehen sollte. Bevor ich den Code einfügen, nur ein paar Notizen

  • Ich habe den Code nicht getestet. Sie sollten es nicht direkt in einer Live-Site einfügen, sondern zuerst in einer lokalen Installation testen

  • Ich habe alles, was ich getan habe, kommentiert, bevor Sie den Code testen, meinen Code und Ihren Code vergleichen und sicherstellen, dass Sie wissen, was ich getan habe. Sie können die Kommentare entfernen, wenn Sie dies wünschen, nachdem Sie dies getan haben

  • Ein sehr wichtiger Hinweis, comments_popup_link echos seine Ausgabe, die nicht in einem Shortcode funktioniert. In WordPress gibt es noch keine alternative function, die ihre Ausgabe zurückgibt. Eines der Site-Mitglieder, @Otto, hat eine benutzerdefinierte function get_comments_popup_link (die Sie auf seiner persönlichen Seite finden können ) geschrieben, um dieses Problem zu get_comments_popup_link . Sie müssen diese function in Ihre functions.php kopieren und einfügen, bevor Sie Ihren Shortcode hinzufügen. Hier ist die function von @Otto

     /** * Modifies WordPress's built-in comments_popup_link() function to return a string instead of echo comment results */ function get_comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) { global $wpcommentspopupfile, $wpcommentsjavascript; $id = get_the_ID(); if ( false === $zero ) $zero = __( 'No Comments' ); if ( false === $one ) $one = __( '1 Comment' ); if ( false === $more ) $more = __( '% Comments' ); if ( false === $none ) $none = __( 'Comments Off' ); $number = get_comments_number( $id ); $str = ''; if ( 0 == $number && !comments_open() && !pings_open() ) { $str = '' . $none . ''; return $str; } if ( post_password_required() ) { $str = __('Enter your password to view comments.'); return $str; } $str = ' 0 ) ); $str .= apply_filters( 'comments_popup_link_attributes', '' ); $str .= ' title="' . esc_attr( sprintf( __('Comment on %s'), $title ) ) . '">'; $str .= get_comments_number_str( $zero, $one, $more ); $str .= ''; return $str; } 

Endlich, hier ist Ihr neuer (hoffentlich funktionierender) Shortcode. Ich hoffe, das triggers einige Ihrer Hauptprobleme 🙂

 function recent_blog_shortcode($atts, $content = null) { /* * Do not wrap booleans and integers in quotes. If they are wrapped in quotes * they are interpreted as string values. 'true' !== true. First one is a string * while the second is a boolean * * Also, stick with single quotes (or double quotes), don't mix them, it is confusing * when it comes to debugging * * Do not use extract() as I have already explained */ $attributes = shortcode_atts(array( 'comments' => true, 'date' => true, 'vote' => true, 'columns' => 4, 'limit' => -1, 'title' => true, 'description' => true, 'cat_slug' => '', 'post_type' => '', 'excerpt_length' => 15, 'readmore_text' => '', 'pagination' => false ), $atts); /* * Stay out of the global scope. It is not very good practise. */ global $post; //$portfoliogrid, < -- Bad name for a global variable, too easy //$column_no; <-- Bad name for a global variable, too easy /* * As precaution, check if $attributes['columns'] is not empty. Also set the $columns variable from * our attributes */ $columns = $attributes['columns']; /* * Sets the variables to make sure your two variables is not undefined (bug) * if $columns are empty */ $column_no = ''; $portfolioImage_type = '', if ( $columns ) { if ( $columns == 4 ) { $column_no = 'col-md-3'; $portfolioImage_type = 'gridblock-medium'; } if ( $columns == 3 ) { $column_no = 'col-md-4'; $portfolioImage_type = 'gridblock-large'; } if ( $columns == 2 ) { $column_no = 'col-md-6'; $portfolioImage_type = 'gridblock-large'; } if ( $columns == 1 ) { $column_no = $columns; $portfolioImage_type = 'gridblock-full'; } } /* * Again, stay with single quotes OR double quotes, going with single quotes */ $postformats = ''; $terms = ''; $portfoliogrid = ''; /* * Great job here */ if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; } /* * Change $post_type to our new attribute system, $attributes['post_type'] * For ease, just set the variable $post_type to the attribute */ $post_type = $attributes['post_type']; if ( $post_type = '' ) { $type_explode = explode( ',', $post_type ); foreach ( $type_explode as $postformat ) { $count++; $postformat_slug = "post-format-" . $postformat; $terms[] .= $postformat_slug; } /* * Never ever use query_posts. It breaks page functionalities, pagination * and the main query. Very bad function to use, please avoid it. Use WP_Query * instead. * * Make your arguments conditional, not your query. Change our variables here to use the * new attribute system without extract() */ $args = array( 'category_name' => $attributes['cat_slug'], 'posts_per_page' => $attributes['limit'], 'paged' => $paged, 'tax_query' => array( array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => $terms ) ) ); } else { /* * Never ever use query_posts. It breaks page functionalities, pagination * and the main query. Very bad function to use, please avoid it. Use WP_Query * instead. * * Make your arguments conditional, not your query. Change our variables here to use the * new attribute system without extract() */ $args = array( 'category_name' => $attributes['cat_slug'], 'paged' => $paged, 'posts_per_page' => $attributes['limit'], ); } /* * Create our query with WP_Query, very good practice */ $q = new WP_Query( $args ); $suareztheme_pagestyle = get_post_meta( $post->ID, 'pagestyle', true ); /* * Each condition on a new line, and use curlies * Set our loop to our custom query. VERY IMPORTANT */ if ( $q->have_posts() ) { while ( $q->have_posts() ) { $q->the_post(); $postformat = get_post_format(); if( $postformat == '' ) $postformat = 'standard'; $protected = ''; $portfoliogrid .= '
'; //if Password Required /* * Where is MTHEME_PATH defined, this is a bug if MTHEME_PATH does not exists */ if ( post_password_required() ) { $protected = ' portfolio-protected'; $iconclass = ''; $portfoliogrid .= ''; $portfoliogrid .= ''; $portfoliogrid .= '
'; } else { // if ( ! has_post_thumbnail() ) { // $portfoliogrid .= '
'; // $portfoliogrid .= ''; // $portfoliogrid .= '
'; // } if ( has_post_thumbnail() ) { //Make sure it's not a slideshow //Switch check for Linked Type $portfoliogrid .= '
'; // If it aint slideshow then display a background. Otherwise one is active in slideshow thumbnails. // Custom Thumbnail // Display Image /* * suareztheme_display_post_image is a custom function. You should first check * if it exists, because if it does not, you will crash your site-content */ if ( function_exists( 'suareztheme_display_post_image' ) { $portfoliogrid .= suareztheme_display_post_image ( get_the_ID(), $have_image_url = '', $link = false, $type = $portfolioImage_type, $imagetitle = '', $class = 'preload-image displayed-image' ); } /* * else if should be elseif. else if !== elseif */ } elseif ( $postformat == 'gallery' ){ /* * the_post_format_gallery_content() is a custom function. You should first check * if it exists, because if it does not, you will crash your site-content */ if ( function_exists( 'the_post_format_gallery_content' ) $portfoliogrid .= the_post_format_gallery_content(); } else if ( $postformat == 'video' || $postformat == 'audio' ){ $embed_audio_code = get_post_meta($post->ID, 'audio_embed_code', true); $embed_video_code = get_post_meta($post->ID, 'video_embed_code', true); if ( $embed_audio_code ){ $portfoliogrid .= '
' . $embed_audio_code . '
'; } else { $portfoliogrid .= '
' . $embed_video_code . '
'; } } else if ( $postformat == "quote" ){ $portfoliogrid .= '
'; $portfoliogrid .= '

' . get_post_meta($post->ID, 'meta_quote', true) . '

'; if ( $quote_author != '' ){ $portfoliogrid .= '

- ' . get_post_meta($post->ID, 'meta_quote_author', true) . '

'; } $portfoliogrid .= '
'; } else { $portfoliogrid .= '
'; $portfoliogrid .= '
'; } } $portfoliogrid .= '
'; /* * Don't wrap booleans in quotes as described earlier * Also set our new attribute scheme */ if ( $attribute['date'] == true ){ $portfoliogrid .= '' . esc_html( get_the_date('F jS, Y') ) . ''; } // If either of title and description needs to be displayed. if ( $title == "true" ) { $portfoliogrid .='
'; $hreflink = esc_url( get_permalink() ); /* * Don't wrap booleans in quotes as described earlier * Also set our new attribute scheme */ if ( $attribute['title'] == true && $postformat == 'link' ) { $portfoliogrid .='

'. get_the_title() .'

'; } else { $portfoliogrid .='

'. get_the_title() .'

'; } $portfoliogrid .='
'; } /* * Don't wrap booleans in quotes as described earlier * Also set our new attribute scheme */ if ( $attribute['description'] == true ) { /* * Set our new attribute scheme * * suareztheme_excerpt_limit is a custom function. You should first check * if it exists, because if it does not, you will crash your site-content */ if ( function_exists( 'suareztheme_excerpt_limit' ) { $summary_content = esc_attr( suareztheme_excerpt_limit($attribute['excerpt_length']) ); $portfoliogrid .= '

'. $summary_content .'

'; } } /* * Don't wrap booleans in quotes as described earlier * Set our new attribute scheme */ if ( $attribute['readmore_text'] != '' || $attribute['comments'] == true || $attribute['vote'] == true ) { $portfoliogrid .= '
'; $portfoliogrid .= '
'; /* * dot_irecommendthis() is a custom function. You should first check * if it exists, because if it does not, you will crash your site-content * * Make sure dot_irecommendthis() returns its output, and not echo */ if ( function_exists( 'dot_irecommendthis' ) $portfoliogrid .= dot_irecommendthis(); /* * comments_popup_link echos its output. There is no version that returns it output * Needs a workaround. See this article by Otto and use that function as a workaround * Link to article http://www.thescubageek.com/code/wordpress-code/add-get_comments_popup_link-to-wordpress/ * Replace comments_popup_link with get_comments_popup_link and first check if the function * exists. Remember to copy the function from the link from Otto to functions.php */ if ( function_exists( 'get_comments_popup_link' ) $portfoliogrid .= '' . get_comments_popup_link('Leave a Reply', '1', '%') . ''; $portfoliogrid .= '
'; $portfoliogrid .= '
'; $portfoliogrid .= '' . $attribute['readmore_text'] . ' '; $portfoliogrid .= '
'; $portfoliogrid .= '
'; } $portfoliogrid .='
'; } } /* * Don't wrap booleans in quotes as described earlier * Set our new attribute scheme */ if ( $attribute['pagination'] == true ){ $portfoliogrid .= '
'; /* * Remember to set our maximum pages for the custom query*/ if ( function_exists( 'suareztheme_pagination' ) ) { $portfoliogrid .= suareztheme_pagination( $q->max_num_pages ); } else { $portfoliogrid .= get_next_posts_link( '«« Older Posts', $q->max_num_pages ); $portfoliogrid .= get_previous_posts_link( 'Newer Posts »»' ); } $portfoliogrid .= '
'; } /* * Do not use wp_reset_query(), it is used with query_posts which you MUST NEVER use * Use wp_reset_postdata() */ wp_reset_postdata(); return $portfoliogrid; } add_shortcode( 'recentblog', 'recent_blog_shortcode' );