Craft Video Dictionary https://www.craftvideodictionary.org
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

functions.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // Allow [shortcode] in templates
  3. add_action( 'acf/init', 'set_acf_settings' );
  4. function set_acf_settings() {
  5. acf_update_setting( 'enable_shortcode', true );
  6. }
  7. add_filter( 'acf/shortcode/allow_in_block_themes_outside_content', '__return_true' );
  8. // Current Year [shortcode]
  9. function current_year_shortcode() {
  10. return date('Y');
  11. }
  12. add_shortcode( 'current_year', 'current_year_shortcode' );
  13. // General Settings - Set Default Featured Image Placeholder if none exists
  14. if ( ! class_exists( 'DefaultThumbnailID' ) ) {
  15. class DefaultThumbnailID {
  16. private static $instance;
  17. public function __construct() {
  18. // Add the option field to the General page.
  19. add_action( 'admin_init', array( &$this, 'show_default_thumbnail_id_option' ) );
  20. // Check if the default featured image exists.
  21. add_filter( 'has_post_thumbnail', array( &$this, 'set_has_post_thumbnail_value' ) );
  22. // Display the default featured image.
  23. add_filter( 'post_thumbnail_html', array( &$this, 'set_default_post_thumbnail_image' ), 10, 4 );
  24. }
  25. public static function get_instance() {
  26. if ( is_null( self::$instance ) ) {
  27. self::$instance = new self();
  28. }
  29. return self::$instance;
  30. }
  31. public function show_default_thumbnail_id_option() {
  32. register_setting(
  33. 'general', // General page
  34. '_default_thumbnail_id' // Option name
  35. );
  36. add_settings_field(
  37. 'default_thumbnail_id', // ID
  38. __( 'Default thumbnail ID', 'text-domain' ), // Option title
  39. array( &$this, 'display_input_field' ), // Display callback
  40. 'general', // General page
  41. 'default' // General page section
  42. );
  43. }
  44. public function display_input_field() {
  45. $value = get_option( '_default_thumbnail_id' );
  46. $value = wp_attachment_is_image( $value ) ? $value : '';
  47. ?>
  48. <input id="default_thumbnail_id" value="<?php echo esc_attr( $value ); ?>" name="_default_thumbnail_id"/>
  49. <?php
  50. }
  51. function set_has_post_thumbnail_value( $has_image ) {
  52. $default_thumbnail_id = get_option( '_default_thumbnail_id' );
  53. if ( ! empty( $default_thumbnail_id ) ) {
  54. $has_image = true;
  55. }
  56. return $has_image;
  57. }
  58. function set_default_post_thumbnail_image( $image, $post_id, $post_thumbnail_id, $size ) {
  59. $default_thumbnail_id = get_option( '_default_thumbnail_id' );
  60. if ( empty( $image ) && ! empty( $default_thumbnail_id ) ) {
  61. $image = wp_get_attachment_image( $default_thumbnail_id, $size );
  62. }
  63. return $image;
  64. }
  65. }
  66. // Instantiate the class object.
  67. DefaultThumbnailID::get_instance();
  68. }
  69. // Order by meta_key 'sortname' for post type 'artists'
  70. function my_pre_get_posts( $query ) {
  71. // do not modify queries in the admin
  72. if( is_admin() ) {
  73. return $query;
  74. }
  75. // only modify queries for 'artist' post type
  76. if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'artist' ) {
  77. $query->set('orderby', 'meta_value');
  78. $query->set('meta_key', 'sortname');
  79. $query->set('order', 'ASC');
  80. }
  81. // return
  82. return $query;
  83. }
  84. add_action('pre_get_posts', 'my_pre_get_posts');
  85. // Exclude Pages from Search Results
  86. function search_filter($query) {
  87. if ( ! is_admin() && $query->is_main_query() ) {
  88. if ( $query->is_search ) {
  89. $query->set( 'post_type', 'post' );
  90. }
  91. }
  92. }
  93. add_action( 'pre_get_posts', 'search_filter' );
  94. /**
  95. * This function modifies the main WordPress query to include an array of
  96. * post types instead of the default 'post' post type.
  97. * @param object $query The main WordPress query.
  98. */
  99. function include_custom_post_types_in_search_results( $query ) {
  100. if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
  101. $query->set( 'post_type', array( 'post', 'artist' ) );
  102. }
  103. }
  104. add_action( 'pre_get_posts', 'include_custom_post_types_in_search_results' );