Suporte » Ajustando o WordPress » Custom Post Type – Error 404 na páginação

Visualizando 5 respostas - 1 até 5 (de um total de 5)
  • Criador do tópico Nura

    (@archer-master)

    add_rewrite_rule("^noticias/([^/]+)/([^/]+)/?",'index.php?post_type=noticias&category=$matches[1]&noticias=$matches[2]','top');
    
    // http://www.localhost/wordpress/noticias/animes/ola-mundo/ 
    
    add_rewrite_rule("^noticias/([^/]+)/page/(\d+)/?",'index.php?post_type=noticias&category=$matches[1]&paged=$matches[2]','top');
    
    // http://www.localhost/wordpress/noticias/animes/page/2/

    Conseguir reescrever a url mas quando eu clico em página 2 só mostra na url e nem mostra o posts da mesma

    • Esta resposta foi modificada 7 anos, 2 meses atrás por Nura.
    Criador do tópico Nura

    (@archer-master)

    Resolvi esse problema, agora quando eu clico na postagem aparece error 404. Alguém pode me explicar o que eu posso resolver essa situação.
    Url que estão oks:
    localhost/wordpress/noticias/
    localhost/wordpress/noticias/anime/page/2
    localhost/wordpress/noticias/anime/page/3

    A url que dá error 404 : localhost/wordpress/noticias/animes/nome-da-postagem

    CPT Notícias – http://pastebin.com/NepP8tAP
    Função Atual:

    // Rewrite permalink structure
    function noticias_rewrite() {
        global $wp_rewrite;
      
        $wp_rewrite->add_permastruct( 'noticias', '/noticias/%category%/', false );
    }
    add_action( 'init', 'noticias_rewrite' );
    
    function noticias_permalink( $post_link, $id = 0, $leavename ) {
        global $wp_rewrite;
        $post = &get_post( $id );
        if ( is_wp_error( $post ) )
            return $post;
            $newlink = $wp_rewrite->get_extra_permastruct( 'noticias' );
            $newlink = str_replace( '%category%', $post->post_name, $newlink );
            $newlink = home_url( user_trailingslashit( $newlink ) );
        return $newlink;
    }
    add_filter('post_type_link', 'noticias_permalink', 1, 3);
    
    function noticias_post_link($post_link, $post, $leavename) {
        if($post->post_type == 'noticias') {
            $category_slug = get_the_terms($post->ID, 'category');
            $category_slug = is_array($category_slug) ? '/noticias/' . $category_slug[0]->slug . '/' : '/';
            $post_link = str_replace('/'.$post->post_type.'/', $category_slug, $post_link);
        }
        return $post_link;
    }
    add_filter('post_type_link', 'noticias_post_link', 10, 3);
    Criador do tópico Nura

    (@archer-master)

    Baixei um plugin pra analizar a url e me mostrou isso, alguém pode me ajudar. Obrigado

    Criador do tópico Nura

    (@archer-master)

    Como posso deixar a url sem o ID caso não tenha categoria marcada deixar sem o post->ID e retorna só o cpt e o nome da url?

    Quero que fique assim
    Exemplo: localhost/noticias/novidades-sobre-o-site

    E não desse jeito: localhost/noticias/84/novidades-sobre-o-site

    function category_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, ‘%noticias_cat%’) === FALSE) return $permalink;
    $post = get_post($post_id);
    if (!$post) return $permalink;
    $terms = wp_get_object_terms($post->ID, ‘noticias_cat’);
    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
    $taxonomy_slug = $terms[0]->slug;
    else $taxonomy_slug = “$post->ID” ;

    return str_replace(‘%noticias_cat%’, $taxonomy_slug, $permalink);
    }
    add_filter(‘post_link’, ‘category_permalink’, 1, 3);
    add_filter(‘post_type_link’, ‘category_permalink’, 1, 3);

    Eu pensei em criar outro tópico porque os códigos acima ficaram com problemas, só preciso resolver isso.

    • Esta resposta foi modificada 7 anos, 2 meses atrás por Nura.
    • Esta resposta foi modificada 7 anos, 2 meses atrás por Nura.
    Criador do tópico Nura

    (@archer-master)

    Tô usando essa nova função:

    Quero que fique assim
    Exemplo: localhost/noticias/novidades-sobre-o-site

    E não desse jeito: localhost/noticias/84/novidades-sobre-o-site

    add_filter( ‘post_type_link’, ‘my_post_type_link’, 99, 2 );
    function my_post_type_link( $post_link, $post = 0 ) {
    $newlink = $post_link;
    if ( !empty($post) ) {
    $post_type = get_post_type($post->ID);

    if($post_type == “noticias”) {
    $newlink = str_replace(‘%noticias_cat%’, $post->ID, $post_link);
    } else {
    $newlink = str_replace(‘%noticias_cat%’, ”);
    }
    }

    return $newlink;
    }

Visualizando 5 respostas - 1 até 5 (de um total de 5)
  • O tópico ‘Custom Post Type – Error 404 na páginação’ está fechado para novas respostas.