Hello Micheal,
Since my earlier reply I have had a further look into this issue.
When sanitize is added it adds the entry to the postmeta table. I have also managed to get the category of downloads to display on the page using:
$download_category = get_post_meta(get_the_ID(), '_ct_contact_download_cat', true);
//echo $download_category;
global $wpdb;
$sql='select term_id from wp_qzsny57q7q_terms where slug = "'.$download_category.'"';
$posts = $wpdb->get_results($sql);
//print_r($posts);
foreach ($posts as $post)
{
//$post->FIELD1
$category_id = $post->term_id;
}
?>
However, the drop down meta box (taxonomy select) in the page editor does not pull the correct category id from the postmeta table.
So for instance, you can select a different category from the drop down and save it to the database but this does not update in the metabox on the edit page.
I have stripped back the full websites code until I resolve this issue, please see the metabox.php code below:
<?php
/**
* Include and setup custom metaboxes and fields. (make sure you copy this file to outside the CMB2 directory)
*
* Be sure to replace all instances of 'yourprefix_' with your project's prefix.
* http://nacin.com/2010/05/11/in-wordpress-prefix-everything/
*
* @category YourThemeOrPlugin
* @package Demo_CMB2
* @license http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
* @link https://github.com/WebDevStudios/CMB2
*/
/**
* Get the bootstrap! If using the plugin from wordpress.org, REMOVE THIS!
*/
if ( file_exists( diame( __FILE__ ) . '/cmb2/init.php' ) ) {
require_once diame( __FILE__ ) . '/cmb2/init.php';
} elseif ( file_exists( diame( __FILE__ ) . '/CMB2/init.php' ) ) {
require_once diame( __FILE__ ) . '/CMB2/init.php';
}
/**
* Conditionally displays a metabox when used as a callback in the 'show_on_cb' cmb2_box parameter
*
* @param CMB2 object $cmb CMB2 object
*
* @retu bool True if metabox should show
*/
function ct_show_if_front_page( $cmb ) {
// Don't show this metabox if it's not the front page template
if ( $cmb->object_id !== get_option( 'page_on_front' ) ) {
retu false;
}
retu true;
}
/**
* Conditionally displays a field when used as a callback in the 'show_on_cb' field parameter
*
* @param CMB2_Field object $field Field object
*
* @retu bool True if metabox should show
*/
function ct_hide_if_no_cats( $field ) {
// Don't show this field if not in the cats category
if ( ! has_tag( 'cats', $field->object_id ) ) {
retu false;
}
retu true;
}
/**
* Manually render a field.
*
* @param array $field_args Array of field arguments.
* @param CMB2_Field $field The field object
*/
function ct_render_row_cb( $field_args, $field ) {
$classes = $field->row_classes();
$id = $field->args( 'id' );
$label = $field->args( 'name' );
$name = $field->args( '_name' );
$value = $field->escaped_value();
$description = $field->args( 'description' );
?>
<div class="custom-field-row <?php echo $classes; ?>">
<p><label for="<?php echo $id; ?>"><?php echo $label; ?></label></p>
<p><input id="<?php echo $id; ?>" type="text" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/></p>
<p class="description"><?php echo $description; ?></p>
</div>
<?php
}
/**
* Manually render a field column display.
*
* @param array $field_args Array of field arguments.
* @param CMB2_Field $field The field object
*/
function ct_display_text_small_column( $field_args, $field ) {
?>
<div class="custom-column-display <?php echo $field->row_classes(); ?>">
<p><?php echo $field->escaped_value(); ?></p>
<p class="description"><?php echo $field->args( 'description' ); ?></p>
</div>
<?php
}
/**
* Conditionally displays a message if the $post_id is 2
*
* @param array $field_args Array of field parameters
* @param CMB2_Field object $field Field object
*/
function ct_before_row_if_2( $field_args, $field ) {
if ( 2 == $field->object_id ) {
echo '<p>Testing <b>"before_row"</b> parameter (on $post_id 2)</p>';
} else {
echo '<p>Testing <b>"before_row"</b> parameter (<b>NOT</b> on $post_id 2)</p>';
}
}
add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
/**
* Hook in and add a demo metabox. Can only happen on the 'cmb2_admin_init' or 'cmb2_init' hook.
*/
/***** HOME PAGE START *****/
add_action( 'cmb2_init', 'ct_register_contact_page_metabox' );
/**
* Hook in and add a metabox that only appears on the 'home' page
*/
function ct_register_contact_page_metabox() {
// Start with an underscore to hide fields from custom fields list
$prefix = '_ct_contact_';
/**
* Metabox to be displayed on a single page ID
*/
$cmb_contact_page = new_cmb2_box( array(
'id' => $prefix . 'metabox',
'title' => __( 'home Page Metabox', 'cmb2' ),
'object_types' => array( 'page', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'show_on' => array( 'key' => 'page-template', 'value' => 'template-service-default.php' ),
) );
$cmb_contact_page->add_field( array(
'name' => __( 'Downloads Categorys', 'cmb2' ),
'desc' => __( 'Select the downloads category.', 'cmb2' ),
'id' => $prefix . 'download_cat',
'type' => 'taxonomy_select',
'taxonomy' => 'form_category',
'sanitization_cb' => 'sanitize_text_field',
) );
Here is the nessesary code from the php template:
<?php
/*
Template Name: Single Service Template Default
*/
$download_category = get_post_meta(get_the_ID(), '_ct_contact_download_cat', true);
//echo $download_category;
global $wpdb;
$sql='select term_id from wp_qzsny57q7q_terms where slug = "'.$download_category.'"';
$posts = $wpdb->get_results($sql);
//print_r($posts);
foreach ($posts as $post)
{
//$post->FIELD1
$category_id = $post->term_id;
}
?>
<ul>
<?php
// the query
$args = array(
'post_type' => 'forms_pt',
'orderby'=>'title',
'order'=>'ASC',
'posts_per_page' => 7,
'tax_query' => array(
array(
'taxonomy' => 'form_category',
'field' => 'id',
'terms' => $category_id,
),
),
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li><a href="<?= $file; ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<li><a href="/downloads/">View All Forms</a></li>
</ul>
Again, many thanks for all of your help and support so far, I feel that we are 90% of the way there now!
WordPress ...
ما را در سایت WordPress دنبال میکنید
برچسب:
نویسنده: استخدام کار
بازدید: 391
تاريخ: شنبه
16 مرداد
1395 ساعت: 14:34