Forum Replies Created

Viewing 8 replies - 16 through 23 (of 23 total)
  • Thread Starter testarn

    (@testarn)

    Hi @bcworkz the permalinks flush i’ve already tried and doesn’t work. I cant post the website here but i can send u in pm on slack!

    This is the code when i created my custom post.

    function my_custom_posttypes() {
    
        // Recipes
        $labels = array(
            'name'               => 'Recipes',
            'singular_name'      => 'Recipes',
            'menu_name'          => 'Recipes',
            'name_admin_bar'     => 'Recipes',
            'add_new'            => 'Add New',
            'add_new_item'       => 'Add New Recipes',
            'new_item'           => 'New Recipes',
            'edit_item'          => 'Edit Recipes',
            'view_item'          => 'View Recipes',
            'all_items'          => 'All Recipes',
            'search_items'       => 'Search Recipes',
            'parent_item_colon'  => 'Parent Recipes:',
            'not_found'          => 'No Recipes found.',
            'not_found_in_trash' => 'No Recipes found in Trash.',
        );
        
        $args = array(
            'labels'             => $labels,
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => array( 'slug' => 'recipes' ),
            'capability_type'    => 'post',
            'has_archive'        => false,
            'hierarchical'       => false,
            'menu_position'      => 5,
            'menu_icon'          => 'dashicons-megaphone',
            'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
            'taxonomies'         => array( 'category', 'post_tag' )
        );
        register_post_type( 'recipes', $args );
    }
    
    add_action('init', 'my_custom_posttypes');
    
    // Flush rewrite rules to add "post name" as a permalink slug
    function my_rewrite_flush() {
        my_custom_posttypes();
        flush_rewrite_rules();
    }
    register_activation_hook( __FILE__, 'my_rewrite_flush' );

    Code for my template :

    <?php 
    
      $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
      $custom_args = array(
          'post_type' => 'recipes',
          'posts_per_page' => 12,
          'paged' => $paged
        );
    
      $custom_query = new WP_Query( $custom_args ); 
      
      $current_date = date("Y-m-d");
    ?>
    
    <?php if ( $custom_query->have_posts() ) : ?>
    
      <!-- the loop -->
      <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
    
      
      <div class="sbo_news_article">
                <a href="<?php the_permalink(); ?>">
                <?php
                if ( $posted_date == get_the_time('H:i') ) {
                  echo"<div><span class=\"sbo_news_date\">I dag $posted_date</span><span class=\"sbo_news_source\">$source</span></div>";
                } else {
                  echo "<div><span class=\"sbo_news_date\">$posted_date</span><span class=\"sbo_news_source\">$source</span></div>";
                }
                ?>
                  <h2 class="sbo_news_title"><?php the_title(); ?></h2></a>
                </div>
    
        <?php endwhile; ?>
    
        <!-- end of the loop -->
    
        <!-- pagination here -->
        <?php
          if (function_exists(custom_pagination)) {
            custom_pagination($custom_query->max_num_pages,"",$paged);
          }
        ?>
    
      <?php wp_reset_postdata(); ?>
    
      <?php else:  ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
      <?php endif; ?>

    Code in functions:

    function custom_pagination($numpages = '', $pagerange = '', $paged='') {
    
      if (empty($pagerange)) {
        $pagerange = 2;
      }
    
      global $paged;
      if (empty($paged)) {
        $paged = 1;
      }
      if ($numpages == '') {
        global $wp_query;
        $numpages = $wp_query->max_num_pages;
        if(!$numpages) {
            $numpages = 1;
        }
      }
    
      $pagination_args = array(
        'base'            => get_pagenum_link(1) . '%_%',
        'format'          => 'page/%#%',
        'total'           => $numpages,
        'current'         => $paged,
        'show_all'        => False,
        'end_size'        => 1,
        'mid_size'        => $pagerange,
        'prev_next'       => False,
        'prev_text'       => __('&laquo;'),
        'next_text'       => __('&raquo;'),
        'type'            => 'plain',
        'add_args'        => false,
        'add_fragment'    => ''
      );
    
      $paginate_links = paginate_links($pagination_args);
    
      if ($paginate_links) {
        echo "<nav class='custom-pagination'>";
          echo "<span class='page-numbers page-num'>Sida " . $paged . " av " . $numpages . "</span> ";
          echo $paginate_links;
        echo "</nav>";
      }
    
    }
    Thread Starter testarn

    (@testarn)

    Thanks! 🙂

    Thread Starter testarn

    (@testarn)

    Thanks, solved! 🙂

    Thread Starter testarn

    (@testarn)

    haha wonderful, thank you!

    Thread Starter testarn

    (@testarn)

    @sterndata thanks for that advice!

    Is it hard to develope by yourself? i´m doing this mostly to learn wordpress 🙂

    Thread Starter testarn

    (@testarn)

    Thank you!! @bcworkz

    Thread Starter testarn

    (@testarn)

    @bcworkz

    wow!! It’s actually working now!!! i added this code in my functions.php and everything works now! Thank you so much!!

    Is everything correct?

    
    function enable_category($query) {
      if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_category()) {
          $query->set('post_type', array( 'post', 'review' ) );
        }
      }
    }
    
    function enable_tags($query) {
      if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_tag()) {
          $query->set('post_type', array( 'post', 'review' ) );
        }
      }
    }
    
    add_action('pre_get_posts','enable_category');
    add_action('pre_get_posts','enable_tags');
    
    • This reply was modified 9 years, 5 months ago by testarn.
    Thread Starter testarn

    (@testarn)

    @bcworkz

    Thank you i got the first part working but i dont understand your “files not found issue” explanation at all. Could you explain it easier some how? i´m really new to wordpress. Or if u know any guide i can follow?

    Thanks again!

Viewing 8 replies - 16 through 23 (of 23 total)