/**
* PostStatusExtender
*
* @author Hyyan Abo Fakher<[email protected]>
*/
class PostStatusExtender
{
/**
* Extend
*
* Extend the current status list for the given post type
*
* @global \WP_POST $post
*
* @param string $postType the post type name , ex: product
* @param array $states array of states where key is the id(state id) and value
* is the state array
*/
public static function extend($postType, $states)
{
foreach ($states as $id => $state) {
register_post_status($id, $state);
}
add_action('admin_footer-post.php', function() use($postType, $states) {
global $post;
if (!$post || $post->post_type !== $postType) {
return false;
}
foreach ($states as $id => $state) {
printf(
'<script>'
. 'jQuery(document).ready(function($){'
. ' $("select#post_status").append("<option value=\"%s\" %s>%s</option>");'
. ' $("a.save-post-status").on("click",function(e){'
. ' e.preventDefault();'
. ' var value = $("select#post_status").val();'
. ' $("select#post_status").value = value;'
. ' $("select#post_status option").removeAttr("selected", true);'
. ' $("select#post_status option[value=\'"+value+"\']").attr("selected", true)'
. ' });'
. '});'
. '</script>'
, $id
, $post->post_status !== $id ? '' : 'selected=\"selected\"'
, $state['label']
);
if ($post->post_status === $id) {
printf(
'<script>'
. 'jQuery(document).ready(function($){'
. ' $(".misc-pub-section #post-status-display").text("%s");'
. '});'
. '</script>'
, $state['label']
);
}
}
});
add_action('admin_footer-edit.php', function() use($states, $postType) {
global $post;
if (!$post || $post->post_type !== $postType) {
return false;
}
foreach ($states as $id => $state) {
printf(
'<script>'
. 'jQuery(document).ready(function($){'
. " $('select[name=\"_status\"]').append('<option value=\"%s\">%s</option>');"
. '});'
. '</script>'
, $id
, $state['label']
);
}
});
add_filter('display_post_states', function($states, $post) use($states, $postType) {
foreach ($states as $id => $state) {
if ($post->post_type == $postType && $post->post_status === $id) {
return array($state['label']);
} else {
if (array_key_exists($id, $states)) {
unset($states[$id]);
}
}
}
return $states;
}, 10, 2);
}
}
और यहाँ उपयोग करने के लिए कि यह कैसे
add_action('init', function() {
PostStatusExtender::extend(self::NAME, array(
'sold' => array(
'label' => __('Sold', 'viasit'),
'public' => true,
'exclude_from_search' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Sold <span class="count">(%s)</span>', 'Sold <span class="count">(%s)</span>'),
)
));
});
संपादित करें: कोड में तय टाइपो।
स्रोत
2016-11-04 10:19:00
इन स्थितियों का गहरा प्रभाव है कि पदों का इलाज और प्रदर्शन कैसे किया जाता है, है ना? मुझे कल्पना नहीं है कि आप कुछ सूची में सिर्फ एक नया जोड़ सकते हैं। आप कौन सी नई स्थिति जोड़ना चाहते हैं? –
मैं केवल अपने कस्टम पोस्ट प्रकारों में नई स्थिति जोड़ना चाहता हूं और उन पदों के साथ जो कस्टम प्रश्नों के माध्यम से पोस्ट प्रदर्शित कर रहा हूं। वर्डप्रेस के साथ आप किस स्थिति के आधार पर पोस्ट प्रदर्शित करने के लिए क्वेरी कर सकते हैं। तो कुछ स्थितियों को जोड़ना जैसे कि सोल्ड और हटाया गया सिस्टम सिस्टम के लिए कोई मुद्दा नहीं होना चाहिए? – Brady
@ ब्रैडी मैं देखता हूं। मुझे नहीं पता कि यह आसानी से संभव है या नहीं। इसके बजाय टैगिंग/श्रेणियों प्रणाली का उपयोग करने के बारे में क्या? –