自定义帖子收到错误帖子的每年存档

时间:2021-10-03 作者:Daniel Leandro

获取错误帖子的自定义帖子的每年存档

我得到了wp\\u get\\u the\\u archives()来列出我的帖子的年份,但即使url没有问题,年份归档中显示的帖子也会根据我每年的帖子数量给出最后一篇帖子。

例如,我2008年有3篇文章,那一年的档案只会显示3篇文章,但仅显示最后三篇(2021 2篇,2016年1篇)。如果我有2016年的1篇文章,档案将显示一篇文章,但这只是2021的最后一篇。等等我做错了什么?

我的职能。php文件


<?php 
function recursos_ah6studio(){
    wp_enqueue_style(\'bootstrap\', get_theme_file_uri(\'/css/bootstrap.min.css\'));
    wp_enqueue_style(\'slick\', get_theme_file_uri(\'/slick/slick.css\'));
    wp_enqueue_style(\'slick-theme\', get_theme_file_uri(\'/slick/slick-theme.css\'));
    wp_enqueue_style(\'magnific-popup\', get_theme_file_uri(\'/magnific-popup/magnific-popup.css\'));
    wp_enqueue_style(\'font-awesome\', get_theme_file_uri(\'/fontawesome-5.5/css/all.min.css\'));
    wp_enqueue_style(\'estilo_ah6studio\', get_stylesheet_uri());
    
    /*wp_enqueue_script(\'mainJS\', get_theme_file_uri(\'/js/main_js.js\'), NULL, \'1.0\', true); */
    wp_enqueue_script(\'jquery\', get_theme_file_uri(\'/js/jquery-3.5.1.min.js\'), NULL, \'1.0\', true);
    wp_enqueue_script(\'popper\', get_theme_file_uri(\'/js/popper.min.js\'), NULL, \'1.0\', true);
    wp_enqueue_script(\'boostrap\', get_theme_file_uri(\'/js/bootstrap.min.js\'), NULL, \'1.0\', true);
    wp_enqueue_script(\'slickJS\', get_theme_file_uri(\'/slick/slick.min.js\'), NULL, \'1.0\', true);
    wp_enqueue_script(\'magnific-popup\', get_theme_file_uri(\'/magnific-popup/jquery.magnific-popup.min.js\'), NULL, \'1.0\', true);
    wp_enqueue_script(\'easingJS\', get_theme_file_uri(\'/js/easing.min.js\'), NULL, \'1.0\', true);
    wp_enqueue_script(\'singlePageJS\', get_theme_file_uri(\'/js/jquery.singlePageNav.min.js\'), NULL, \'1.0\', true); 
    wp_enqueue_script(\'tool-tip\', get_theme_file_uri(\'/js/tooltip.js\'), NULL, \'1.0\', true);

} 
add_action(\'wp_enqueue_scripts\', \'recursos_ah6studio\');

function mytheme_enqueue_front_page_scripts() {
    if( is_front_page() )
    {
        wp_enqueue_script(\'bkcground-random\', get_theme_file_uri(\'/js/background_random.js\'), NULL, \'1.0\', true);   
    }
}
add_action( \'wp_enqueue_scripts\', \'mytheme_enqueue_front_page_scripts\' );

function ourWidgetInit(){

register_sidebar( array (
\'name\' => \'Sidebar\',
\'id\' => \'sidebar1\',

));

register_sidebar( array (
    \'name\' => \'Desplegable\',
    \'id\' => \'desplega1\',
    \'before_widget\'=>\'<div class="widget-item">\',
    \'after_widget\'=> \'</div>\',
    \'before_title\'=>\'<h6 class="category_title">\',
    \'after_title\'=>\'</h6>\', 
    ));
}

add_action(\'widgets_init\', \'ourWidgetInit\');

//Añadir el thumbnail//
function ah6studio_setup() {
    add_theme_support(\'post-thumbnails\');
    add_theme_support(\'title-tag\'); 
}
add_action(\'after_setup_theme\',\'ah6studio_setup\');

我的插件文件ah6\\U postypes。php


<?php

function ah6_papers() {
    register_post_type(\'papers\', array(
        \'has_archive\'=> true,
        \'public\'=> true,
        \'posts_per_page\' => -1,
        \'labels\'=> array(
            \'name\'=>\'Papers\',
            \'add_new\'=> \'Agregar un Paper\',
            \'edit_item\'=>\'Editar un Paper\',
            \'all_items\'=>\'Todos los Papers\',
            \'singular_name\'=>\'Paper\'
            
        ),
        \'taxonomies\' => array(\'temas\'),
        \'menu_icon\'=>  \'dashicons-media-text\',
    ));
}

add_action(\'init\', \'ah6_papers\');


function papers_categories() {
    //Taxonomía: TEMAS
    //Traducción para el backend

      $labels = array(
        \'name\' => _x( \'Temas\', \'taxonomy general name\' ),
        \'singular_name\' => _( \'Tema\', \'taxonomy singular name\' ),
        \'search_items\' =>  __( \'Buscar tema\' ),
        \'all_items\' => __( \'Todos los temas\' ),
        \'parent_item\' => __( \'Tema padre\' ),
        \'parent_item_colon\' => __( \'Tema padre:\' ),
        \'edit_item\' => __( \'Editar tema\' ), 
        \'update_item\' => __( \'Actualizar tema\' ),
        \'add_new_item\' => __( \'Añadir nuevo tema\' ),
        \'new_item_name\' => __( \'Nombre del nuevo tema\' ),
        \'menu_name\' => __( \'Temas\' ),
      );    
     
    // Registrando la taxonomía
      register_taxonomy(\'temas\',array(\'papers\'), array(
        \'hierarchical\' => true,
        \'labels\' => $labels,
        \'show_ui\' => true,
        \'show_in_rest\' => true,
        \'show_admin_column\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'temas\' ),
      ));

      //Taxonomía: TERRITORIOS
    //Traducción para el backend

    $labels = array(
        \'name\' => _x( \'Territorios\', \'taxonomy general name\' ),
        \'singular_name\' => _( \'Territorio\', \'taxonomy singular name\' ),
        \'search_items\' =>  __( \'Buscar territorio\' ),
        \'all_items\' => __( \'Todos los territorios\' ),
        \'parent_item\' => __( \'Territorio padre\' ),
        \'parent_item_colon\' => __( \'Territorio padre:\' ),
        \'edit_item\' => __( \'Editar territorio\' ), 
        \'update_item\' => __( \'Actualizar territorio\' ),
        \'add_new_item\' => __( \'Añadir nuevo territorio\' ),
        \'new_item_name\' => __( \'Nombre del nuevo territorio\' ),
        \'menu_name\' => __( \'Territorios\' ),
      );    
     
    // Registrando la taxonomía
      register_taxonomy(\'territorios\',array(\'papers\'), array(
        \'hierarchical\' => true,
        \'labels\' => $labels,
        \'show_ui\' => true,
        \'show_in_rest\' => true,
        \'show_admin_column\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'territorios\' ),
      ));
     
    }

    add_action( \'init\', \'papers_categories\', 0 );

1 个回复
最合适的回答,由SO网友:Jahid Hasan 整理而成

试试看,你需要的是为wp\\u get\\u归档文件制作一个过滤器();

function custom_post_type_archive_yearly($where,$args){  
    $post_type  = isset($args[\'post_type\'])  ? $args[\'post_type\']  : \'post\';  
    $where = "WHERE post_type = \'$post_type\' AND post_status = \'publish\'";
    return $where;  
}
调用此筛选器挂钩:

add_filter( \'getarchives_where\',\'custom_post_type_archive_yearly\',10,3);
新增您可以显示自定义帖子:

$args = array(
    \'post_type\'    => \'your_custom_post_type\',
    \'type\'         => \'monthly\',
    \'echo\'         => 0
);
var_dump(wp_get_archives($args));

相关推荐

permalinks issue and archives

我对运行在WP 3.3上的一个站点有一个问题,当我们通过“/%post\\u id%/%postname%/”使永久链接成为任何内容时,归档页面会断开并变成404。经过一些研究,我明白了为什么从性能的角度来看,这不是一个好的做法,所以我尝试了建议的备选方案:“/%year%/%postname%/”和“/%post\\u id%/%postname%/”这两个建议都有效,只是只有使用post\\u id的建议,归档URL才会变成“/date/2012/11/”,并被找到。根据permalink的任何其他建