I'm using a custom wp-query on my website to display all pages from a parent page inside a list style with select, when selecting a page I'm redirected to the right page.
works fine :
here is my code :
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '22',
'order' => 'ASC',
'orderby'=>'meta_value',
'meta_key'=>'nom_de_loeuvre'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<span class="styled-select">
<select name="" onchange="location = this.options[this.selectedIndex].value;">
<option>A - Z</option>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_field('nom_de_loeuvre'); ?>
, de
<?php if(get_field('prenom_artiste')): ?>
<?php the_field('prenom_artiste'); ?>
<?php endif ;?>
<?php the_field('nom_artiste'); ?> | Galerie
<?php if(get_field('prenom_galerie')): ?>
<?php the_field('prenom_galerie'); ?>
<?php endif ;?>
<?php the_field('nom_galerie'); ?>
</option>
<?php endwhile; ?>
</select>
</span>
<?php endif; wp_reset_query(); ?>
no I'm trying to hightlight the active page of my list, using "option selected value".
when on a page, I want the select list to display the current page in the liste.
Can anybody help me with this ?
I can't find a solution or a way to do it on the web...
I'm not sure if it's possible, but I guess if it is, It must be something to add in my Wp-query or maybe in jquery ?
thanks a lot for your help...
Related
I have a repair prices calculator on my website, where users can first select the brand from a dropdown and then select the exact model from the second dropdown.
Currently the page reloads after selecting an option from the first dropdown and passes the selected value with $_POST, the second dropdown then shows all devices from the selected brand. After selecting the model, the user gets forwarded to the specific device page.
I would prefer to pass the value and show the second dropdown without the page being reloaded. I have tried doing this with ajax, but wasn't able to get the correct code, so I went back to the first working setup. Could anyone tell me the code I need to submit the value to the second dropdown without reloading?
This is the code I have so far:
<h2>Preisrechner</h2>
<div class="preisrechner-select-wrapper">
<form method="POST" action="">
<div>
<?php
$args = array(
'hierarchical' => 1,
'depth' => 1,
'orderby' => 'name',
'echo' => 0,
'taxonomy' => 'marke',
// this leads to variable name $_POST['marke']
'name' => 'marke-sel'
);
if( ! isset($_POST['marke-sel']) ):
$args['show_option_none'] = 'Hersteller auswählen';
else:
$args['selected'] = $_POST['marke-sel'];
endif;
$marke = wp_dropdown_categories( $args );
// this enables the buttonless js possibility
$marke = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $marke);
echo $marke;
?>
<noscript>
<div>
<input type="submit" value="marke"/>
</div>
</noscript>
</div>
</form>
<?php
if( isset($_POST['marke-sel']) && $_POST['marke-sel'] ):
?>
<form method="POST" action="<? bloginfo('url'); ?>">
<input type="hidden" name="marke" value="<?php echo $_POST['marke-sel'] ?>">
<div>
<?php
$the_query = new WP_Query( array(
'post_type' => 'reparaturpreise',
'tax_query' => array(
array (
'taxonomy' => 'marke',
'field' => 'id',
'terms' => $_POST['marke-sel'],
)
),
) );
if ( $the_query->have_posts() ) :
?>
<div class="form-option parent-field-wrapper">
<label for=""/>
<select name='modell' id='modell' onchange='document.location=this.value'>
<option value="">Modell auswählen</option>
<?php while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<option value="<?php the_permalink();?>">
<?php the_title(); ?>
</option>
<?php endwhile; ?>
</select>
</div>
<?php endif;
wp_reset_postdata();
$modell = preg_replace("#<select([^>]*)>#", "<select$1 onchange='this.form.submit()'>", $modell);
echo $modell;
?>
<noscript>
<div>
<input type="submit" value="modell"/>
</div>
</noscript>
</div>
</form>
<?php endif; ?>
<?php
if( isset($_POST['marke-sel']) && $_POST['modell'] ):
$args = array(
'post_type' => 'reparaturpreise',
'cat' => $_POST['marke-sel'],
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;
?>
</div>
If the code generated by PHP needs to change (ie results) then the page will need to reload. The only way around this would be to move this code to an endpoint which can be called via an AJAX call and returns the results.
A good example which can be changed for your scenario can be found here:
https://premium.wpmudev.org/blog/using-ajax-with-wordpress/
I'm trying to get a page ID before and after a post loop, the page ID is displaying correctly before the loop, but when I try and display it after the loop it just displays the last post ID of the loop.
I've tried using all of the following to reset the loop and none seem to work:
wp_reset_postdata()
wp_reset_query()
rewind_posts()
The loop code is:
<?php
echo 'Shows the page ID (correct)=' . $post->ID;
$args = array(
'post_type' => 'accommodation',
'posts_per_page' => '9999',
);
$wp_query = new WP_Query( $args );
if ($wp_query->have_posts()) :
while($wp_query->have_posts()) :
$wp_query->the_post(); ?>
<option value="<?php the_ID(); ?>"><?php echo the_title(); ?></option>
<?php endwhile;
else :
esc_html_e('No bookings','sohohotel');
endif;
wp_reset_postdata();
echo 'Shows the ID of the last post in the loop (not correct)=' . $post->ID; ?>
Note I'm using WP_Query and not query_posts, so I'm not sure where I'm going wrong, any ideas greatly appreciated!
use this
$wp_query = new WP_Query( $args );
if ($wp_query->have_posts()) : $wp_query->the_post();
while($wp_query->have_posts()) :
$wp_query->the_post(); ?>
<option value="<?php the_ID(); ?>"><?php the_title(); ?></option>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else :
esc_html_e('No bookings','sohohotel');
endif;
I'm building a meta box for one of my custom post types that has a selection option. I'm trying to make it so that when you make a selection and update the post, the option stays selected when the page reloads. I've found a few people on StackOverflow working on the same and I've followed those suggestions but haven't quite figured it out yet. If anyone has any suggestions, any help is appreciated.
<?php
function property_info_meta_box() {
add_meta_box('property-info', 'Property', 'property_info_cb', 'properties', 'normal', 'high');
}
add_action('add_meta_boxes', 'property_info_meta_box');
function property_info_cb($propertyInfo) {
$selectAgent = get_post_meta($propertyInfo->ID, 'select-agent-value', true);
?>
<label for="select-agent-text">Select Agent</label> <br>
<select multiple size="5" name="select-agent" id="select-agent">
<option value="none">None</option>
<?php
$args = array(
'post_type' => 'agents',
'posts_per_page' => -1
);
$posts = new WP_Query($args);
if ( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post(); ?>
<option value="<?php the_ID() ?>" <?php selected( $selectAgent, the_ID() ); ?>> <?php the_title(); ?> </option>
<?php endwhile; endif; ?>
</select>
<?php }
function add_property_info_fields ($propertyInfoId, $propertyInfo){
if ($propertyInfo->post_type == 'properties') {
if(isset($_POST['select-agent'])){
update_post_meta($propertyInfoId, 'select-agent-value', $_POST['select-agent']);
}
}
}
add_action('save_post', 'add_property_info_fields', 10, 2);
Shot in the dark here, but you are using the_ID() improperly. This function prints the ID to the screen. You are trying to return the ID for use as a function parameter. You should try something like:
<?php selected( $selectedAgent, get_the_ID() ); ?>
See get_the_ID() vs the_ID()
I'm using a php code to display children pages from a specific parent page ID to display page links in a select list type.
all my children pages have a sub_field called "artiste", with an artist name.
here is my php code to display the page name with link to the page :
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '22',
'order' => 'ASC',
'orderby' => 'title'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="styled-select">
<select name="" onchange="location = this.options[this.selectedIndex].value;">
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></option>
<?php endwhile; ?>
</select>
</div>
<?php endif; wp_reset_query(); ?>
this works fine, I get all the children page titles and the permalink and sorted alphabetically.
now I want to get instead of the page title, a custom field from each children pages, here is my code :
my custom field is "the_sub_field('nom_artiste');"
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '22',
'order' => 'ASC',
'orderby' => 'title'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="styled-select">
<select name="" onchange="location = this.options[this.selectedIndex].value;">
<option>--- A - Z ---</option>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php if(get_field('artiste')): while (has_sub_field('artiste') ): ?><?php the_sub_field('nom_artiste'); ?><?php endwhile; endif; ?></option>
<?php endwhile; ?>
</select>
</div>
<?php endif; wp_reset_query(); ?>
it works also, I get the custom field instead of the page title, and the permalink of my page title, that's perfect.
but in this second code, my custom field are sorted depending of there page title.
for example, I have 2 pages :
AAAAA
ZZZZZ
and custom fields :
for AAAA : custom field is GGGG
for ZZZZ : custom field is BBBB
so with the second php code, my list is not sorted right because it sorted depending on the page title and not of custom field... I get :
GGGG
BBBB
I think I have to create an array with my custom field to be abble to sort them alphabetically before displaying them in my select onchange, but I don't know how to do it...
can anybody help me with this ?
thanks a lot for your help
Try this, replacing everything between <?php while ... endwhile; ?> in your code:
<?php
//Declare an array to hold data
$options = array();
//The loop
while ( $parent->have_posts() ) : $parent->the_post();
//Define a temporary array
$option = array();
//Get all the values we need and store them in an array.
$option['value'] = get_permalink();
$option['title'] = get_the_title();
if(get_field('artiste')):
while (has_sub_field('artiste') ) {
$option['label'] = get_sub_field('nom_artiste');
}
else:
$option['label'] = get_the_title(); //Put something here if artiste is not populated.
endif;
$options[] = $option;
endwhile;
//Sort the array on the 'label' field
usort($options, function($a, $b) { return strcmp($a['label'], $b['label']); });
//Output the <option> tags
$format = ' <option value="%s" title="%s">%s</option>'."\n";
foreach ($options as $o) {
printf($format, $o['value'], $o['title'], $o['label']);
}
?>
I have updated 20 posts in wordpress theme. And i need to add my own text box after every 5 posts. So that i can add 4 text box after every 5 post.
I think it can be done by $post_counter please any one give me a query for my question.
My code simply seems to,
<?php
query_posts( array('posts_per_page'=>20,orderby=>post_date, order=>desc) );
while ( have_posts() ) : the_post();
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php endwhile; ?>
I believe this is what you need
<?php
$query = new WP_Query( array('posts_per_page' => 20, orderby => post_date, order => desc) );
$p = 1;
while ( $query->have_posts() ) : $query->the_post();
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php echo ($p%5 == 0) ? '<input type="text" name="mytext[]" />' : "";
$p++;
?>
<?php endwhile; ?>
I read your comment and i updated #zameerkhan code.
<?php
$query = new WP_Query( array('posts_per_page' => 20, orderby => post_date, order => desc) );
$p = 1;
while ( $query->have_posts() ) : $query->the_post();
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
//this will create text box after 5 post with name mytext1,mytext2 etc.
<?php echo ($p%5 == 0) ? '<input type="text" name="mytext'.($p/5).'" />': "";
$p++;
?>
<?php endwhile; ?>
You should never use query_posts, forget that it exists or ever existed. Use WP_Query or pre_get_posts
FROM THE CODEX ON query_posts
Note: This function isn't meant to be used by plugins or themes. As
explained later, there are better, more performant options to alter
the main query. query_posts() is overly simplistic and problematic way
to modify main query of a page by replacing it with new instance of
the query
Your code should look like this
$args = array(
'order' => 'DESC',
'posts_per_page'=>20,
'orderby' => 'post_date',
);
$the_query = new WP_Query( $args );
$p = 1;
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
//this will create text box after 5 post with name mytext1,mytext2 etc.
<?php echo ($p%5 == 0) ? '<input type="text" name="mytext'.($p/5).'" />': "";
$p++;
?>
<?php endwhile; ?>
From your code
<?php
query_posts( array('posts_per_page'=>20,orderby=>post_date, order=>desc) );
$p = 1;
while ( have_posts() ) : the_post();
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php echo ($p%5 == 0) ? '<input type="text" name="mytext[]" />' : "";
$p++;
?>
<?php endwhile; ?>
Instead of <input type="text" name="mytext[]"/> i replace <h3>This is First text box</h3>
So, now my output is,
Title 1Title 2Title3Title4Title5This is First text box
Title 6Title 7Title 8Title 9Title 10This is First text box
Title 11Title 12Title 13Title 14Title 15This is First text box...
Here see that "This is First text box" returns 4 times. My question is, i need after Title 10 the text should display as "This is Second text box" and so on.. I think now its clear