Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1394341

    Hi I am creating a custom function in order to show the number of a certain portfolio post respect to the total number of Portfolio posts with teh same category (my portfolio posts are characterized by one single category).

    The function creates a shortcode to visualize n./tot where
    n = single post number
    tot = tot posts within the category applied to the actual post

    Right now my function works, but it retrieves all posts (disregarding the category type) and it is as follows

    add_shortcode('test-count-post', 'get_current_post_num');
    function get_current_post_num() {
       $url = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $curr_post_id = url_to_postid( "https://".$url )
      
      $args = array(
          'post_type' => 'portfolio',
          'post_status' => 'publish',
          'order'=> 'ASC',
          'posts_per_page' => -1
      );
      $portfolio_posts = new WP_Query($args);
    
      if ($portfolio_posts->have_posts()):
          $total_posts = $portfolio_posts->found_posts;
          $num = 1;
         
          while ($portfolio_posts->have_posts()) : $portfolio_posts->the_post();
          $post_id = get_the_ID();
    
          if ($post_id === $curr_post_id) {
              $output= $num . '/' . $total_posts;
          } else {
              $num++;
          }
        endwhile;
      endif;
    return $output;
    } 

    For example, I have 3 portfolio posts and the output on post 1 is “1/3”. The output on the second published post is “2/3” etc. Of my test posts, the first 2 have category “xx” and the last one has category “yy”
    So I would like to show, when I am in post 1, “1/2”, while when I am on post 3 -> “1/1”

    To do so I tried to add to my function the following code on line 5 (just after having grabbed the postID)

    $category = get_the_category( $curr_post_id);

    but the var_dump gives me an empty array (while I am expecting the ID of the applied category to that specific post). Later I was planning to add the variable $category to the array of $args, in order to retrieve only the posts within that specific category.

    So my question is: why the $category array appears as empty?
    Thanks

    • This topic was modified 1 year, 10 months ago by elenapoliti.
    #1394664

    Hey Elena,
    Thanks for your question, I tested your code above but it would not run on my test site and would give no output.
    Typically you need to use ob_start(); and ob_end_clean(); to show any output.
    I don’t think you can get the category from a url
    $curr_post_id = url_to_postid( "https://".$url );
    $category = get_the_category( $curr_post_id);

    Best regards,
    Mike

    #1394749

    Thanks Mike for your answer. The code above works as I said and you can see it on three test portfolio items at the link in private area. I can see how to add ob_start() and ob_end_clean()

    So you think there is no possibility of obtaining a category if I know the post_ID? because from WP documentation it seems that it is possible. What I don’t understand is if Portfolio categories should be handled as categories or taxonomy (sorry for my ignorance in WP)

    Thanks

    • This reply was modified 1 year, 10 months ago by elenapoliti.
    #1394769

    Hi,
    There probably is a way to do it I just don’t think that you can do it that way, but I think that this is over my head.
    Unfortunately, I couldn’t get your code to work on my site so I couldn’t tinker with it, I’m glad it works on your site.
    I’d like to see this work, but we are probably going to need more advanced help, I would recommend posting it on Upwork.com

    Best regards,
    Mike

    #1394805

    Hi Mike, actually I am not interested in hiring somebody for this thing on Upwork.com. I am getting some inputs through Stackoverflow and here what I found.
    The code above I was trying to use to get the category was obviously wrong. The right code should be this:

    $categories = get_the_category( $post_id->ID);

    So with my data it should come out as follows

    function get_current_post_num() {
       $url = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $curr_post_id = url_to_postid( "https://".$url )
        $categories = get_the_category(  $curr_post_id->ID);
     //etc etc

    The problem is that in the portfolio item the array returned is 0, while the same exact code applied to a blog single post returns an array with all the terms (hence the categories applied).

    So I come back to my previous question: how are the portfolio categories treated??? How can I access them? This is a question that should pertain to the Enfold support I believe.

    Thank you very much

    • This reply was modified 1 year, 10 months ago by elenapoliti.
    #1394847

    This last post is to close the topic. I have found the solution and it was only a matter of understanding how it was named the taxonomy for the portfolio entries. Easy question. However the working code is here below.
    Explanation: when on a single portfolio entry, it shows number of single post and total number of posts within one category)

    add_shortcode('post-counter', 'get_current_post_num');
    function get_current_post_num() {
      $url = $_SERVER['REQUEST_URI'];
      
      $curr_post_id = url_to_postid( "https://sharkrayareas.org".$url ); //Too early for $post object
      $taxonomy = 'portfolio_entries';
      
      $term_objs = get_the_terms( $curr_post_id->ID, $taxonomy );
    
      foreach ($term_objs as $term_obj)
                   $term_ids = $term_obj->term_id; // get the id from the WP_Term object
    
      
      $args = array(
          'post_type' => 'portfolio',
          'post_status' => 'publish',
          'tax_query' => array(
            array(
              'taxonomy' => 'portfolio_entries',
              'terms'    => $term_ids,
            ),
          ),      
          'order'=> 'ASC',
          'posts_per_page' => -1
      );
      $portfolio_posts = new WP_Query($args);
      
      if ($portfolio_posts->have_posts()):
        echo 'true';
          $total_posts = $portfolio_posts->found_posts;
          
          $num = 1;
         
          while ($portfolio_posts->have_posts()) : $portfolio_posts->the_post();
          $post_id = get_the_ID();
    
          if ($post_id === $curr_post_id) {
              $output= '<div class="count">'. $num . '/' . $total_posts .'</div>';
          } else {
              $num++;
          }
        endwhile;
      endif;
    return $output;
    }
    #1394856

    Hi,

    Yes, that is correct. You have to retrieve the terms from the portfolio_entries taxonomy. Let us know if you require more assistance.

    Best regards,
    Ismael

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.