How can I add rel next and previous in drupal-7 term pages programmatically? and how important is rel for google?
some of my pages are listed here:
کاردستی
سوپ ساده
عکس کودک
کاردستی
کاردستی
there was no solution for it as a module i myself developed a module myself.
i wish it could help you
first of all we user hook_preprocess_html
and i checked if this page is a term page or not
if (arg(0) == 'taxonomy' && arg(1) == 'term') {
and then with use of page argument and function taxonomy_select_nodes you can find on what page your are and what is the next and prevoius page
but one important this,
First page has no previous page
Last page has no next page
and there is no page with argument page=1, this is the first page
function your_theme_preprocess_html(&$variables) {
if (arg(0) == 'taxonomy' && arg(1) == 'term') {
$term = taxonomy_term_load(arg(2));
if( $_GET && $_GET['page'] && is_numeric(#$_GET['page']) ){
$prev = $_GET['page']-1;
$next = $_GET['page']+1;
$url = url('taxonomy/term/'.arg(2));
if( $_GET['page'] > 1 ){
$head_link = array(
'rel' => 'prev',
'href' => 'http://yourdomain.com'.$url.'?page='.$prev
);
drupal_add_html_head_link($head_link);
}
if( $_GET['page'] == 1 ){
$head_link = array(
'rel' => 'prev',
'href' => 'http://yourdomain.com'.$url
);
drupal_add_html_head_link($head_link);
}
$numbers = taxonomy_select_nodes( arg(2),true ,1000 );
if( count($numbers) > $next * 100 ){
$head_link = array(
'rel' => 'next',
'href' => 'http://yourdomain.com'.$url.'?page='.$next
);
drupal_add_html_head_link($head_link);
}
}
else {
$numbers = taxonomy_select_nodes( arg(2),true ,1000 );
if( count($numbers) > 100 ){
$url = url('taxonomy/term/'.arg(2));
$head_link = array(
'rel' => 'next',
'href' => 'http://yourdomain.com'.$url.'?page=1'
);
drupal_add_html_head_link($head_link);
}
}
}
}
this function taxonomy_select_nodes is really awesome, you give it "tid" of a term, it tells you how node is tagged with this term.
even you can add class to body with hook_preprocess_html
write all this code in template.php file in your theme
Related
I am using a custom plugin to add previous and next links in the "admin edit post" page. This so that the author can easily skip from his own posts (previous to next) without having to leave that page in the backend.
I am however struggling with how I need to get there.
Is the below adjustable to make it display only the current editing user ?
function change_apn_post_status( $post_statuses, $post_type ) {
// Add a post status.
// Note: by default these are already in the $post_statuses array: 'draft', 'future', 'pending', 'private', 'publish'
$post_statuses[] = 'trash';
// Remove post status(es).
$post_statuses_to_remove = array( 'draft' ); // Customize here.
if ( 'page' === $post_type ) {
$post_statuses_to_remove[] = 'pending';
}
foreach ( $post_statuses_to_remove as $remove ) {
if ( false !== $index = array_search( $remove, $post_statuses ) ) {
unset( $post_statuses[ $index ] );
}
}
return array_values( $post_statuses );
}
add_filter( 'c2c_admin_post_navigation_post_statuses', 'change_apn_post_status', 10, 2 );'
I thought this was a good starting point but alas I can't get it to work
global $current_user;
wp_get_current_user();
$author_query = array(
'posts_per_page' => '-1',
'author' => $current_user->ID
);
I have this code which I use to filter Woocommerce product page, products by attributes, please can someone help me tweak this to filter Woocommerce shop orders by attribute such as 'brand' or 'weight'.
I have tried changing it myself but it doesn't return any results, any help would be very much appreciated.
Original code found here> Add a filter dropdown for specific product attribute in woocommerce admin product list
add_action('restrict_manage_posts', 'product_attribute_sorting_dropdown');
function product_attribute_sorting_dropdown() {
global $typenow;
$taxonomy = 'pa_brand';
if ( $typenow == 'product' ) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Attribute {$info_taxonomy->label}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
};
}
add_action('parse_query', 'product_attribute_sorting_query');
function product_attribute_sorting_query( $query ) {
global $pagenow;
$taxonomy = 'pa_brand';
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'product' && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}
I'm working on a Wordpress theme. The theme is Classifieds theme from premiumpress. The theme has a shortcode to list all the listings. The corresponding shortcode is [LISTINGS].
The function for the shortcode is as follows
/* =============================================================================
[LISTINGS] - SHORTCODE
========================================================================== */
function wlt_page_listings( $atts, $content = null ) {
global $userdata, $wpdb, $CORE; $STRING = ""; $extra=""; $i=1; $stopcount = 4;
extract( shortcode_atts( array( 'query' => '', 'show' => '', 'type' => '', 'cat' => '', 'orderby' => '', 'order' => '', 'grid' => "no", 'featuredonly' => "no"), $atts ) );
// SETUP DEFAULTS
if(!isset($atts['show']) || (isset($atts['show']) && $atts['show'] == "") ){ $atts['show'] = 5; }
if($atts['type'] == ""){ $atts['type'] = THEME_TAXONOMY.'_type'; }
if($atts['orderby'] == ""){ $atts['orderby'] = "post_title"; }
if($atts['order'] == ""){ $atts['order'] = "desc"; }
// DEFAULT FOR LIST STYLE
if($grid == "yes"){
$sstyle = "grid_style";
$STRING .= '<script language="javascript">jQuery(window).load(function() { equalheight(\'.grid_style .item .thumbnail\');});</script>';
}else{
$sstyle = "list_style";
}
$query= str_replace("#038;","&",$query);
if(strlen($query) > 1){
// ADD ON POST TYPE FOR THOSE WHO FORGET
if(strpos($query,'post_type') == false){
$args = $query ."&post_type=".THEME_TAXONOMY."_type";
}else{
$args = $query;
}
}elseif($featuredonly == "yes"){
$args = array('posts_per_page' => $atts['show'],
'post_type' => $atts['type'], 'orderby' => $atts['orderby'], 'order' => $atts['order'],
'meta_query' => array (
array (
'key' => 'featured',
'value' => 'yes',
)
)
);
}else{
/*** default string ***/
$args = array('posts_per_page' => $atts['show'], 'post_type' => $atts['type'], 'orderby' => $atts['orderby'], 'order' => $atts['order'] );
}
/*** custom category ***/
if(strlen($atts['cat']) > 1){
$args = array('tax_query' => array( array( 'taxonomy' => str_replace("_type","",$atts['type']) ,'field' => 'term_id','terms' => array( $atts['cat'] ))), 'posts_per_page' => $atts['show'] );
}
// BUILD QUERY
$the_query = new WP_Query( hook_custom_queries($args) );
if ( $the_query->have_posts() ) {
$STRING .= '<div class="_searchresultsdata"><div class="wlt_search_results row '.$sstyle.'">';
while ( $the_query->have_posts() ) { $the_query->the_post(); $post = get_post();
$STRING .= '<div class="item '.hook_gallerypage_item_class('col-md-4').$CORE->FEATURED($post->ID).'">'.hook_item_cleanup(hook_gallerypage_item($CORE->ITEM_CONTENT($post))).'</div>';
}
$STRING .= '</div></div><div class="clearfix"></div>';
}
// END QUERY
wp_reset_postdata();
return $STRING;
}
add_shortcode( 'LISTINGS', array($this,'wlt_page_listings') );
The shortcode does not have an attribute to hide certain categories. I need to display all listings, except the ones in wedding category, which is a custom taxonomy. Is there any way to do that with the above code?
Will something like this work?
if ( is_tax( 'listing', 'wedding' ) ) {
do not display the wedding listings and display the rest}
Any suggestions?
EDITS:
This my online site url : http://webzer.comxa.com/
The main page shows the all the products.I like to have all but not one that is from wedding category coz i have separate page to list wedding category.
i have tried this where 51 is the page id of my home store page
if ( is_page( 51 ) && is_tax( 'listing', 'wedding' ) ) {
?><style>.caption {display:none!important;}</style>
<?php } ?>
this also didn't work
Consider this :
change
function wlt_page_listings( $atts, $content = null ) {
to
function wlt_page_listings( $atts, $content = null, $exclude=array(99) ) { // 99 is wedding cat id
where $exclude is an optional array of excluded cat names (99 in there for ease of testing/use)
Then in the
while ( $the_query->have_posts() ) {
add something like this:
$post = get_post(); // get post obj, will use the ID attr
$cats = wp_get_post_categories( $post->ID )); // returns array of IDs for all cats
foreach($exclude as $x){ // loop on the excluded cat ids
if(in_array($x, $cats))continue; // if excluded skip
}
http://codex.wordpress.org/Function_Reference/wp_get_post_categories
I think this will work or at least got you close.
display:none is bad mojo as the content will still be in your source code for others to see.
I hope I addressed the problem correctly for you, cheers.
I have two custom taxonomies.
I wanna filter my product,obtaining the same result of this wp_query.
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'auto',
'field' => 'ID',
'terms' => $auto_id
),
array(
'taxonomy' => 'ricambio',
'field' => 'ID',
'terms' => $ricambi_id
)
),
'post_type' => 'product',
'orderby' => 'title',
);
$query = new WP_Query( $args );
How i show this filtering result ?
I have to create a template like taxonomy-auto-ricambio.php ? archive-auto-ricambio ?
If i in my url address write like this:
http://www.myshop.com/?auto=my_auto&ricambio=my_ricambio
it works.
How can i show it in a template with a pretty url ?
Thanks.
Pretty much pulling this directly from this excellent article on advanced taxonomies with pretty urls.
First create some new re-write rules. This should probably be in a site-specific snippets plugin and NOT in your theme. Then go to permalinks and re-save your permalinks.
function so_25722819_add_rewrite_rules() {
global $wp_rewrite;
$new_rules = array(
'event/(industry|location)/(.+?)/(industry|location)/(.+?)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4),
'event/(industry|location)/(.+)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'so_25722819_add_rewrite_rules' );
Now if you visit your site with the url:
www.example.com/auto/my_auto/ricambio/my_ricambio
You should go to a taxonomy archive page that shows the same result as:
www.example.com/?auto=my_auto&ricambio=my_ricambio
To echo out these pretty permalinks you can use the plugin the article author provided, with just a tiny adjustment to switch from his eg_events post type to your product post type.
function eg_get_filter_permalink( $taxonomy_slug, $term ) {
global $wp_query;
// If there is already a filter running for this taxonomy
if( isset( $wp_query->query_vars[$taxonomy_slug] ) ){
// And the term for this URL is not already being used to filter the taxonomy
if( strpos( $wp_query->query_vars[$taxonomy_slug], $term ) === false ) {
// Append the term
$filter_query = $taxonomy_slug . '/' . $wp_query->query_vars[$taxonomy_slug] . '+' . $term;
} else {
// Otherwise, remove the term
if( $wp_query->query_vars[$taxonomy_slug] == $term ) {
$filter_query = '';
} else {
$filter = str_replace( $term, '', $wp_query->query_vars[$taxonomy_slug] );
// Remove any residual + symbols left behind
$filter = str_replace( '++', '+', $filter );
$filter = preg_replace( '/(^\+|\+$)/', '', $filter );
$filter_query = $taxonomy_slug . '/' . $filter;
}
}
} else {
$filter_query = $taxonomy_slug . '/' . $term;
}
// Maintain the filters for other taxonomies
if( isset( $wp_query->tax_query ) ) {
foreach( $wp_query->tax_query->queries as $query ) {
$tax = get_taxonomy( $query['taxonomy'] );
// Have we already handled this taxonomy?
if( $tax->query_var == $taxonomy_slug )
continue;
// Make sure taxonomy hasn't already been added to query string
if( strpos( $existing_query, $tax->query_var ) === false )
$existing_query .= $tax->query_var . '/' . $wp_query->query_vars[$tax->query_var] . '/';
}
}
if( isset( $existing_query ) )
$filter_query = $existing_query . $filter_query;
return trailingslashit( get_post_type_archive_link( 'product' ) . $filter_query );
}
Then in any template you can use the above function to generate a pretty permalink to your multiple taxonomy archive:
// Link to page with only my_ricambio in my_auto
echo eg_get_filter_permalink( 'my_auto', 'my_ricambio' );
I am currently using the Multi Post Thumbnails plugin for Wordpress, but I only want the extra thumbnails provided by the plugin to show on one specific page. The plugin does not appear to natively support this functionality but it seems like something that would be pretty easy to add, I'm just not sure of the right way to go about it as I'm fairly new to Wordpress development.
The code for Multi Post Thumbnails is the following, which simply goes in functions.php:
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(
array(
'label' => 'Secondary Image',
'id' => 'secondary-image',
'post_type' => 'page'
)
);
new MultiPostThumbnails(
array(
'label' => 'Tertiary Image',
'id' => 'tertiary-image',
'post_type' => 'page'
)
);
}
It seems to me it would just be a simple case of wrapping this in a check so that it only runs for a specific page ID, but I'm not quite sure how to go about doing that.
This is probably somewhat of a hack. To my knowledge post/page id's are not accessible from inside functions.php.
// get the id of the post/page based on the request uri.
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$post_id = url_to_postid($url);
// the id of the specific page/post.
$specific_post_id = 3;
// check if the requested post id is identical to the specific post id.
if ($post_id == $specific_post_id) {
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(
array(
'label' => 'Secondary Image',
'id' => 'secondary-image',
'post_type' => 'page'
)
);
new MultiPostThumbnails(
array(
'label' => 'Tertiary Image',
'id' => 'tertiary-image',
'post_type' => 'page'
)
);
}
}
This is also probably a hack but it worked for me. I got stung by the AJAX 'post_id' back to the admin page once the image has been selected. My usage was for a slug but the function could easily be modified for a post ID.
function is_admin_edit_page( $slug ){
if( ( isset($_GET) && isset($_GET['post']) ) || ( isset($_POST) && isset($_POST['post_id']) ) )
{
$post_id = 0;
if(isset($_GET) && isset($_GET['post']))
{
$post_id = $_GET['post'];
}
else if(isset($_POST) && isset($_POST['post_id']))
{
$post_id = $_POST['post_id'];
}
if($post_id != 0)
{
$c_post = get_post($post_id);
if( $c_post->post_name == $slug )
{
return true;
}
}
}
return false;
}
if( is_admin_edit_page('work') ) {
new MultiPostThumbnails(
array(
'label' => 'Hero 1 (2048px x 756px JPEG)',
'id' => 'am-hero-1',
'post_type' => 'page'
)
);
}